-
Notifications
You must be signed in to change notification settings - Fork 119
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: flatten/duplicate regression #75
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -325,8 +325,8 @@ function parse (args, opts) { | |
i = ii | ||
argsToSet.push(args[ii]) | ||
} | ||
if (multipleArrayFlag && !configuration['flatten-duplicate-arrays']) { | ||
setArg(key, argsToSet) | ||
if (multipleArrayFlag) { | ||
setArg(key, argsToSet.map(arg => processValue(key, arg))) | ||
} else { | ||
argsToSet.forEach(function (arg) { | ||
setArg(key, arg) | ||
|
@@ -339,33 +339,13 @@ function parse (args, opts) { | |
function setArg (key, val) { | ||
unsetDefaulted(key) | ||
|
||
// handle parsing boolean arguments --foo=true --bar false. | ||
if (checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) { | ||
if (typeof val === 'string') val = val === 'true' | ||
} | ||
|
||
if (/-/.test(key) && !(flags.aliases[key] && flags.aliases[key].length) && configuration['camel-case-expansion']) { | ||
var c = camelCase(key) | ||
flags.aliases[key] = [c] | ||
newAliases[c] = true | ||
} | ||
|
||
var value = val | ||
if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) { | ||
if (isNumber(val)) value = Number(val) | ||
if (!isUndefined(val) && !isNumber(val) && checkAllAliases(key, flags.numbers)) value = NaN | ||
} | ||
|
||
// increment a count given as arg (either no value or value parsed as boolean) | ||
if (checkAllAliases(key, flags.counts) && (isUndefined(value) || typeof value === 'boolean')) { | ||
value = increment | ||
} | ||
|
||
// Set normalized value when key is in 'normalize' and in 'arrays' | ||
if (checkAllAliases(key, flags.normalize) && checkAllAliases(key, flags.arrays)) { | ||
if (Array.isArray(val)) value = val.map(path.normalize) | ||
else value = path.normalize(val) | ||
} | ||
var value = processValue(key, val) | ||
|
||
var splitKey = key.split('.') | ||
setKey(argv, splitKey, value) | ||
|
@@ -407,6 +387,31 @@ function parse (args, opts) { | |
} | ||
} | ||
|
||
function processValue (key, val) { | ||
// handle parsing boolean arguments --foo=true --bar false. | ||
if (checkAllAliases(key, flags.bools) || checkAllAliases(key, flags.counts)) { | ||
if (typeof val === 'string') val = val === 'true' | ||
} | ||
|
||
var value = val | ||
if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.coercions)) { | ||
if (isNumber(val)) value = Number(val) | ||
if (!isUndefined(val) && !isNumber(val) && checkAllAliases(key, flags.numbers)) value = NaN | ||
} | ||
|
||
// increment a count given as arg (either no value or value parsed as boolean) | ||
if (checkAllAliases(key, flags.counts) && (isUndefined(value) || typeof value === 'boolean')) { | ||
value = increment | ||
} | ||
|
||
// Set normalized value when key is in 'normalize' and in 'arrays' | ||
if (checkAllAliases(key, flags.normalize) && checkAllAliases(key, flags.arrays)) { | ||
if (Array.isArray(val)) value = val.map(path.normalize) | ||
else value = path.normalize(val) | ||
} | ||
return value | ||
} | ||
|
||
// set args from config.json file, this should be | ||
// applied last so that defaults can be applied. | ||
function setConfig (argv) { | ||
|
@@ -551,14 +556,142 @@ function parse (args, opts) { | |
|
||
var key = keys[keys.length - 1] | ||
|
||
var isTypeArray = checkAllAliases(key, flags.arrays) | ||
var isOldValueArray = Array.isArray(o[key]) | ||
var isNewValueArray = Array.isArray(value) | ||
var duplicate = configuration['duplicate-arguments-array'] | ||
var flatten = configuration['flatten-duplicate-arrays'] | ||
|
||
if (value === increment) { | ||
o[key] = increment(o[key]) | ||
} else if (o[key] === undefined && checkAllAliases(key, flags.arrays)) { | ||
o[key] = Array.isArray(value) && configuration['flatten-duplicate-arrays'] ? value : [value] | ||
o[key] = Array.isArray(value) ? value : [value] | ||
} else if (o[key] === undefined || checkAllAliases(key, flags.bools) || checkAllAliases(keys.join('.'), flags.bools) || checkAllAliases(key, flags.counts)) { | ||
o[key] = value | ||
} else if (Array.isArray(o[key])) { | ||
o[key].push(value) | ||
if (!duplicate && !flatten) { | ||
if (isTypeArray) { | ||
if (isOldValueArray) { | ||
if (isNewValueArray) { | ||
o[key] = value | ||
} else { | ||
o[key] = o[key].concat([value]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This logic can be simplified. |
||
} | ||
} else { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are unnecessary and drop us below 100% test coverage. |
||
} else { | ||
throw new Error('this should not happen') | ||
} | ||
} | ||
} else { | ||
if (isOldValueArray) { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
} else { | ||
throw new Error('this should not happen') | ||
} | ||
} else { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
} else { | ||
o[key] = value | ||
} | ||
} | ||
} | ||
} else if (!duplicate && flatten) { | ||
if (isTypeArray) { | ||
if (isOldValueArray) { | ||
if (isNewValueArray) { | ||
o[key] = value | ||
} else { | ||
o[key] = o[key].concat([value]) | ||
} | ||
} else { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
} else { | ||
throw new Error('this should not happen') | ||
} | ||
} | ||
} else { | ||
if (isOldValueArray) { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
} else { | ||
o[key] = value | ||
} | ||
} else { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
} else { | ||
o[key] = value | ||
} | ||
} | ||
} | ||
} else if (duplicate && flatten) { | ||
if (isTypeArray) { | ||
if (isOldValueArray) { | ||
if (isNewValueArray) { | ||
o[key] = o[key].concat(value) | ||
} else { | ||
o[key] = o[key].concat([value]) | ||
} | ||
} else { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
} else { | ||
throw new Error('this should not happen') | ||
} | ||
} | ||
} else { | ||
if (isOldValueArray) { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
} else { | ||
o[key] = o[key].concat([value]) | ||
} | ||
} else { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
} else { | ||
o[key] = o[key].concat([value]) | ||
} | ||
} | ||
} | ||
} else if (duplicate && !flatten) { | ||
if (isTypeArray) { | ||
if (isOldValueArray) { | ||
if (isNewValueArray) { | ||
o[key] = [o[key]].concat([value]) | ||
} else { | ||
o[key] = o[key].concat([value]) | ||
} | ||
} else { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
} else { | ||
throw new Error('this should not happen') | ||
} | ||
} | ||
} else { | ||
if (isOldValueArray) { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
} else { | ||
o[key] = o[key].concat([value]) | ||
} | ||
} else { | ||
if (isNewValueArray) { | ||
throw new Error('this should not happen') | ||
} else { | ||
o[key] = o[key].concat([value]) | ||
} | ||
} | ||
} | ||
} else { | ||
throw new Error('this should not happen') | ||
} | ||
} else if (configuration['duplicate-arguments-array']) { | ||
o[key] = [ o[key], value ] | ||
} else { | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to maintain Node 0.10 support for now (until early next year), so the arrow function needs to be replaced with a plain old function.