The nodejs command line parameter processing module uses the commander instance and nodejscommander

Source: Internet
Author: User

The nodejs command line parameter processing module uses the commander instance and nodejscommander

It is true that the previous processing was using the built-in process. agrv, which can work, but it is not easy to use, so the tj god wrote a, my god, completely tall:

1. Installation

Copy codeThe 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.

Copy codeThe 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

Copy codeThe 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 <type> Add the specified type of cheese [marble]
-H, -- help output usage information

Of course, you can also manually generate:
Copy codeThe 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

Copy codeThe 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] <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 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 );


How does the eventproxy template of nodejs handle exceptions?

In nodejs, fail and done are IO-specific, not exception-specific, and must be handled through try-catch. The eventproxy you mentioned is a node. js module, where there is indeed a fail API, which is used to set the callback for the 'error' event.

Generally, nodejs uses callback to set subsequent processing. While nodejs processes callback, it generally carries an err parameter at the same time. If it is not empty, it indicates that an error has occurred, the error handling process should be entered. The main function of eventproxy is to merge the hander of event processing. Therefore, to merge such errors into the handler of 'error', the fail api is to set the handler of 'error.

I don't know if I understand it. If I don't understand it, I can ask.

Also, the problem types should be put in javascript and nodejs.

Nodejs processes the obtained results





Related Article

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.