Nodejs command line parameter processing module Commander use instance _node.js

Source: Internet
Author: User

Admittedly, the previous processing is using the built-in PROCESS.AGRV, this can be work, but not, so TJ Great God wrote A, my God, completely tall:

1, installation

Copy Code code as follows:

NPM Install Commander

2, option resolution

Options with Commander are defined with the 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 n OT consumed by options.

Copy Code code 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 ' specified type of cheese [marble] ', ' marble ')
. Parse (PROCESS.ARGV);

Console.log (' You ordered a pizza with: ');
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 could passed as a single arg, for EXAMPLE-ABC is equivalent to-a-b-c. Multi-word options such as "–templa Te-engine "are camel-cased, becoming program.templateengine etc.

3. Automatically generate Help information

Copy Code code 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 <type> ADD The specified type of cheese [marble]
-h,--help output usage information

Of course you can also manually generate:

Copy Code code 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 ('--he LP ', 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, to give a complete example

Copy Code code 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] <file ...> ')
. option ('-I,--integer <n> ', ' an integer argument ', parseint)
. Option (' F,--float <n> ', ' A float argument ', parsefloat)
. Option (' R,--range <a> <b> ', ' A range ', range
. Option ('-L,--list <items> ', ' A list ', list)
. Option (' O,--optional [value] ', ' an optional value ')
. option ('-C,--collect [value] ', ' A repeatable value ', collect, [])
. Option ('-V,--verbose ', ' A value ' 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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.