Skip to content
Myr

fix .usage() when args are present. Closes #106

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

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 2a21655 · Browse files · 2 files+11 added−10 removed
This commit

State

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

Diff stats

2 files+11 added−10 removed

  • other:+11 added−10 removed

2 files+11 added−10 removed

modified:examples/custom-help+1 added-1 removed
27 unmodified lines
28});2930program.parse(process.argv);3132console.log('stuff');32program.help();
modified:index.js+10 added-9 removed
89 unmodified lines
9091function Command(name) {92 this.commands = [];93 this.options = [];94 this.args = [];94 this._args = [];95 this._name = name;96}9798/**
89 unmodified lines
188 var self = this;189 args.forEach(function(arg){190 switch (arg[0]) {191 case '<':192 self.args.push({ required: true, name: arg.slice(1, -1) });192 self._args.push({ required: true, name: arg.slice(1, -1) });193 break;194 case '[':195 self.args.push({ required: false, name: arg.slice(1, -1) });195 self._args.push({ required: false, name: arg.slice(1, -1) });196 break;197 }198 });199 return this;
35 unmodified lines
235 236 // Leftover arguments need to be pushed back. Fixes issue #56237 if (parsed.args.length) args = parsed.args.concat(args);238 239 self.args.forEach(function(arg, i){239 self._args.forEach(function(arg, i){240 if (arg.required && null == args[i]) {241 self.missingArgument(arg.name);242 }243 });244 245 // Always append ourselves to the end of the arguments,246 // to make sure we match the number of arguments the user247 // expects248 if (self.args.length) {249 args[self.args.length] = self;248 if (self._args.length) {249 args[self._args.length] = self;250 } else {251 args.push(self);252 }253
421 unmodified lines
675 * @api public676 */677678Command.prototype.usage = function(str){679 var args = this.args.map(function(arg){679 var args = this._args.map(function(arg){680 return arg.required681 ? '<' + arg.name + '>'682 : '[' + arg.name + ']';683 });684685 var usage = '[options'686 + (this.commands.length ? '] [command' : '')687 + ']'688 + (this.args.length ? ' ' + args : '');688 + (this._args.length ? ' ' + args : '');689690 if (0 == arguments.length) return this._usage || usage;691 this._usage = str;692693 return this;
44 unmodified lines
738 ''739 , ' Commands:'740 , ''741 , this.commands.map(function(cmd){741 var args = cmd.args.map(function(arg){742 var args = cmd._args.map(function(arg){743 return arg.required744 ? '<' + arg.name + '>'745 : '[' + arg.name + ']';746 }).join(' ');
385 unmodified lines