Skip to content

Commit

Permalink
fix: nargs was still aggressively consuming too many arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin committed Jan 20, 2018
1 parent cf0d9d0 commit 9b28aad
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ function parse (args, opts) {
// on the nargs option?
function eatNargs (i, key, args) {
var ii
var toEat = checkAllAliases(key, flags.nargs)
const toEat = checkAllAliases(key, flags.nargs)

// nargs will not consume flag arguments, e.g., -abc, --foo,
// and terminates when one is observed.
Expand All @@ -315,11 +315,12 @@ function parse (args, opts) {

if (available < toEat) error = Error(__('Not enough arguments following: %s', key))

for (ii = i + 1; ii < (Math.min(available, toEat) + i + 1); ii++) {
const consumed = Math.min(available, toEat)
for (ii = i + 1; ii < (consumed + i + 1); ii++) {
setArg(key, args[ii])
}

return (i + toEat)
return (i + consumed)
}

// if an option is an array, eat all non-hyphenated arguments
Expand Down
3 changes: 2 additions & 1 deletion test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1645,12 +1645,13 @@ describe('yargs-parser', function () {
})

it('should not treat flag arguments as satisfying narg requirements', function () {
var result = parser.detailed(['--foo', '--bar'], {
var result = parser.detailed(['--foo', '--bar', '99'], {
narg: {
foo: 1
}
})

result.argv.bar.should.equal(99)
result.error.message.should.equal('Not enough arguments following: foo')
})

Expand Down

0 comments on commit 9b28aad

Please sign in to comment.