@@ -270,9 +270,7 @@ function parse (args, opts) {
270
270
}
271
271
}
272
272
} else {
273
- argv . _ . push (
274
- flags . strings [ '_' ] || ! isNumber ( arg ) ? arg : Number ( arg )
275
- )
273
+ argv . _ . push ( maybeCoerceNumber ( '_' , arg ) )
276
274
}
277
275
}
278
276
@@ -348,10 +346,8 @@ function parse (args, opts) {
348
346
function setArg ( key , val ) {
349
347
unsetDefaulted ( key )
350
348
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 ) )
355
351
}
356
352
357
353
var value = processValue ( key , val )
@@ -396,17 +392,23 @@ function parse (args, opts) {
396
392
}
397
393
}
398
394
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
+
399
405
function processValue ( key , val ) {
400
406
// handle parsing boolean arguments --foo=true --bar false.
401
407
if ( checkAllAliases ( key , flags . bools ) || checkAllAliases ( key , flags . counts ) ) {
402
408
if ( typeof val === 'string' ) val = val === 'true'
403
409
}
404
410
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 )
410
412
411
413
// increment a count given as arg (either no value or value parsed as boolean)
412
414
if ( checkAllAliases ( key , flags . counts ) && ( isUndefined ( value ) || typeof value === 'boolean' ) ) {
@@ -421,6 +423,14 @@ function parse (args, opts) {
421
423
return value
422
424
}
423
425
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
+
424
434
// set args from config.json file, this should be
425
435
// applied last so that defaults can be applied.
426
436
function setConfig ( argv ) {
@@ -602,7 +612,9 @@ function parse (args, opts) {
602
612
flags . aliases [ key ] . concat ( key ) . forEach ( function ( x ) {
603
613
if ( / - / . test ( x ) && configuration [ 'camel-case-expansion' ] ) {
604
614
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
+ }
606
618
newAliases [ c ] = true
607
619
}
608
620
} )
@@ -664,7 +676,6 @@ function parse (args, opts) {
664
676
}
665
677
666
678
function isNumber ( x ) {
667
- if ( ! configuration [ 'parse-numbers' ] ) return false
668
679
if ( typeof x === 'number' ) return true
669
680
if ( / ^ 0 x [ 0 - 9 a - f ] + $ / i. test ( x ) ) return true
670
681
return / ^ [ - + ] ? (?: \d + (?: \. \d * ) ? | \. \d + ) ( e [ - + ] ? \d + ) ? $ / . test ( x )
0 commit comments