2.9 KiB
2.9 KiB
command-line-args
commandLineArgs(optionDefinitions, [options]) ⇒ object ⏏
Returns an object containing all option values set on the command line. By default it parses the global process.argv array.
Parsing is strict by default - an exception is thrown if the user sets a singular option more than once or sets an unknown value or option (one without a valid definition). To be more permissive, enabling partial or stopAtFirstUnknown modes will return known options in the usual manner while collecting unknown arguments in a separate _unknown property.
Kind: Exported function
Throws:
UNKNOWN_OPTIONIfoptions.partialis false and the user set an undefined option. Theerr.optionNameproperty contains the arg that specified an unknown option, e.g.--one.UNKNOWN_VALUEIfoptions.partialis false and the user set a value unaccounted for by an option definition. Theerr.valueproperty contains the unknown value, e.g.5.ALREADY_SETIf a user sets a singular, non-multiple option more than once. Theerr.optionNameproperty contains the option name that has already been set, e.g.one.INVALID_DEFINITIONS- If an option definition is missing the required
nameproperty - If an option definition has a
typevalue that's not a function - If an alias is numeric, a hyphen or a length other than 1
- If an option definition name was used more than once
- If an option definition alias was used more than once
- If more than one option definition has
defaultOption: true - If a
Booleanoption is also set as thedefaultOption.
- If an option definition is missing the required
| Param | Type | Description |
|---|---|---|
| optionDefinitions | Array.<module:definition> |
An array of OptionDefinition objects |
| [options] | object |
Options. |
| [options.argv] | Array.<string> |
An array of strings which, if present will be parsed instead of process.argv. |
| [options.partial] | boolean |
If true, an array of unknown arguments is returned in the _unknown property of the output. |
| [options.stopAtFirstUnknown] | boolean |
If true, parsing will stop at the first unknown argument and the remaining arguments returned in _unknown. When set, partial: true is also implied. |
| [options.camelCase] | boolean |
If true, options with hypenated names (e.g. move-to) will be returned in camel-case (e.g. moveTo). |