command-line-args

Command-line parser, usage text producer

work in progress, draft documentation

command-line-args

A command-line parser and usage-guide producer.. Particularly good at organising large sets of options.

$ npm install command-line-args --save

the following app.js...

var cliArgs = require("command-line-args");
 
/* define the command-line options */
var cli = cliArgs([
    { name: "verbose", type: Boolean, alias: "v", description: "Write plenty output" },
    { name: "help", type: Boolean, description: "Print usage instructions" },
    { name: "files", type: Array, defaultOption: true, description: "The input files" }
]);
 
/* parse the supplied command-line values */
var options = cli.parse();
 
/* generate a usage guide */
var usage = cli.getUsage({
    header: "A synopsis application.",
    footer: "For more information, visit http://example.com"
});
    
console.log(options.help ? usage : options);

...returns this output at the command line:

$ node app.js
{}
 
$ node app.js -v
{ verbose: true }
 
$ node app.js README.md package.json
{ files: [ 'README.md''package.json' ] }
 
$ node app.js README.md package.json -v
{ verbose: true, files: [ 'README.md''package.json' ] }
 
$ node app.js --help
 
  A synopsis application.
 
  Usage
 
  -v, --verbose    Write plenty output
  --help           Print usage instructions
  --files <array>  The input files
 
  For more information, visit http://example.com
 

API Reference

Example

var cliArgs = require("command-line-args");
var cli = cliArgs([
    { name: "help", type: Boolean, alias: "h" },
    { name: "files", type: Array, defaultOption: true}
]);
var argv = cli.parse();

A constructor function, taking your desired command-line option definitions as input, returning an instance of command-line-args which you can parse() or getUsage().

ParamTypeDescription
optionsArray.<OptionDefinition>list of option definitions

Returns a flat, or grouped object containing the values set at the command-line

ParamTypeDefaultDescription
[argv]objectprocess.argvOptional argv array, pass to override the default process.argv.

Example
Output from parse() looks something like this:

{
    delete: "thisfile.txt",
    force: true
}

or, if the option definitions are grouped:

{
    standard: {
        delete: "thisfile.txt",
        force: true
    },
    extra: {
        intentions: "bad"
    }
}

ParamTypeDescription
optionsobjectoptions for template
options.titlestringa title
options.headerstringa header
options.footerstringa footer
options.formsarraythe invocation forms

Defines an option

Properties

NameTypeDescription
namestringthe option name, used as the long option (e.g. --name)
typefunctionan optional function (e.g. Number or a custom function) used as a setter to enforce type.
aliasstringa single character alias, used as the short option (e.g. -n)
defaultOptionbooleanif values are specified without an option name, they are assigned to the defaultOption
descriptionstringused in the usage guide

documented by jsdoc-to-markdown.