-
Notifications
You must be signed in to change notification settings - Fork 105
Partial parsing
Lloyd Brookes edited this page Jan 14, 2018
·
2 revisions
By default, commandLineArgs
will throw an exception if the user set an unexpected option or value. This is often what we need, although there are times when need to be permissive, for example when collecting args we intend to pass to a different application. To achieve this we use partial parsing mode.
In this example, we define a --value
option and enable partial parsing (set partial: true
in the commandLineArgs
options). Save the script as example.js
.
const commandLineArgs = require('command-line-args')
const optionDefinitions = [
{ name: 'value', type: Number }
]
const options = commandLineArgs(optionDefinitions, { partial: true })
console.log(options)
Set one known option (--value
) and two unknown options (--milk
and --bread
).
$ node example.js --milk --value 2 --bread cheese
Notice the _unknown
property contains the unknown args. No exceptions are thrown.
{
value: 2,
_unknown: [ '--milk', '--bread', 'cheese']
}