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 13, 2019
1 parent 26643e0 commit d1cdc70
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) && (args.length > i + 1 || checkAllAliases(key, flags.bools))) {
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)) {
// TODO: set user default value, if available
// argsToSet.push(defaultForType('array'))
} 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 d1cdc70

Please sign in to comment.