Package Exports
- command-line-args
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (command-line-args) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
work in progress, draft documentation
command-line-args
A command-line parser and usage-guide producer.. Particularly good at organising large sets of options.
Install
$ npm install command-line-args --save
Synopsis
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
command-line-args
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();
- command-line-args
- CliArgs ⏏
- new CliArgs(options)
- instance
- .parse([argv]) ⇒
object
- .getUsage(options) ⇒
string
- .parse([argv]) ⇒
- inner
- ~OptionDefinition :
object
- ~OptionDefinition :
- CliArgs ⏏
CliArgs ⏏
new CliArgs(options)
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()
.
Param | Type | Description |
---|---|---|
options | Array.<OptionDefinition> |
list of option definitions |
cliArgs.parse([argv]) ⇒ object
Returns a flat, or grouped object containing the values set at the command-line
Kind: instance method of CliArgs
Param | Type | Default | Description |
---|---|---|---|
[argv] | object |
process.argv |
Optional 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"
}
}
cliArgs.getUsage(options) ⇒ string
Kind: instance method of CliArgs
Param | Type | Description |
---|---|---|
options | object |
options for template |
options.title | string |
a title |
options.header | string |
a header |
options.footer | string |
a footer |
options.forms | array |
the invocation forms |
CliArgs~OptionDefinition : object
Defines an option
Kind: inner typedef of CliArgs
Properties
Name | Type | Description |
---|---|---|
name | string |
the option name, used as the long option (e.g. --name ) |
type | function |
an optional function (e.g. Number or a custom function) used as a setter to enforce type. |
alias | string |
a single character alias, used as the short option (e.g. -n ) |
defaultOption | boolean |
if values are specified without an option name, they are assigned to the defaultOption |
description | string |
used in the usage guide |
© 2015 Lloyd Brookes 75pound@gmail.com. Documented by jsdoc-to-markdown.