Skip to content
Myr

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

3 files+72 added−20 removed

modified:examples/deploy+5 added
26 unmodified lines
27 .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 });3738program39 .command('*')
5 unmodified lines
modified:lib/commander.js+53 added-18 removed
199 unmodified lines
200Command.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 lines
222 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 lines
397 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 lines
604 * @api public605 */606607Command.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 lines
640 */641642Command.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};653654/**655 * Return command help documentation.
13 unmodified lines
669 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 lines
968 return str + Array(len + 1).join(' ');969}970971/**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 */978950exports.option('-h, --help', 'output usage information');951exports.on('help', function(){952 exports.emit('--help');953 process.stdout.write(this.helpInformation());954 process.exit(0);955});979function 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