Skip to content

Commit

Permalink
eatArray() with boolean's
Browse files Browse the repository at this point in the history
  • Loading branch information
juergba committed Jul 14, 2019
1 parent 31c204b commit 29542b2
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,15 @@ function parse (args, opts) {
args.splice(i + 1, 0, m[2])
i = eatNargs(i, m[1], args)
// arrays format = '--f=a b c'
} else if (checkAllAliases(m[1], flags.arrays) && args.length > i + 1) {
} else if (checkAllAliases(m[1], flags.arrays)) {
args.splice(i + 1, 0, m[2])
i = eatArray(i, m[1], args)
} else {
setArg(m[1], m[2])
}
} else if (arg.match(negatedBoolean) && configuration['boolean-negation']) {
key = arg.match(negatedBoolean)[1]
setArg(key, false)
setArg(key, checkAllAliases(key, flags.arrays) ? [false] : false)

// -- seperated by space.
} else if (arg.match(/^--.+/) || (
Expand All @@ -176,7 +176,7 @@ function parse (args, opts) {
if (checkAllAliases(key, flags.nargs)) {
i = eatNargs(i, key, args)
// array format = '--foo a b c'
} else if (checkAllAliases(key, flags.arrays) && args.length > i + 1) {
} else if (checkAllAliases(key, flags.arrays)) {
i = eatArray(i, key, args)
} else {
next = flags.nargs[key] === 0 ? undefined : args[i + 1]
Expand Down Expand Up @@ -369,30 +369,24 @@ function parse (args, opts) {
// following it... YUM!
// e.g., --foo apple banana cat becomes ["apple", "banana", "cat"]
function eatArray (i, key, args) {
var start = i + 1
var argsToSet = []
var multipleArrayFlag = i > 0
for (var ii = i + 1; ii < args.length; ii++) {
if (/^-/.test(args[ii]) && !negative.test(args[ii])) {
if (ii === start) {
setArg(key, defaultForType('array'))
}
multipleArrayFlag = true
break
}
i = ii
argsToSet.push(args[ii])
}
if (multipleArrayFlag) {
setArg(key, argsToSet.map(function (arg) {
return processValue(key, arg)
}))
let argsToSet = []
let next = args[i + 1]

if (checkAllAliases(key, flags.bools) && !(/^(true|false)$/.test(next))) {
argsToSet.push(true)
} else if (/^-/.test(next) && !negative.test(next)) {
// if ( TODO: set user default value, if available )
// else ( key without value ==> argsToSet === [] )
} else {
argsToSet.forEach(function (arg) {
setArg(key, arg)
})
for (var ii = i + 1; ii < args.length; ii++) {
next = args[ii]
if (/^-/.test(next) && !negative.test(next)) break
i = ii
argsToSet.push(processValue(key, next))
}
}

setArg(key, argsToSet)
return i
}

Expand Down

0 comments on commit 29542b2

Please sign in to comment.