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: array should take precedence over nargs, but enforce nargs #243

Merged
merged 1 commit into from Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
53 changes: 30 additions & 23 deletions index.js
Expand Up @@ -163,14 +163,14 @@ function parse (args, opts) {
// http://stackoverflow.com/a/1068308/13216
m = arg.match(/^--?([^=]+)=([\s\S]*)$/)

// nargs format = '--f=monkey washing cat'
if (checkAllAliases(m[1], flags.nargs)) {
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)) {
if (checkAllAliases(m[1], flags.arrays)) {
args.splice(i + 1, 0, m[2])
i = eatArray(i, m[1], args)
} else if (checkAllAliases(m[1], flags.nargs)) {
// nargs format = '--f=monkey washing cat'
args.splice(i + 1, 0, m[2])
i = eatNargs(i, m[1], args)
} else {
setArg(m[1], m[2])
}
Expand All @@ -184,13 +184,13 @@ function parse (args, opts) {
)) {
key = arg.match(/^--?(.+)/)[1]

// nargs format = '--foo a b c'
// should be truthy even if: flags.nargs[key] === 0
if (checkAllAliases(key, flags.nargs) !== false) {
i = eatNargs(i, key, args)
// array format = '--foo a b c'
} else if (checkAllAliases(key, flags.arrays)) {
if (checkAllAliases(key, flags.arrays)) {
// array format = '--foo a b c'
i = eatArray(i, key, args)
} else if (checkAllAliases(key, flags.nargs) !== false) {
// nargs format = '--foo a b c'
// should be truthy even if: flags.nargs[key] === 0
i = eatNargs(i, key, args)
} else {
next = args[i + 1]

Expand Down Expand Up @@ -237,14 +237,14 @@ function parse (args, opts) {
value = arg.slice(j + 3)
key = letters[j]

// nargs format = '-f=monkey washing cat'
if (checkAllAliases(key, flags.nargs)) {
args.splice(i + 1, 0, value)
i = eatNargs(i, key, args)
// array format = '-f=a b c'
} else if (checkAllAliases(key, flags.arrays)) {
if (checkAllAliases(key, flags.arrays)) {
// array format = '-f=a b c'
args.splice(i + 1, 0, value)
i = eatArray(i, key, args)
} else if (checkAllAliases(key, flags.nargs)) {
// nargs format = '-f=monkey washing cat'
args.splice(i + 1, 0, value)
i = eatNargs(i, key, args)
} else {
setArg(key, value)
}
Expand Down Expand Up @@ -278,13 +278,13 @@ function parse (args, opts) {
key = arg.slice(-1)[0]

if (!broken && key !== '-') {
// nargs format = '-f a b c'
// should be truthy even if: flags.nargs[key] === 0
if (checkAllAliases(key, flags.nargs) !== false) {
i = eatNargs(i, key, args)
// array format = '-f a b c'
} else if (checkAllAliases(key, flags.arrays)) {
if (checkAllAliases(key, flags.arrays)) {
// array format = '-f a b c'
i = eatArray(i, key, args)
} else if (checkAllAliases(key, flags.nargs) !== false) {
// nargs format = '-f a b c'
// should be truthy even if: flags.nargs[key] === 0
i = eatNargs(i, key, args)
} else {
next = args[i + 1]

Expand Down Expand Up @@ -409,6 +409,13 @@ function parse (args, opts) {
}
}

// If both array and nargs are configured, create an error if less than
// nargs positionals were found:
const toEat = checkAllAliases(key, flags.nargs)
if (toEat && argsToSet.length < toEat) {
error = Error(__('Not enough arguments following: %s', key))
}

setArg(key, argsToSet)
return i
}
Expand Down
24 changes: 24 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -3539,4 +3539,28 @@ describe('yargs-parser', function () {
})
parse.infinite.should.equal(false)
})

// See: https://github.com/yargs/yargs/issues/1098
describe('array with nargs', () => {
it('allows array and nargs to be configured in conjunction, enforcing the nargs value', () => {
var parse = parser(['-a', 'apple', 'banana'], {
array: 'a',
narg: {
a: 1
}
})
parse.a.should.eql(['apple', 'banana'])
})

it('returns an error if not enough positionals were provided for nargs', () => {
var parse = parser.detailed(['-a'], {
array: 'a',
narg: {
a: 1
}
})
parse.argv.a.should.eql([])
parse.error.message.should.equal('Not enough arguments following: a')
})
})
})