commander
Advanced tools
Comparing version 0.4.3 to 0.5.0
0.5.0 / 2011-12-04 | ||
================== | ||
* Added sub-command option support [itay] | ||
0.4.3 / 2011-12-04 | ||
@@ -3,0 +8,0 @@ ================== |
@@ -202,3 +202,17 @@ | ||
var self = this; | ||
this.parent.on(this.name, function(args){ | ||
this.parent.on(this.name, function(args, unknown){ | ||
// Parse any so-far unknown options | ||
unknown = unknown || []; | ||
var parsed = self.parseOptions(unknown); | ||
// Output help if necessary | ||
outputHelpIfNecessary(self, parsed.unknown); | ||
// If there are still any unknown options, then we simply | ||
// die, unless someone asked for help, in which case we give it | ||
// to them, and then we die. | ||
if (parsed.unknown.length > 0) { | ||
self.unknownOption(parsed.unknown[0]); | ||
} | ||
self.args.forEach(function(arg, i){ | ||
@@ -209,2 +223,12 @@ if (arg.required && null == args[i]) { | ||
}); | ||
// Always append ourselves to the end of the arguments, | ||
// to make sure we match the number of arguments the user | ||
// expects | ||
if (self.args.length) { | ||
args[self.args.length] = self; | ||
} else { | ||
args.push(self); | ||
} | ||
fn.apply(this, args); | ||
@@ -325,4 +349,5 @@ }); | ||
// process argv | ||
this.args = this.parseOptions(this.normalize(argv)); | ||
return this.parseArgs(this.args); | ||
var parsed = this.parseOptions(this.normalize(argv.slice(2))); | ||
this.args = parsed.args; | ||
return this.parseArgs(this.args, parsed.unknown); | ||
}; | ||
@@ -369,3 +394,3 @@ | ||
Command.prototype.parseArgs = function(args){ | ||
Command.prototype.parseArgs = function(args, unknown){ | ||
var cmds = this.commands | ||
@@ -378,6 +403,14 @@ , len = cmds.length | ||
if (this.listeners(name).length) { | ||
this.emit(args.shift(), args); | ||
this.emit(args.shift(), args, unknown); | ||
} else { | ||
this.emit('*', args); | ||
} | ||
} else { | ||
outputHelpIfNecessary(this, unknown); | ||
// If there were no args and we have unknown options, | ||
// then they are extraneous and we need to error. | ||
if (unknown.length > 0) { | ||
this.unknownOption(unknown[0]); | ||
} | ||
} | ||
@@ -415,3 +448,2 @@ | ||
var args = [] | ||
, argv = argv.slice(2) | ||
, len = argv.length | ||
@@ -422,2 +454,4 @@ , literal | ||
var unknownOptions = []; | ||
// parse options | ||
@@ -467,3 +501,11 @@ for (var i = 0; i < len; ++i) { | ||
if (arg.length > 1 && '-' == arg[0]) { | ||
this.unknownOption(arg); | ||
unknownOptions.push(arg); | ||
// If the next argument looks like it might be | ||
// an argument for this option, we pass it on. | ||
// If it isn't, then it'll simply be ignored | ||
if (argv[i+1] && '-' != argv[i+1][0]) { | ||
unknownOptions.push(argv[++i]); | ||
} | ||
continue; | ||
} | ||
@@ -474,4 +516,4 @@ | ||
} | ||
return args; | ||
return { args: args, unknown: unknownOptions }; | ||
}; | ||
@@ -573,7 +615,15 @@ | ||
Command.prototype.usage = function(str){ | ||
var args = this.args.map(function(arg){ | ||
return arg.required | ||
? '<' + arg.name + '>' | ||
: '[' + arg.name + ']'; | ||
}); | ||
var usage = '[options' | ||
+ (this.commands.length ? '] [command' : '') | ||
+ ']'; | ||
+ ']' | ||
+ (this.args.length ? ' ' + args : ''); | ||
if (0 == arguments.length) return this._usage || usage; | ||
this._usage = str; | ||
return this; | ||
@@ -604,6 +654,10 @@ }; | ||
var width = this.largestOptionLength(); | ||
return this.options.map(function(option){ | ||
return pad(option.flags, width) | ||
+ ' ' + option.description; | ||
}).join('\n'); | ||
// Prepend the help information | ||
return [pad('-h, --help', width) + ' ' + 'output usage information'] | ||
.concat(this.options.map(function(option){ | ||
return pad(option.flags, width) | ||
+ ' ' + option.description; | ||
})) | ||
.join('\n'); | ||
}; | ||
@@ -630,3 +684,7 @@ | ||
}).join(' '); | ||
return cmd.name + ' ' + args | ||
return cmd.name | ||
+ (cmd.options.length | ||
? ' [options]' | ||
: '') + ' ' + args | ||
+ (cmd.description() | ||
@@ -928,10 +986,18 @@ ? '\n' + cmd.description() | ||
/** | ||
* Default -h, --help option. | ||
* Output help information if necessary | ||
* | ||
* @param {Command} command to output help for | ||
* @param {Array} array of options to search for -h or --help | ||
* @api private | ||
*/ | ||
exports.option('-h, --help', 'output usage information'); | ||
exports.on('help', function(){ | ||
process.stdout.write(this.helpInformation()); | ||
exports.emit('--help'); | ||
process.exit(0); | ||
}); | ||
function outputHelpIfNecessary(cmd, options) { | ||
options = options || []; | ||
for (var i = 0; i < options.length; i++) { | ||
if (options[i] == '--help' || options[i] == '-h') { | ||
process.stdout.write(cmd.helpInformation()); | ||
cmd.emit('--help'); | ||
process.exit(0); | ||
} | ||
} | ||
} |
{ | ||
"name": "commander" | ||
, "version": "0.4.3" | ||
, "version": "0.5.0" | ||
, "description": "the complete solution for node.js command-line programs" | ||
@@ -5,0 +5,0 @@ , "keywords": ["command", "option", "parser", "prompt", "stdin"] |
Sorry, the diff of this file is not supported yet
46340
34
1140