[command-line-args](
[To see links please register here]
) is worth a look!
You can set options using the main notation standards ([learn more](
[To see links please register here]
)). These commands are all equivalent, setting the same values:
$ example --verbose --timeout=1000 --src one.js --src two.js
$ example --verbose --timeout 1000 --src one.js two.js
$ example -vt 1000 --src one.js two.js
$ example -vt 1000 one.js two.js
To access the values, first create a list of [option definitions](
[To see links please register here]
) describing the options your application accepts. The [`type`](
[To see links please register here]
) property is a setter function (the value supplied is passed through this), giving you full control over the value received.
const optionDefinitions = [
{ name: 'verbose', alias: 'v', type: Boolean },
{ name: 'src', type: String, multiple: true, defaultOption: true },
{ name: 'timeout', alias: 't', type: Number }
]
Next, parse the options using [commandLineArgs()](
[To see links please register here]
):
const commandLineArgs = require('command-line-args')
const options = commandLineArgs(optionDefinitions)
`options` now looks like this:
{
src: [
'one.js',
'two.js'
],
verbose: true,
timeout: 1000
}
### Advanced usage
Beside the above typical usage, you can configure command-line-args to accept more advanced syntax forms.
[Command-based syntax](
[To see links please register here]
) (git style) in the form:
$ executable <command> [options]
For example.
$ git commit --squash -m "This is my commit message"
[Command and sub-command syntax](
[To see links please register here]
) (docker style) in the form:
$ executable <command> [options] <sub-command> [options]
For example.
$ docker run --detached --image centos bash -c yum install -y httpd
## Usage guide generation
A usage guide (typically printed when `--help` is set) can be generated using [command-line-usage](
[To see links please register here]
). See the examples below and [read the documentation](
[To see links please register here]
) for instructions how to create them.
A typical usage guide example.
![usage](
[To see links please register here]
)
The [polymer-cli](
[To see links please register here]
) usage guide is a good real-life example.
![usage](
[To see links please register here]
)
## Further Reading
There is plenty more to learn, please see [the wiki](
[To see links please register here]
) for examples and documentation.