@@ -29,7 +29,8 @@ class Command extends EventEmitter {
29
29
this . _allowUnknownOption = false ;
30
30
this . _allowExcessArguments = true ;
31
31
/** @type {Argument[] } */
32
- this . _args = [ ] ;
32
+ this . registeredArguments = [ ] ;
33
+ this . _args = this . registeredArguments ; // deprecated old name
33
34
/** @type {string[] } */
34
35
this . args = [ ] ; // cli args with options removed
35
36
this . rawArgs = [ ] ;
@@ -356,14 +357,14 @@ class Command extends EventEmitter {
356
357
* @return {Command } `this` command for chaining
357
358
*/
358
359
addArgument ( argument ) {
359
- const previousArgument = this . _args . slice ( - 1 ) [ 0 ] ;
360
+ const previousArgument = this . registeredArguments . slice ( - 1 ) [ 0 ] ;
360
361
if ( previousArgument && previousArgument . variadic ) {
361
362
throw new Error ( `only the last argument can be variadic '${ previousArgument . name ( ) } '` ) ;
362
363
}
363
364
if ( argument . required && argument . defaultValue !== undefined && argument . parseArg === undefined ) {
364
365
throw new Error ( `a default value for a required argument is never used: '${ argument . name ( ) } '` ) ;
365
366
}
366
- this . _args . push ( argument ) ;
367
+ this . registeredArguments . push ( argument ) ;
367
368
return this ;
368
369
}
369
370
@@ -483,7 +484,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
483
484
action ( fn ) {
484
485
const listener = ( args ) => {
485
486
// The .action callback takes an extra parameter which is the command or options.
486
- const expectedArgsCount = this . _args . length ;
487
+ const expectedArgsCount = this . registeredArguments . length ;
487
488
const actionArgs = args . slice ( 0 , expectedArgsCount ) ;
488
489
if ( this . _storeOptionsAsProperties ) {
489
490
actionArgs [ expectedArgsCount ] = this ; // backwards compatible "options"
@@ -1109,29 +1110,29 @@ Expecting one of '${allowedValues.join("', '")}'`);
1109
1110
}
1110
1111
1111
1112
/**
1112
- * Check this.args against expected this._args .
1113
+ * Check this.args against expected this.registeredArguments .
1113
1114
*
1114
1115
* @api private
1115
1116
*/
1116
1117
1117
1118
_checkNumberOfArguments ( ) {
1118
1119
// too few
1119
- this . _args . forEach ( ( arg , i ) => {
1120
+ this . registeredArguments . forEach ( ( arg , i ) => {
1120
1121
if ( arg . required && this . args [ i ] == null ) {
1121
1122
this . missingArgument ( arg . name ( ) ) ;
1122
1123
}
1123
1124
} ) ;
1124
1125
// too many
1125
- if ( this . _args . length > 0 && this . _args [ this . _args . length - 1 ] . variadic ) {
1126
+ if ( this . registeredArguments . length > 0 && this . registeredArguments [ this . registeredArguments . length - 1 ] . variadic ) {
1126
1127
return ;
1127
1128
}
1128
- if ( this . args . length > this . _args . length ) {
1129
+ if ( this . args . length > this . registeredArguments . length ) {
1129
1130
this . _excessArguments ( this . args ) ;
1130
1131
}
1131
1132
}
1132
1133
1133
1134
/**
1134
- * Process this.args using this._args and save as this.processedArgs!
1135
+ * Process this.args using this.registeredArguments and save as this.processedArgs!
1135
1136
*
1136
1137
* @api private
1137
1138
*/
@@ -1150,7 +1151,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
1150
1151
this . _checkNumberOfArguments ( ) ;
1151
1152
1152
1153
const processedArgs = [ ] ;
1153
- this . _args . forEach ( ( declaredArg , index ) => {
1154
+ this . registeredArguments . forEach ( ( declaredArg , index ) => {
1154
1155
let value = declaredArg . defaultValue ;
1155
1156
if ( declaredArg . variadic ) {
1156
1157
// Collect together remaining arguments for passing together as an array.
@@ -1765,7 +1766,7 @@ Expecting one of '${allowedValues.join("', '")}'`);
1765
1766
_excessArguments ( receivedArgs ) {
1766
1767
if ( this . _allowExcessArguments ) return ;
1767
1768
1768
- const expected = this . _args . length ;
1769
+ const expected = this . registeredArguments . length ;
1769
1770
const s = ( expected === 1 ) ? '' : 's' ;
1770
1771
const forSubcommand = this . parent ? ` for '${ this . name ( ) } '` : '' ;
1771
1772
const message = `error: too many arguments${ forSubcommand } . Expected ${ expected } argument${ s } but got ${ receivedArgs . length } .` ;
@@ -1905,13 +1906,13 @@ Expecting one of '${allowedValues.join("', '")}'`);
1905
1906
if ( str === undefined ) {
1906
1907
if ( this . _usage ) return this . _usage ;
1907
1908
1908
- const args = this . _args . map ( ( arg ) => {
1909
+ const args = this . registeredArguments . map ( ( arg ) => {
1909
1910
return humanReadableArgName ( arg ) ;
1910
1911
} ) ;
1911
1912
return [ ] . concat (
1912
1913
( this . options . length || this . _hasHelpOption ? '[options]' : [ ] ) ,
1913
1914
( this . commands . length ? '[command]' : [ ] ) ,
1914
- ( this . _args . length ? args : [ ] )
1915
+ ( this . registeredArguments . length ? args : [ ] )
1915
1916
) . join ( ' ' ) ;
1916
1917
}
1917
1918
0 commit comments