@@ -115,6 +115,7 @@ class CAC extends EventEmitter {
115115 this . rawArgs = argv
116116 this . bin = argv [ 1 ] ? path . basename ( argv [ 1 ] ) : 'cli'
117117
118+ // Search sub-commands
118119 for ( const command of this . commands ) {
119120 const minimistOptions = getMinimistOptions ( [
120121 ...this . globalCommand . options ,
@@ -124,21 +125,22 @@ class CAC extends EventEmitter {
124125 argv . slice ( 2 ) ,
125126 minimistOptions
126127 )
127- if ( command . isMatched ( args [ 0 ] ) ) {
128+ const commandName = args [ 0 ]
129+ if ( command . isMatched ( commandName ) ) {
128130 this . matchedCommand = command
129- this . args = args
131+ this . args = args . slice ( 1 )
130132 this . options = options
131- this . emit ( `command:${ args [ 0 ] } ` , command )
133+ this . emit ( `command:${ commandName } ` , command )
132134 this . runCommandAction ( command , this . globalCommand , {
133- args,
135+ args : this . args ,
134136 options,
135137 originalOptions
136138 } )
137- return { args, options }
139+ return { args : this . args , options }
138140 }
139141 }
140142
141- // Try the default command
143+ // Search the default command
142144 for ( const command of this . commands ) {
143145 if ( command . name === '' ) {
144146 const minimistOptions = getMinimistOptions ( [
@@ -233,16 +235,11 @@ class CAC extends EventEmitter {
233235
234236 command . checkUnknownOptions ( originalOptions , globalCommand )
235237
236- // The first one is command name
237- if ( ! command . isDefaultCommand ) {
238- args = args . slice ( 1 )
239- }
240-
241238 const minimalArgsCount = command . args . filter ( arg => arg . required ) . length
242239
243- if ( args . length < minimalArgsCount ) {
240+ if ( command . args . length < minimalArgsCount ) {
244241 console . error (
245- `error: missing required args for command " ${ command . rawName } " `
242+ `error: missing required args for command \` ${ command . rawName } \` `
246243 )
247244 process . exit ( 1 )
248245 }
0 commit comments