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

feat: narg arguments no longer consume flag arguments #114

Merged
merged 1 commit into from
Jan 20, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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