11import { EventEmitter } from 'events'
22import path from 'path'
3- import mri , { Options as MriOpts } from 'mri'
3+ import mri from 'mri'
44import Command , {
55 GlobalCommand ,
66 CommandConfig ,
@@ -165,8 +165,7 @@ class CAC extends EventEmitter {
165165
166166 // Search sub-commands
167167 for ( const command of this . commands ) {
168- const mriOptions = getMriOptions ( this . globalCommand , command )
169- const mriResult = this . mri ( argv . slice ( 2 ) , mriOptions )
168+ const mriResult = this . mri ( argv . slice ( 2 ) , command )
170169
171170 const commandName = mriResult . args [ 0 ]
172171 if ( command . isMatched ( commandName ) ) {
@@ -185,17 +184,15 @@ class CAC extends EventEmitter {
185184 for ( const command of this . commands ) {
186185 if ( command . name === '' ) {
187186 shouldParse = false
188- const mriOptions = getMriOptions ( this . globalCommand , command )
189- const mriResult = this . mri ( argv . slice ( 2 ) , mriOptions )
187+ const mriResult = this . mri ( argv . slice ( 2 ) , command )
190188 this . setParsedInfo ( mriResult , command )
191189 this . emit ( `command:!` , command )
192190 }
193191 }
194192 }
195193
196194 if ( shouldParse ) {
197- const globalMriOptions = getMriOptions ( this . globalCommand )
198- const mriResult = this . mri ( argv . slice ( 2 ) , globalMriOptions )
195+ const mriResult = this . mri ( argv . slice ( 2 ) )
199196 this . setParsedInfo ( mriResult )
200197 }
201198
@@ -224,13 +221,25 @@ class CAC extends EventEmitter {
224221 return parsedArgv
225222 }
226223
227- private mri ( argv : string [ ] , mriOptions : MriOpts ) : MriResult {
224+ private mri (
225+ argv : string [ ] ,
226+ /** Matched command */ command ?: Command
227+ ) : MriResult {
228+ // All added options
229+ const cliOptions = [
230+ ...this . globalCommand . options ,
231+ ...( command ? command . options : [ ] )
232+ ]
233+ const mriOptions = getMriOptions ( cliOptions )
234+
235+ // Extract everything after `--` since mri doesn't support it
228236 let argsAfterDoubleDashes : string [ ] = [ ]
229237 const doubleDashesIndex = argv . indexOf ( '--' )
230238 if ( doubleDashesIndex > - 1 ) {
231239 argsAfterDoubleDashes = argv . slice ( 0 , doubleDashesIndex )
232240 argv = argv . slice ( doubleDashesIndex + 1 )
233241 }
242+
234243 const parsed = mri ( argv , mriOptions )
235244
236245 const args = parsed . _
@@ -239,6 +248,24 @@ class CAC extends EventEmitter {
239248 const options : { [ k : string ] : any } = {
240249 '--' : argsAfterDoubleDashes
241250 }
251+
252+ // Set option default value
253+ const ignoreDefault =
254+ command && command . config . ignoreOptionDefaultValue
255+ ? command . config . ignoreOptionDefaultValue
256+ : this . globalCommand . config . ignoreOptionDefaultValue
257+
258+ if ( ! ignoreDefault ) {
259+ for ( const cliOption of cliOptions ) {
260+ if ( cliOption . config . default !== undefined ) {
261+ for ( const name of cliOption . names ) {
262+ options [ name ] = cliOption . config . default
263+ }
264+ }
265+ }
266+ }
267+
268+ // Camelcase option names and set dot nested option values
242269 for ( const key of Object . keys ( parsed ) ) {
243270 const keys = key . split ( '.' ) . map ( ( v , i ) => {
244271 return i === 0 ? camelcase ( v ) : v
0 commit comments