raptor-args
A flexible and simple command line arguments parser that generates friendly help messages.
Installation
npm install raptor-args --save
Usage
// Create a parser:var parser = ;var parsed = parser; // parsed will be an object with properties corresponding to provided arguments
Simple Example
Parse arguments provided by process.argv
:
Given the following JavaScript code to parse the args:
// Create a parser and parse process.argv ;
And the following command:
node app.js --foo -b b
The output will be:
//Output: foo: true bar: 'baz'
You can also parse your own array of arguments instead of using process.argv
:
// Create a parser and parse provided args ; //Output: foo: true bar: 'baz'
You can also be more descriptive and add usage, examples, error handlers and validation checks:
// Create a parser: ;
Running the above program with the --help
argument will produce the following output:
Usage: args [options]
Examples:
First example:
args --foo hello
Second example:
args --foo hello --bar world
Options:
--help Show this help message [string]
--foo -f Some helpful description for "foo" [string]
--bar -b Some helpful description for "bar" [string]
Aliases
Aliases can be provided as space-separated values for an option:
// Create a parser:var parser = ; parser;// Output: foobar: 'FOO' hello: 'HELLO' // **NOTE**: Only the first entry is used to determine the target property name--not the aliases.
Booleans
An argument value of "true" or "false" is automatically converted to the corresponding boolean type. If a argument is prefixed with "no-" then it will be set to false
.
// Create a parser:var parser = ; parser;// Output: foo: true bar: false
Arrays
Any argument with multiple values will result in an Array
value, but if you want to force an array for a single value then you can append "[]" to the option type as shown in the following sample code:
// Create a parser:var parser = ; parser;// Output: foo: 'a' parser;// Output: foo: 'a' 'b' 'c'
Wildcards
A parser will throw an error for unrecognized arguments unless wildcards are used as shown in the examples below.
// Create a parser:var parser = ; parser;// Output: foo: 'a' 'b' 'c'
// Create a parser:var parser = ; parser;// Output: '*': 'a' 'b' foo: 'FOO' bar: 'BAR'
Complex Types
Square brackets can be used to begin and end complex types:
// Create a parser:var parser = ; var parsed = parser; // Output: foo: true plugins: module: 'plugin1' x: true y: true module: 'plugin2' z: 'Hello'
Similar Projects
- optimist - Popular but deprecated. Awkward API and not DRY as shown in the following comparison:
optimist:
var result = args argv;
raptor-args:
var result = ;
- yargs - A fork of
optimist
with documentation for those who speak Pirate. - minimist - Very few features (by design). Not DRY.
TODO
- Support equal separator:
--hello=world
- Support number arg:
-x256
- Detect repeated declared options and throw an error
- Add support for a default value
var parser = ;
Additional Reading
For module help, check out the test cases under the "test" directory.
License
MIT