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_OPTION
Ifoptions.partial
is false and the user set an undefined option. Theerr.optionName
property contains the arg that specified an unknown option, e.g.--one
.UNKNOWN_VALUE
Ifoptions.partial
is false and the user set a value unaccounted for by an option definition. Theerr.value
property contains the unknown value, e.g.5
.ALREADY_SET
If a user sets a singular, non-multiple option more than once. Theerr.optionName
property contains the option name that has already been set, e.g.one
.INVALID_DEFINITIONS
- If an option definition is missing the required
name
property - If an option definition has a
type
value 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
Boolean
option is also set as thedefaultOption
.
- If an option definition is missing the required
Param | Type | Description |
---|---|---|
optionDefinitions | Array.<OptionDefinition> |
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 ). |
[options.caseInsensitive] | boolean |
If true , the case of each option name or alias parsed is insignificant. In other words, both --Verbose and --verbose , -V and -v would be equivalent. Defaults to false. |