Skip to content

Commit

Permalink
feat: narg arguments no longer consume flag arguments (yargs#114)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: arguments of form --foo, -abc, will no longer be consumed by nargs
  • Loading branch information
bcoe authored Jan 20, 2018
1 parent 29b0248 commit 60bb9b3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
10 changes: 10 additions & 0 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 60bb9b3

Please sign in to comment.