Skip to content
This repository
Browse code

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

  • Loading branch information...
commit e52a3216ce8fdb8ae0411e48a9828dc243c3f90f 1 parent 2a21655
TJ Holowaychuk authored November 20, 2012
2  examples/custom-help
@@ -29,4 +29,4 @@ program.on('--help', function(){
29 29
 
30 30
 program.parse(process.argv);
31 31
 
32  
-console.log('stuff');
  32
+program.help();
19  index.js
@@ -91,7 +91,7 @@ Option.prototype.is = function(arg){
91 91
 function Command(name) {
92 92
   this.commands = [];
93 93
   this.options = [];
94  
-  this.args = [];
  94
+  this._args = [];
95 95
   this._name = name;
96 96
 }
97 97
 
@@ -189,10 +189,10 @@ Command.prototype.parseExpectedArgs = function(args){
189 189
   args.forEach(function(arg){
190 190
     switch (arg[0]) {
191 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 193
         break;
194 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 196
         break;
197 197
     }
198 198
   });
@@ -236,7 +236,7 @@ Command.prototype.action = function(fn){
236 236
     // Leftover arguments need to be pushed back. Fixes issue #56
237 237
     if (parsed.args.length) args = parsed.args.concat(args);
238 238
     
239  
-    self.args.forEach(function(arg, i){
  239
+    self._args.forEach(function(arg, i){
240 240
       if (arg.required && null == args[i]) {
241 241
         self.missingArgument(arg.name);
242 242
       }
@@ -245,8 +245,8 @@ Command.prototype.action = function(fn){
245 245
     // Always append ourselves to the end of the arguments,
246 246
     // to make sure we match the number of arguments the user
247 247
     // expects
248  
-    if (self.args.length) {
249  
-      args[self.args.length] = self;
  248
+    if (self._args.length) {
  249
+      args[self._args.length] = self;
250 250
     } else {
251 251
       args.push(self);
252 252
     }
@@ -676,7 +676,7 @@ Command.prototype.description = function(str){
676 676
  */
677 677
 
678 678
 Command.prototype.usage = function(str){
679  
-  var args = this.args.map(function(arg){
  679
+  var args = this._args.map(function(arg){
680 680
     return arg.required
681 681
       ? '<' + arg.name + '>'
682 682
       : '[' + arg.name + ']';
@@ -685,7 +685,8 @@ Command.prototype.usage = function(str){
685 685
   var usage = '[options'
686 686
     + (this.commands.length ? '] [command' : '')
687 687
     + ']'
688  
-    + (this.args.length ? ' ' + args : '');
  688
+    + (this._args.length ? ' ' + args : '');
  689
+
689 690
   if (0 == arguments.length) return this._usage || usage;
690 691
   this._usage = str;
691 692
 
@@ -738,7 +739,7 @@ Command.prototype.commandHelp = function(){
738 739
     , '  Commands:'
739 740
     , ''
740 741
     , this.commands.map(function(cmd){
741  
-      var args = cmd.args.map(function(arg){
  742
+      var args = cmd._args.map(function(arg){
742 743
         return arg.required
743 744
           ? '<' + arg.name + '>'
744 745
           : '[' + arg.name + ']';

0 notes on commit e52a321

Please sign in to comment.
Something went wrong with that request. Please try again.