command-line-args

A mature, feature-complete library to parse command-line options.

MIT 76 个版本
安装
npm install command-line-args
yarn add command-line-args
pnpm add command-line-args
bun add command-line-args
README

view on npm npm module downloads Gihub repo dependents Gihub package dependents Node.js CI

Upgraders, please read the release notes

command-line-args

A mature, feature-complete library to parse command-line options.

Synopsis

You can set options using the main notation standards (learn more). 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 describing the options your application accepts. The type 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():

import commandLineArgs from '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 (git style) in the form:

    $ executable <command> [options]
    

    For example.

    $ git commit --squash -m "This is my commit message"
    
  • Command and sub-command syntax (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. See the examples below and read the documentation for instructions how to create them.

A typical usage guide example.

usage

The polymer-cli usage guide is a good real-life example.

usage

Further Reading

There is plenty more to learn, please see the wiki for examples and documentation.

Install

$ npm install command-line-args --save

© 2014-26 Lloyd Brookes <opensource@75lb.com>.

Documented by jsdoc-to-markdown.

版本列表
6.0.2 2026-03-23
6.0.1 2024-10-28
6.0.0 2024-07-05
6.0.0-preview.1 2018-06-25
5.2.1 2022-01-29
5.2.0 2021-07-29
5.1.3 2021-07-12
5.1.2 2021-07-11
5.1.1 2019-03-31
5.1.0 2019-03-24
5.0.2 2018-02-07
5.0.1 2018-01-19
5.0.0-alpha.2 2018-01-17
5.0.0-alpha.1 2018-01-16
5.0.0-alpha.0 2018-01-15
5.0.0 2018-01-17
4.0.7 2017-08-09
4.0.6 2017-05-30
4.0.5 2017-05-23
4.0.4 2017-05-02
4.0.3 2017-04-22
4.0.2 2017-03-06
4.0.1 2017-02-01
4.0.0-1 2017-01-31
4.0.0-0 2017-01-28
4.0.0 2017-01-31
3.0.5 2016-12-25
3.0.4 2016-12-03
3.0.3-0 2016-11-08
3.0.3 2016-11-08
3.0.2 2016-10-31
3.0.1 2016-07-31
3.0.0 2016-05-29
2.1.6 2016-02-15
2.1.5 2016-02-15
2.1.4 2016-01-01
2.1.3 2015-11-29
2.1.2 2015-11-28
2.1.1 2015-11-11
2.1.0 2015-11-04
2.0.4 2015-10-28
2.0.3 2015-10-28
2.0.2 2015-10-12
2.0.1 2015-10-12
2.0.0 2015-10-12
1.1.0-1 2015-08-09
1.1.0-0 2015-08-05
1.0.2 2015-09-28
1.0.1 2015-09-21
1.0.0-3 2015-07-10
1.0.0-2 2015-07-09
1.0.0-1 2015-07-09
1.0.0-0 2015-07-06
1.0.0 2015-08-13
0.5.9 2015-03-18
0.5.8 2015-03-17
0.5.7 2015-03-14
0.5.6 2015-03-14
0.5.5 2015-02-01
0.5.4 2015-02-01
0.5.3 2014-11-05
0.5.2 2014-10-09
0.5.1 2014-09-18
0.5.0 2014-07-15
0.4.0 2014-06-25
0.3.1 2014-06-16
0.3.0 2014-06-16
0.2.4 2014-06-13
0.2.3 2014-06-09
0.2.2 2014-06-08
0.2.1 2014-06-05
0.2.0 2014-06-01
0.1.0 2014-05-30
0.0.2 2014-05-27
0.0.1 2014-05-26
0.0.0 2014-05-26