diff --git a/index.js b/index.js index 1d3e6b9b..5c5638bd 100644 --- a/index.js +++ b/index.js @@ -302,11 +302,20 @@ function parse (args, opts) { // how many arguments should we consume, based // on the nargs option? function eatNargs (i, key, args) { + var ii var toEat = checkAllAliases(key, flags.nargs) - if (args.length - (i + 1) < toEat) error = Error(__('Not enough arguments following: %s', key)) + // nargs will not consume flag arguments, e.g., -abc, --foo, + // and terminates when one is observed. + var available = 0 + for (ii = i + 1; ii < args.length; ii++) { + if (!args[ii].match(/^-[^0-9]/)) available++ + else break + } + + if (available < toEat) error = Error(__('Not enough arguments following: %s', key)) - for (var ii = i + 1; ii < (toEat + i + 1); ii++) { + for (ii = i + 1; ii < (available + i + 1); ii++) { setArg(key, args[ii]) } diff --git a/package.json b/package.json index 0c940b12..72d37499 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,8 @@ "description": "the mighty option parser used by yargs", "main": "index.js", "scripts": { - "pretest": "standard", "test": "nyc mocha test/*.js", + "posttest": "standard", "coverage": "nyc report --reporter=text-lcov | coveralls", "release": "standard-version" }, diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 24825f9e..6e1ae0cf 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -1643,6 +1643,16 @@ describe('yargs-parser', function () { result['1'][0].should.equal('a') result['1'][1].should.equal('b') }) + + it('should not treat flag arguments as satisfying narg requirements', function () { + var result = parser.detailed(['--foo', '--bar'], { + narg: { + foo: 1 + } + }) + + result.error.message.should.equal('Not enough arguments following: foo') + }) }) describe('env vars', function () {