@@ -325,8 +325,8 @@ function parse (args, opts) {
325
325
i = ii
326
326
argsToSet . push ( args [ ii ] )
327
327
}
328
- if ( multipleArrayFlag && ! configuration [ 'flatten-duplicate-arrays' ] ) {
329
- setArg ( key , argsToSet )
328
+ if ( multipleArrayFlag ) {
329
+ setArg ( key , argsToSet . map ( arg => processValue ( key , arg ) ) )
330
330
} else {
331
331
argsToSet . forEach ( function ( arg ) {
332
332
setArg ( key , arg )
@@ -339,33 +339,13 @@ function parse (args, opts) {
339
339
function setArg ( key , val ) {
340
340
unsetDefaulted ( key )
341
341
342
- // handle parsing boolean arguments --foo=true --bar false.
343
- if ( checkAllAliases ( key , flags . bools ) || checkAllAliases ( key , flags . counts ) ) {
344
- if ( typeof val === 'string' ) val = val === 'true'
345
- }
346
-
347
342
if ( / - / . test ( key ) && ! ( flags . aliases [ key ] && flags . aliases [ key ] . length ) && configuration [ 'camel-case-expansion' ] ) {
348
343
var c = camelCase ( key )
349
344
flags . aliases [ key ] = [ c ]
350
345
newAliases [ c ] = true
351
346
}
352
347
353
- var value = val
354
- if ( ! checkAllAliases ( key , flags . strings ) && ! checkAllAliases ( key , flags . coercions ) ) {
355
- if ( isNumber ( val ) ) value = Number ( val )
356
- if ( ! isUndefined ( val ) && ! isNumber ( val ) && checkAllAliases ( key , flags . numbers ) ) value = NaN
357
- }
358
-
359
- // increment a count given as arg (either no value or value parsed as boolean)
360
- if ( checkAllAliases ( key , flags . counts ) && ( isUndefined ( value ) || typeof value === 'boolean' ) ) {
361
- value = increment
362
- }
363
-
364
- // Set normalized value when key is in 'normalize' and in 'arrays'
365
- if ( checkAllAliases ( key , flags . normalize ) && checkAllAliases ( key , flags . arrays ) ) {
366
- if ( Array . isArray ( val ) ) value = val . map ( path . normalize )
367
- else value = path . normalize ( val )
368
- }
348
+ var value = processValue ( key , val )
369
349
370
350
var splitKey = key . split ( '.' )
371
351
setKey ( argv , splitKey , value )
@@ -407,6 +387,31 @@ function parse (args, opts) {
407
387
}
408
388
}
409
389
390
+ function processValue ( key , val ) {
391
+ // handle parsing boolean arguments --foo=true --bar false.
392
+ if ( checkAllAliases ( key , flags . bools ) || checkAllAliases ( key , flags . counts ) ) {
393
+ if ( typeof val === 'string' ) val = val === 'true'
394
+ }
395
+
396
+ var value = val
397
+ if ( ! checkAllAliases ( key , flags . strings ) && ! checkAllAliases ( key , flags . coercions ) ) {
398
+ if ( isNumber ( val ) ) value = Number ( val )
399
+ if ( ! isUndefined ( val ) && ! isNumber ( val ) && checkAllAliases ( key , flags . numbers ) ) value = NaN
400
+ }
401
+
402
+ // increment a count given as arg (either no value or value parsed as boolean)
403
+ if ( checkAllAliases ( key , flags . counts ) && ( isUndefined ( value ) || typeof value === 'boolean' ) ) {
404
+ value = increment
405
+ }
406
+
407
+ // Set normalized value when key is in 'normalize' and in 'arrays'
408
+ if ( checkAllAliases ( key , flags . normalize ) && checkAllAliases ( key , flags . arrays ) ) {
409
+ if ( Array . isArray ( val ) ) value = val . map ( path . normalize )
410
+ else value = path . normalize ( val )
411
+ }
412
+ return value
413
+ }
414
+
410
415
// set args from config.json file, this should be
411
416
// applied last so that defaults can be applied.
412
417
function setConfig ( argv ) {
@@ -551,14 +556,142 @@ function parse (args, opts) {
551
556
552
557
var key = keys [ keys . length - 1 ]
553
558
559
+ var isTypeArray = checkAllAliases ( key , flags . arrays )
560
+ var isOldValueArray = Array . isArray ( o [ key ] )
561
+ var isNewValueArray = Array . isArray ( value )
562
+ var duplicate = configuration [ 'duplicate-arguments-array' ]
563
+ var flatten = configuration [ 'flatten-duplicate-arrays' ]
564
+
554
565
if ( value === increment ) {
555
566
o [ key ] = increment ( o [ key ] )
556
567
} else if ( o [ key ] === undefined && checkAllAliases ( key , flags . arrays ) ) {
557
- o [ key ] = Array . isArray ( value ) && configuration [ 'flatten-duplicate-arrays' ] ? value : [ value ]
568
+ o [ key ] = Array . isArray ( value ) ? value : [ value ]
558
569
} else if ( o [ key ] === undefined || checkAllAliases ( key , flags . bools ) || checkAllAliases ( keys . join ( '.' ) , flags . bools ) || checkAllAliases ( key , flags . counts ) ) {
559
570
o [ key ] = value
560
571
} else if ( Array . isArray ( o [ key ] ) ) {
561
- o [ key ] . push ( value )
572
+ if ( ! duplicate && ! flatten ) {
573
+ if ( isTypeArray ) {
574
+ if ( isOldValueArray ) {
575
+ if ( isNewValueArray ) {
576
+ o [ key ] = value
577
+ } else {
578
+ o [ key ] = o [ key ] . concat ( [ value ] )
579
+ }
580
+ } else {
581
+ if ( isNewValueArray ) {
582
+ throw new Error ( 'this should not happen' )
583
+ } else {
584
+ throw new Error ( 'this should not happen' )
585
+ }
586
+ }
587
+ } else {
588
+ if ( isOldValueArray ) {
589
+ if ( isNewValueArray ) {
590
+ throw new Error ( 'this should not happen' )
591
+ } else {
592
+ throw new Error ( 'this should not happen' )
593
+ }
594
+ } else {
595
+ if ( isNewValueArray ) {
596
+ throw new Error ( 'this should not happen' )
597
+ } else {
598
+ o [ key ] = value
599
+ }
600
+ }
601
+ }
602
+ } else if ( ! duplicate && flatten ) {
603
+ if ( isTypeArray ) {
604
+ if ( isOldValueArray ) {
605
+ if ( isNewValueArray ) {
606
+ o [ key ] = value
607
+ } else {
608
+ o [ key ] = o [ key ] . concat ( [ value ] )
609
+ }
610
+ } else {
611
+ if ( isNewValueArray ) {
612
+ throw new Error ( 'this should not happen' )
613
+ } else {
614
+ throw new Error ( 'this should not happen' )
615
+ }
616
+ }
617
+ } else {
618
+ if ( isOldValueArray ) {
619
+ if ( isNewValueArray ) {
620
+ throw new Error ( 'this should not happen' )
621
+ } else {
622
+ o [ key ] = value
623
+ }
624
+ } else {
625
+ if ( isNewValueArray ) {
626
+ throw new Error ( 'this should not happen' )
627
+ } else {
628
+ o [ key ] = value
629
+ }
630
+ }
631
+ }
632
+ } else if ( duplicate && flatten ) {
633
+ if ( isTypeArray ) {
634
+ if ( isOldValueArray ) {
635
+ if ( isNewValueArray ) {
636
+ o [ key ] = o [ key ] . concat ( value )
637
+ } else {
638
+ o [ key ] = o [ key ] . concat ( [ value ] )
639
+ }
640
+ } else {
641
+ if ( isNewValueArray ) {
642
+ throw new Error ( 'this should not happen' )
643
+ } else {
644
+ throw new Error ( 'this should not happen' )
645
+ }
646
+ }
647
+ } else {
648
+ if ( isOldValueArray ) {
649
+ if ( isNewValueArray ) {
650
+ throw new Error ( 'this should not happen' )
651
+ } else {
652
+ o [ key ] = o [ key ] . concat ( [ value ] )
653
+ }
654
+ } else {
655
+ if ( isNewValueArray ) {
656
+ throw new Error ( 'this should not happen' )
657
+ } else {
658
+ o [ key ] = o [ key ] . concat ( [ value ] )
659
+ }
660
+ }
661
+ }
662
+ } else if ( duplicate && ! flatten ) {
663
+ if ( isTypeArray ) {
664
+ if ( isOldValueArray ) {
665
+ if ( isNewValueArray ) {
666
+ o [ key ] = [ o [ key ] ] . concat ( [ value ] )
667
+ } else {
668
+ o [ key ] = o [ key ] . concat ( [ value ] )
669
+ }
670
+ } else {
671
+ if ( isNewValueArray ) {
672
+ throw new Error ( 'this should not happen' )
673
+ } else {
674
+ throw new Error ( 'this should not happen' )
675
+ }
676
+ }
677
+ } else {
678
+ if ( isOldValueArray ) {
679
+ if ( isNewValueArray ) {
680
+ throw new Error ( 'this should not happen' )
681
+ } else {
682
+ o [ key ] = o [ key ] . concat ( [ value ] )
683
+ }
684
+ } else {
685
+ if ( isNewValueArray ) {
686
+ throw new Error ( 'this should not happen' )
687
+ } else {
688
+ o [ key ] = o [ key ] . concat ( [ value ] )
689
+ }
690
+ }
691
+ }
692
+ } else {
693
+ throw new Error ( 'this should not happen' )
694
+ }
562
695
} else if ( configuration [ 'duplicate-arguments-array' ] ) {
563
696
o [ key ] = [ o [ key ] , value ]
564
697
} else {
0 commit comments