From 9b28aad9c44d73e966d406c00218f004d31411e0 Mon Sep 17 00:00:00 2001 From: Benjamin Date: Sat, 20 Jan 2018 15:21:11 -0800 Subject: [PATCH] fix: nargs was still aggressively consuming too many arguments --- index.js | 7 ++++--- test/yargs-parser.js | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 6405e571..d39414b7 100644 --- a/index.js +++ b/index.js @@ -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. @@ -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 diff --git a/test/yargs-parser.js b/test/yargs-parser.js index c1e3ff1e..b9bc91c9 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -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') })