This article mainly introduces the example of using the nodejs command line parameter processing module commander. commander is a very tall command line parameter processing module. For more information, see, previously, we used the built-in process. agrv, this can work, but it is not easy to make, so the tj god wrote a, my god, completely tall:
1. Installation
The Code is as follows:
Npm install commander
2. option Parsing
Options with commander are defined with. option () method, also serving as documentation for the options. the example below parses args and options from process. argv, leaving remaining args as the program. args array which were not consumed by options.
The Code is as follows:
#! /Usr/bin/env node
/**
* Module dependencies.
*/
Var program = require ('commander ');
Program
. Version ('0. 0.1 ')
. Option ('-p, -- peppers', 'add peppers ')
. Option ('-P, -- pineapple', 'add pineapple ')
. Option ('-B, -- bbq', 'add bbq sauce ')
. Option ('-c, -- cheese [type]', 'add the specified type of cheese [marble] ', 'marble ')
. Parse (process. argv );
Console. log (you ordered a pizza :');
If (program. peppers) console. log ('-peppers ');
If (program. pineapple) console. log ('-pineapple ');
If (program. bbq) console. log ('-bbq ');
Console. log ('-% s cheese', program. cheese );
Short flags may be passed as a single arg, for example-abc is equivalent to-a-B-c. multi-word options such as "-template-engine" are camel-cased, becoming program. templateEngine etc.
3. automatically generate help information
The Code is as follows:
$./Examples/pizza -- help
Usage: pizza [options]
Options:
-V, -- version output the version number
-P, -- peppers Add peppers
-P, -- pineapple Add pineapple
-B, -- bbq Add bbq sauce
-C, -- cheese Add the specified type of cheese [marble]
-H, -- help output usage information
Of course, you can also manually generate:
The Code is as follows:
#! /Usr/bin/env node
/**
* Module dependencies.
*/
Var program = require ('../');
Function list (val ){
Return val. split (','). map (Number );
}
Program
. Version ('0. 0.1 ')
. Option ('-f, -- foo', 'Enable some foo ')
. Option ('-B, -- bar', 'Enable some bar ')
. Option ('-B, -- Baz', 'Enable some Baz ');
// Must be before. parse () since
// Node's emit () is immediate
Program. on ('-- help', function (){
Console. log ('examples :');
Console. log ('');
Console. log ('$ custom-help -- help ');
Console. log ('$ custom-help-H ');
Console. log ('');
});
Program. parse (process. argv );
Console. log ('stuff ');
4. A complete example
The Code is as follows:
Function range (val ){
Return val. split ('...'). map (Number );
}
Function list (val ){
Return val. split (',');
}
Function collect (val, memo ){
Memo. push (val );
Return memo;
}
Function increaseVerbosity (v, total ){
Return total + 1;
}
Program
. Version ('0. 0.1 ')
. Usage ('[options] ')
. Option ('-I, -- integer ', 'An integer argument', parseInt)
. Option ('-f, -- float ', 'A float argument', parseFloat)
. Option ('-r, -- range .. ', 'A range', range)
. Option ('-l, -- list ', 'A list', list)
. Option ('-o, -- optional [value]', 'an optional value ')
. Option ('-c, -- collect [value]', 'a repeatable value', collect, [])
. Option ('-v, -- verbose', 'a value that can be increased', increaseVerbosity, 0)
. Parse (process. argv );
Console. log ('int: % J', program. integer );
Console. log ('float: % J', program. float );
Console. log ('optional: % J', program. optional );
Program. range = program. range | [];
Console. log ('range: % j .. % J', program. range [0], program. range [1]);
Console. log ('list: % J', program. list );
Console. log ('Collect: % J', program. collect );
Console. log ('verbosity: % J', program. verbose );
Console. log ('args: % J', program. args );