Constructor
new CommandLineArgs(definitions)
The constructor will throw if you pass invalid option definitions. You should fix these issues before proceeding.
Parameters:
Name | Type | Description |
---|---|---|
definitions |
Array.<module:definition> | An optional array of [OptionDefinition](#exp_module_definition--OptionDefinition) objects |
- Source:
Throws:
-
`NAME_MISSING` if an option definition is missing the required `name` property
-
`INVALID_TYPE` if an option definition has a `type` value that's not a function
-
`INVALID_ALIAS` if an alias is numeric, a hyphen or a length other than 1
-
`DUPLICATE_NAME` if an option definition name was used more than once
-
`DUPLICATE_ALIAS` if an option definition alias was used more than once
-
`DUPLICATE_DEFAULT_OPTION` if more than one option definition has `defaultOption: true`
Example
```js
const commandLineArgs = require('command-line-args')
const cli = commandLineArgs([
{ name: 'file' },
{ name: 'verbose' },
{ name: 'depth'}
])
```
Methods
getUsage(optionsopt) → {string}
Generates a usage guide. Please see [command-line-usage](https://github.com/75lb/command-line-usage) for full instructions of how to use.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
options |
object |
<optional> |
the options to pass to [command-line-usage](https://github.com/75lb/command-line-usage) |
- Source:
Returns:
- Type
- string
parse(argvopt) → {object}
Returns an object containing all the values and flags set on the command line. By default it parses the global [`process.argv`](https://nodejs.org/api/process.html#process_process_argv) array.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
argv |
Array.<string> |
<optional> |
An array of strings, which if passed will be parsed instead of `process.argv`. |
- Source:
Throws:
`UNKNOWN_OPTION` if the user sets an option without a definition
Returns:
- Type
- object