What is command-line-args?
The command-line-args npm package is a library for parsing command-line arguments in Node.js applications. It allows developers to define options and parse them from the command line, making it easier to handle user inputs and configurations.
What are command-line-args's main functionalities?
Basic Argument Parsing
This feature allows you to define and parse basic command-line arguments. In this example, the 'file' option expects a string value, and the 'verbose' option is a boolean flag.
const commandLineArgs = require('command-line-args');
const optionDefinitions = [
{ name: 'file', type: String },
{ name: 'verbose', type: Boolean }
];
const options = commandLineArgs(optionDefinitions);
console.log(options);
Default Values
This feature allows you to set default values for options. If the user does not provide a value for 'file' or 'verbose', the defaults 'default.txt' and 'false' will be used, respectively.
const commandLineArgs = require('command-line-args');
const optionDefinitions = [
{ name: 'file', type: String, defaultValue: 'default.txt' },
{ name: 'verbose', type: Boolean, defaultValue: false }
];
const options = commandLineArgs(optionDefinitions);
console.log(options);
Multiple Values
This feature allows you to handle multiple values for a single option. In this example, the 'files' option can accept multiple string values.
const commandLineArgs = require('command-line-args');
const optionDefinitions = [
{ name: 'files', type: String, multiple: true, defaultOption: true }
];
const options = commandLineArgs(optionDefinitions);
console.log(options);
Aliases
This feature allows you to define aliases for options. In this example, 'help' can be accessed with '-h' and 'version' with '-v'.
const commandLineArgs = require('command-line-args');
const optionDefinitions = [
{ name: 'help', alias: 'h', type: Boolean },
{ name: 'version', alias: 'v', type: Boolean }
];
const options = commandLineArgs(optionDefinitions);
console.log(options);
Other packages similar to command-line-args
yargs
Yargs is a popular library for parsing command-line arguments in Node.js. It provides a rich set of features, including command handling, argument validation, and automatic help generation. Compared to command-line-args, yargs offers more advanced features and a more user-friendly API.
commander
Commander is another widely-used library for building command-line interfaces in Node.js. It supports option parsing, command definitions, and automatic help generation. Commander is known for its simplicity and ease of use, making it a good alternative to command-line-args for simpler use cases.
minimist
Minimist is a lightweight library for parsing command-line arguments. It provides basic functionality for handling options and arguments, but lacks some of the advanced features found in command-line-args, yargs, and commander. Minimist is a good choice for projects that require minimal overhead.
command-line-args
Collect command-line options, generate a usage guide..
- Support most option notation styles
- long options (
--find lib.js
) - short options (
-f lib.js
) - getopt-style combinations (
-xvf lib.js
) - option=val style (
--find=lib.js
)
- Customisable usage guide generator
- Modular - define reusage option sets.
- Split options into groups, for apps with a large set of options.
- Fine control over validation, type
Install
$ npm install command-line-args --save
API Reference
Modules
- command-line-args
- definition
- definitions
- option
## command-line-args
### CliArgs(definitions, argv) ⇒
object
⏏
**Kind**: Exported function
Param | Type |
---|
definitions | module:command-line-args.argDefType |
argv | Array.<string> |
definition
Definition ⏏
Option Definition
Kind: Exported class
definition.name : string
Kind: instance property of Definition
definition.type : function
Kind: instance property of Definition
definition.description : string
Kind: instance property of Definition
definition.alias : string
a single character
Kind: instance property of Definition
definition.multiple : boolean
Kind: instance property of Definition
definition.defaultOption : boolean
Kind: instance property of Definition
definition.group : string
| Array.<string>
Kind: instance property of Definition
definition.value : boolean
Kind: instance property of Definition
definitions
option
© 2015 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.