Nodejs command line parameter processing module commander uses instance _ node. js-js tutorial

Source: Internet
Author: User
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 );

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.