@@ -270,9 +270,7 @@ function parse (args, opts) {
270270 }
271271 }
272272 } else {
273- argv . _ . push (
274- flags . strings [ '_' ] || ! isNumber ( arg ) ? arg : Number ( arg )
275- )
273+ argv . _ . push ( maybeCoerceNumber ( '_' , arg ) )
276274 }
277275 }
278276
@@ -348,10 +346,8 @@ function parse (args, opts) {
348346 function setArg ( key , val ) {
349347 unsetDefaulted ( key )
350348
351- if ( / - / . test ( key ) && ! ( flags . aliases [ key ] && flags . aliases [ key ] . length ) && configuration [ 'camel-case-expansion' ] ) {
352- var c = camelCase ( key )
353- flags . aliases [ key ] = [ c ]
354- newAliases [ c ] = true
349+ if ( / - / . test ( key ) && configuration [ 'camel-case-expansion' ] ) {
350+ addNewAlias ( key , camelCase ( key ) )
355351 }
356352
357353 var value = processValue ( key , val )
@@ -396,17 +392,23 @@ function parse (args, opts) {
396392 }
397393 }
398394
395+ function addNewAlias ( key , alias ) {
396+ if ( ! ( flags . aliases [ key ] && flags . aliases [ key ] . length ) ) {
397+ flags . aliases [ key ] = [ alias ]
398+ newAliases [ alias ] = true
399+ }
400+ if ( ! ( flags . aliases [ alias ] && flags . aliases [ alias ] . length ) ) {
401+ addNewAlias ( alias , key )
402+ }
403+ }
404+
399405 function processValue ( key , val ) {
400406 // handle parsing boolean arguments --foo=true --bar false.
401407 if ( checkAllAliases ( key , flags . bools ) || checkAllAliases ( key , flags . counts ) ) {
402408 if ( typeof val === 'string' ) val = val === 'true'
403409 }
404410
405- var value = val
406- if ( ! checkAllAliases ( key , flags . strings ) && ! checkAllAliases ( key , flags . coercions ) ) {
407- if ( isNumber ( val ) ) value = Number ( val )
408- if ( ! isUndefined ( val ) && ! isNumber ( val ) && checkAllAliases ( key , flags . numbers ) ) value = NaN
409- }
411+ var value = maybeCoerceNumber ( key , val )
410412
411413 // increment a count given as arg (either no value or value parsed as boolean)
412414 if ( checkAllAliases ( key , flags . counts ) && ( isUndefined ( value ) || typeof value === 'boolean' ) ) {
@@ -421,6 +423,14 @@ function parse (args, opts) {
421423 return value
422424 }
423425
426+ function maybeCoerceNumber ( key , value ) {
427+ if ( ! checkAllAliases ( key , flags . strings ) && ! checkAllAliases ( key , flags . coercions ) ) {
428+ const shouldCoerceNumber = isNumber ( value ) && configuration [ 'parse-numbers' ] && ( Number . isSafeInteger ( parseInt ( value ) ) )
429+ if ( shouldCoerceNumber || ( ! isUndefined ( value ) && checkAllAliases ( key , flags . numbers ) ) ) value = Number ( value )
430+ }
431+ return value
432+ }
433+
424434 // set args from config.json file, this should be
425435 // applied last so that defaults can be applied.
426436 function setConfig ( argv ) {
@@ -602,7 +612,9 @@ function parse (args, opts) {
602612 flags . aliases [ key ] . concat ( key ) . forEach ( function ( x ) {
603613 if ( / - / . test ( x ) && configuration [ 'camel-case-expansion' ] ) {
604614 var c = camelCase ( x )
605- flags . aliases [ key ] . push ( c )
615+ if ( flags . aliases [ key ] . indexOf ( c ) === - 1 ) {
616+ flags . aliases [ key ] . push ( c )
617+ }
606618 newAliases [ c ] = true
607619 }
608620 } )
@@ -664,7 +676,6 @@ function parse (args, opts) {
664676 }
665677
666678 function isNumber ( x ) {
667- if ( ! configuration [ 'parse-numbers' ] ) return false
668679 if ( typeof x === 'number' ) return true
669680 if ( / ^ 0 x [ 0 - 9 a - f ] + $ / i. test ( x ) ) return true
670681 return / ^ [ - + ] ? (?: \d + (?: \. \d * ) ? | \. \d + ) ( e [ - + ] ? \d + ) ? $ / . test ( x )
0 commit comments