Skip to content
Myr

reorganize readme

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by zhiyelee · committed by zhiyelee · parent 0b95ea2 · Browse files · 1 file+90 added−49 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+90 added−49 removed

  • other:+90 added−49 removed

1 file+90 added−49 removed

modified:Readme.md+90 added-49 removed
3 unmodified lines
4[![NPM Version](http://img.shields.io/npm/v/commander.svg?style=flat)](https://www.npmjs.org/package/commander)5[![NPM Downloads](https://img.shields.io/npm/dm/commander.svg?style=flat)](https://www.npmjs.org/package/commander)67 The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/tj/commander). 8 API documentation: [http://tj.github.com/commander.js/](http://tj.github.com/commander.js/)8 [API documentation](http://tj.github.com/commander.js/)91011## Installation12
28 unmodified lines
41```4243 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.444546## Coercion4748```js49function range(val) {50 return val.split('..').map(Number);51}5253function list(val) {54 return val.split(',');55}5657function collect(val, memo) {58 memo.push(val);59 return memo;60}6162function increaseVerbosity(v, total) {63 return total + 1;64}6566program67 .version('0.0.1')68 .usage('[options] <file ...>')69 .option('-i, --integer <n>', 'An integer argument', parseInt)70 .option('-f, --float <n>', 'A float argument', parseFloat)71 .option('-r, --range <a>..<b>', 'A range', range)72 .option('-l, --list <items>', 'A list', list)73 .option('-o, --optional [value]', 'An optional value')74 .option('-c, --collect [value]', 'A repeatable value', collect, [])75 .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)76 .parse(process.argv);7778console.log(' int: %j', program.integer);79console.log(' float: %j', program.float);80console.log(' optional: %j', program.optional);81program.range = program.range || [];82console.log(' range: %j..%j', program.range[0], program.range[1]);83console.log(' list: %j', program.list);84console.log(' collect: %j', program.collect);85console.log(' verbosity: %j', program.verbose);86console.log(' args: %j', program.args);87```8889## Variadic arguments9091 The last argument of a command can be variadic, and only the last argument. To make an argument variadic you have to92 append `...` to the argument name. Here is an example:
64 unmodified lines
157 -C, --no-cheese You do not want any cheese158159```160117## Coercion118119```js120function range(val) {121 return val.split('..').map(Number);122}123124function list(val) {125 return val.split(',');126}127128function collect(val, memo) {129 memo.push(val);130 return memo;131}132133function increaseVerbosity(v, total) {134 return total + 1;135}136137program138 .version('0.0.1')139 .usage('[options] <file ...>')140 .option('-i, --integer <n>', 'An integer argument', parseInt)141 .option('-f, --float <n>', 'A float argument', parseFloat)142 .option('-r, --range <a>..<b>', 'A range', range)143 .option('-l, --list <items>', 'A list', list)144 .option('-o, --optional [value]', 'An optional value')145 .option('-c, --collect [value]', 'A repeatable value', collect, [])146 .option('-v, --verbose', 'A value that can be increased', increaseVerbosity, 0)147 .parse(process.argv);148149console.log(' int: %j', program.integer);150console.log(' float: %j', program.float);151console.log(' optional: %j', program.optional);152program.range = program.range || [];153console.log(' range: %j..%j', program.range[0], program.range[1]);154console.log(' list: %j', program.list);155console.log(' collect: %j', program.collect);156console.log(' verbosity: %j', program.verbose);157console.log(' args: %j', program.args);158```159161## Custom help162163 You can display arbitrary `-h, --help` information164 by listening for "--help". Commander will automatically
61 unmodified lines
226## .help()227228 Output help information and exit immediately.229229## Links230## Examples231231 - [ascii tables](https://github.com/LearnBoost/cli-table)232 - [progress bars](https://github.com/tj/node-progress)233 - [more progress bars](https://github.com/substack/node-multimeter)234 - [examples](https://github.com/tj/commander.js/tree/master/examples)232```js233var program = require('commander');234235program236 .version('0.0.1')237 .option('-C, --chdir <path>', 'change the working directory')238 .option('-c, --config <path>', 'set config path. defaults to ./deploy.conf')239 .option('-T, --no-tests', 'ignore test hook')240241program242 .command('setup [env]')243 .description('run setup commands for all envs')244 .option("-s, --setup_mode [mode]", "Which setup mode to use")245 .action(function(env, options){246 var mode = options.setup_mode || "normal";247 env = env || 'all';248 console.log('setup for %s env(s) with %s mode', env, mode);249 });250251program252 .command('exec <cmd>')253 .alias('ex')254 .description('execute the given remote cmd')255 .option("-e, --exec_mode <mode>", "Which exec mode to use")256 .action(function(cmd, options){257 console.log('exec "%s" using %s mode', cmd, options.exec_mode);258 }).on('--help', function() {259 console.log(' Examples:');260 console.log();261 console.log(' $ deploy exec sequential');262 console.log(' $ deploy exec async');263 console.log();264 });265266program267 .command('*')268 .action(function(env){269 console.log('deploying "%s"', env);270 });271272program.parse(process.argv);273```274275You can see more Demos in the [examples](https://github.com/tj/commander.js/tree/master/examples) directory.276277## License278279(The MIT License)280
20 unmodified lines