Deviopath › TypeScript

Parse Command-Line Arguments

Hard · Parsing

Write parseArgs(argv, spec). spec maps long names to a type, an optional one-letter alias and an optional default.

- --name value or --name=value sets a string/number option. The value is the next argument even if it starts with -. Numbers use Number(value), must be finite and not blank.

- --flag sets a boolean to true, --no-flag to false.

- -x uses an alias. In a group like -abc all but the last letter must be booleans; the last may take a value.

- After -- every argument is positional; so is any other argument not starting with -.

- A repeated option keeps its last value. Unused options get their default, if any.

Stop at the first error: Unknown option: --<name> (no =value) or -<letter>, Missing value for --<name>, Invalid number for --<name> or Option --<name> does not take a value.

Next: Convert Compound Units