commander
Advanced tools
Comparing version 2.5.1 to 2.6.0
55
index.js
@@ -85,2 +85,3 @@ | ||
this._execs = []; | ||
this._allowUnknownOption = false; | ||
this._args = []; | ||
@@ -394,2 +395,14 @@ this._name = name; | ||
/** | ||
* Allow unknown options on the command line. | ||
* | ||
* @param {Boolean} arg if `true` or omitted, no error will be thrown | ||
* for unknown options. | ||
* @api public | ||
*/ | ||
Command.prototype.allowUnknownOption = function(arg) { | ||
this._allowUnknownOption = arguments.length === 0 || arg; | ||
return this; | ||
}; | ||
/** | ||
* Parse `argv`, settings options and invoking commands when defined. | ||
@@ -700,2 +713,3 @@ * | ||
Command.prototype.unknownOption = function(flag) { | ||
if(this._allowUnknownOption) return; | ||
console.error(); | ||
@@ -890,16 +904,37 @@ console.error(" error: unknown option `%s'", flag); | ||
Command.prototype.helpInformation = function() { | ||
return [ | ||
'' | ||
, ' Usage: ' + this._name | ||
+ (this._alias | ||
? '|' + this._alias | ||
: '') | ||
+ ' ' + this.usage() | ||
, '' + this.commandHelp() | ||
, ' Options:' | ||
var desc = []; | ||
if (this._description) { | ||
desc = [ | ||
' ' + this._description | ||
, '' | ||
]; | ||
} | ||
var cmdName = this._name; | ||
if(this._alias) { | ||
cmdName = cmdName + '|' + this._alias; | ||
} | ||
var usage = [ | ||
'' | ||
,' Usage: ' + cmdName + ' ' + this.usage() | ||
, '' | ||
]; | ||
var cmds = []; | ||
var commandHelp = this.commandHelp(); | ||
if (commandHelp) cmds = [commandHelp]; | ||
var options = [ | ||
' Options:' | ||
, '' | ||
, '' + this.optionHelp().replace(/^/gm, ' ') | ||
, '' | ||
, '' | ||
].join('\n'); | ||
]; | ||
return usage | ||
.concat(cmds) | ||
.concat(desc) | ||
.concat(options) | ||
.join('\n'); | ||
}; | ||
@@ -906,0 +941,0 @@ |
{ | ||
"name": "commander" | ||
, "version": "2.5.1" | ||
, "version": "2.6.0" | ||
, "description": "the complete solution for node.js command-line programs" | ||
@@ -5,0 +5,0 @@ , "keywords": ["command", "option", "parser", "prompt"] |
144
Readme.md
@@ -8,3 +8,3 @@ # Commander.js | ||
The complete solution for [node.js](http://nodejs.org) command-line interfaces, inspired by Ruby's [commander](https://github.com/tj/commander). | ||
API documentation: [http://tj.github.com/commander.js/](http://tj.github.com/commander.js/) | ||
[API documentation](http://tj.github.com/commander.js/) | ||
@@ -46,2 +46,46 @@ | ||
## Coercion | ||
```js | ||
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); | ||
``` | ||
## Variadic arguments | ||
@@ -105,4 +149,7 @@ | ||
An application for pizzas ordering | ||
Options: | ||
-h, --help output usage information | ||
-V, --version output the version number | ||
@@ -113,49 +160,6 @@ -p, --peppers Add peppers | ||
-c, --cheese <type> Add the specified type of cheese [marble] | ||
-h, --help output usage information | ||
-C, --no-cheese You do not want any cheese | ||
``` | ||
## Coercion | ||
```js | ||
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); | ||
``` | ||
## Custom help | ||
@@ -230,9 +234,49 @@ | ||
## Links | ||
## Examples | ||
- [ascii tables](https://github.com/LearnBoost/cli-table) | ||
- [progress bars](https://github.com/tj/node-progress) | ||
- [more progress bars](https://github.com/substack/node-multimeter) | ||
- [examples](https://github.com/tj/commander.js/tree/master/examples) | ||
```js | ||
var program = require('commander'); | ||
program | ||
.version('0.0.1') | ||
.option('-C, --chdir <path>', 'change the working directory') | ||
.option('-c, --config <path>', 'set config path. defaults to ./deploy.conf') | ||
.option('-T, --no-tests', 'ignore test hook') | ||
program | ||
.command('setup [env]') | ||
.description('run setup commands for all envs') | ||
.option("-s, --setup_mode [mode]", "Which setup mode to use") | ||
.action(function(env, options){ | ||
var mode = options.setup_mode || "normal"; | ||
env = env || 'all'; | ||
console.log('setup for %s env(s) with %s mode', env, mode); | ||
}); | ||
program | ||
.command('exec <cmd>') | ||
.alias('ex') | ||
.description('execute the given remote cmd') | ||
.option("-e, --exec_mode <mode>", "Which exec mode to use") | ||
.action(function(cmd, options){ | ||
console.log('exec "%s" using %s mode', cmd, options.exec_mode); | ||
}).on('--help', function() { | ||
console.log(' Examples:'); | ||
console.log(); | ||
console.log(' $ deploy exec sequential'); | ||
console.log(' $ deploy exec async'); | ||
console.log(); | ||
}); | ||
program | ||
.command('*') | ||
.action(function(env){ | ||
console.log('deploying "%s"', env); | ||
}); | ||
program.parse(process.argv); | ||
``` | ||
You can see more Demos in the [examples](https://github.com/tj/commander.js/tree/master/examples) directory. | ||
## License | ||
@@ -239,0 +283,0 @@ |
36647
4
879
301