Skip to content

Commit e5c6ee2

Browse files
committed
fix: flatten/duplicate regression
1 parent 941d8f3 commit e5c6ee2

File tree

2 files changed

+341
-27
lines changed

2 files changed

+341
-27
lines changed

index.js

Lines changed: 158 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,8 @@ function parse (args, opts) {
325325
i = ii
326326
argsToSet.push(args[ii])
327327
}
328-
if (multipleArrayFlag && !configuration['flatten-duplicate-arrays']) {
329-
setArg(key, argsToSet)
328+
if (multipleArrayFlag) {
329+
setArg(key, argsToSet.map(arg => processValue(key, arg)))
330330
} else {
331331
argsToSet.forEach(function (arg) {
332332
setArg(key, arg)
@@ -339,33 +339,13 @@ function parse (args, opts) {
339339
function setArg (key, val) {
340340
unsetDefaulted(key)
341341

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-
347342
if (/-/.test(key) && !(flags.aliases[key] && flags.aliases[key].length) && configuration['camel-case-expansion']) {
348343
var c = camelCase(key)
349344
flags.aliases[key] = [c]
350345
newAliases[c] = true
351346
}
352347

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)
369349

370350
var splitKey = key.split('.')
371351
setKey(argv, splitKey, value)
@@ -407,6 +387,31 @@ function parse (args, opts) {
407387
}
408388
}
409389

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+
410415
// set args from config.json file, this should be
411416
// applied last so that defaults can be applied.
412417
function setConfig (argv) {
@@ -551,14 +556,142 @@ function parse (args, opts) {
551556

552557
var key = keys[keys.length - 1]
553558

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+
554565
if (value === increment) {
555566
o[key] = increment(o[key])
556567
} 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]
558569
} else if (o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts)) {
559570
o[key] = value
560571
} 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+
}
562695
} else if (configuration['duplicate-arguments-array']) {
563696
o[key] = [ o[key], value ]
564697
} else {

0 commit comments

Comments
 (0)