Skip to content
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 2 commits into from Jan 2, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
183 changes: 158 additions & 25 deletions index.js
Expand Up @@ -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)))
Copy link
Member

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.

} else {
argsToSet.forEach(function (arg) {
setArg(key, arg)
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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])
Copy link
Member

Choose a reason for hiding this comment

The 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')
Copy link
Member

Choose a reason for hiding this comment

The 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 {
Expand Down