Nodejs command line parameter processing module Commander Use instance

Source: Internet
Author: User
Tags require

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

The code is 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.

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 ' 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

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, to give 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 ' 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.