Add help for subcommands (should resolve issue #2)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Itay Neeman · committed by Itay Neeman · parent b2f4dad · Browse files · 3 files·+72 added−20 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+72 added−20 removed
- tests:+14 added−2 removed
- other:+58 added−18 removed
Provenance
- Commit
4788f70- Tree
3511145- Author
Itay Neeman <itay@neeman.net>- Committer
Itay Neeman <itay@neeman.net>- Parent
b2f4dad
3 files changed+72 added−20 removed
modified:examples/deploy+5 added
26 unmodified lines27 .description('execute the given remote cmd')28 .option("-e, --exec_mode <mode>", "Which exec mode to use")29 .action(function(cmd, options){30 console.log('exec "%s" using %s mode', cmd, options.exec_mode);31+ }).on('--help', function() {32+ console.log(' Examples:');33+ console.log('');34+ console.log(' $ deploy exec sequential');35+ console.log(' $ deploy exec async');36 });37 38 program39 .command('*')5 unmodified linesmodified:lib/commander.js+53 added-18 removed
199 unmodified lines200 Command.prototype.action = function(fn){201 var self = this;202 this.parent.on(this.name, function(args, unknown){ 203 // Parse any so-far unknown options204+ unknown = unknown || [];205 var parsed = self.parseOptions(unknown);206 207+ // Output help if necessary208+ outputHelpIfNecessary(self, parsed.unknown);209+ 210 // If there are still any unknown options, then we simply 211+ // die, unless someone asked for help, in which case we give it207- // die.208- if (parsed.unknown.length > 0) {212+ // to them, and then we die.213+ if (parsed.unknown.length > 0) { 214 self.unknownOption(parsed.unknown[0]);215 }216 217 self.args.forEach(function(arg, i){4 unmodified lines222 223 // Always append ourselves to the end of the arguments,224 // to make sure we match the number of arguments the user225 // expects226+ if (self.args.length) {221- args[self.args.length] = self;227+ args[self.args.length] = self;228+ }229+ else {230+ args.push(self);231+ }232 233 fn.apply(this, args);234 });235 return this;161 unmodified lines397 if (args.length) {398 name = args[0];399 if (this.listeners(name).length) {400 this.emit(args.shift(), args, unknown);391- } else { 401+ } else {402 this.emit('*', args);403 }404 }405 else {406+ outputHelpIfNecessary(this, unknown);407+ 408 // If there were no args and we have unknown options,409 // then they are extraneous and we need to error.398- if (unknown.length > 0) {410+ if (unknown.length > 0) { 411 this.unknownOption(unknown[0]);412 }413 }414 189 unmodified lines604 * @api public605 */606 607 Command.prototype.usage = function(str){608+ var args = this.args.map(function(arg){609+ return arg.required610+ ? '<' + arg.name + '>'611+ : '[' + arg.name + ']';612+ });613 var usage = '[options'614 + (this.commands.length ? '] [command' : '')598- + ']';615+ + ']'616+ + (this.args.length ? ' ' + args : '');617 if (0 == arguments.length) return this._usage || usage;618 this._usage = str;619 return this;620 };19 unmodified lines640 */641 642 Command.prototype.optionHelp = function(){643 var width = this.largestOptionLength();644+ 645+ // Prepend the help information646+ return [pad('-h, --help', width) + ' ' + 'output usage information']626- return this.options.map(function(option){627- return pad(option.flags, width)628- + ' ' + option.description;629- }).join('\n');647+ .concat(this.options.map(function(option){648+ return pad(option.flags, width)649+ + ' ' + option.description;650+ }))651+ .join('\n');652 };653 654 /**655 * Return command help documentation.13 unmodified lines669 return arg.required670 ? '<' + arg.name + '>'671 : '[' + arg.name + ']';672 }).join(' ');651- return cmd.name + ' ' + args673+ return cmd.name 674+ + (cmd.options.length 675+ ? ' [options]'676+ : '') + ' ' + args677 + (cmd.description()678 ? '\n' + cmd.description()679 : '');680 }).join('\n\n').replace(/^/gm, ' ')287 unmodified lines968 return str + Array(len + 1).join(' ');969 }970 971 /**947- * Default -h, --help option.972+ * Output help information if necessary973+ *974+ * @param {Command} command to output help for975+ * @param {Array} array of options to search for -h or --help976+ * @api private977 */978 950-exports.option('-h, --help', 'output usage information');951-exports.on('help', function(){952- exports.emit('--help');953- process.stdout.write(this.helpInformation());954- process.exit(0);955-});979+function outputHelpIfNecessary(cmd, options) {980+ options = options || [];981+ 982+ for(var i = 0; i < options.length; i++) {983+ if (options[i] === '--help' || options[i] === '-h') {984+ cmd.emit('--help');985+ process.stdout.write(cmd.helpInformation());986+ process.exit(0);987+ }988+ }989+ 990+}modified:test/test.options.commands.js+14 added-2 removed
11 unmodified lines12131415161718192012 unmodified lines3334353637383940414244 unmodified lines87888990919293949596979899100101102103104105106961071089810911011 unmodified lines .option('-T, --no-tests', 'ignore test hook')
var envValue = "";var cmdValue = "";var customHelp = false;
program .command('setup [env]') .description('run setup commands for all envs')12 unmodified lines .option("-e, --exec_mode <mode>", "Which exec mode to use") .option("-t, --target [target]", "Target to use") .action(function(cmd, options){ cmdValue = cmd; }).on("--help", function() { customHelp = true; });
program .command('*')44 unmodified lines
// Make sure we still catch errors with required values for optionsvar exceptionOccurred = false;var oldProcessExit = process.exit;var oldConsoleError = console.error;process.exit = function() { exceptionOccurred = true; throw new Error(); };console.error = function() {};
try { program.parse(['node', 'test', '--config', 'conf6', 'exec', '--help']);} catch(ex) { program.config.should.equal("conf6");}
try { program.parse(['node', 'test', '--config', 'conf', 'exec', '-t', 'target1', 'exec1', '-e']);}catch(ex) {}
exceptionOccurred.should.be.true;process.exit = oldProcessExit;
process.exit = oldProcessExit;No newline at end of fileexceptionOccurred.should.be.true;customHelp.should.be.true;