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: introduce nargs-eats-options config option #246

Merged
merged 1 commit into from Feb 10, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -265,6 +265,13 @@ node example.js -x 1 2 -x 3 4
{ _: [], x: [[1, 2], [3, 4]] }
```

### nargs eats options

* default: `false`
* key: `nargs-eats-options`

Should nargs consume dash options as well as positional arguments.

### negation prefix

* default: `no-`
Expand Down
22 changes: 14 additions & 8 deletions index.js
Expand Up @@ -27,7 +27,8 @@ function parse (args, opts) {
'halt-at-non-option': false,
'strip-aliased': false,
'strip-dashed': false,
'unknown-options-as-args': false
'unknown-options-as-args': false,
'nargs-eats-options': false
}, opts.configuration)
const defaults = Object.assign(Object.create(null), opts.default)
const configObjects = opts.configObjects || []
Expand Down Expand Up @@ -366,16 +367,21 @@ function parse (args, opts) {
return i
}

// nargs will not consume flag arguments, e.g., -abc, --foo,
// and terminates when one is observed.
let available = 0
for (ii = i + 1; ii < args.length; ii++) {
if (!args[ii].match(/^-[^0-9]/) || args[ii].match(negative) || isUnknownOptionAsArg(args[ii])) available++
else break
if (configuration['nargs-eats-options']) {
// classic behavior, yargs eats positional and dash arguments.
if (args.length - (i + 1) < toEat) error = Error(__('Not enough arguments following: %s', key))
available = toEat
} else {
// nargs will not consume flag arguments, e.g., -abc, --foo,
// and terminates when one is observed.
for (ii = i + 1; ii < args.length; ii++) {
if (!args[ii].match(/^-[^0-9]/) || args[ii].match(negative) || isUnknownOptionAsArg(args[ii])) available++
else break
}
if (available < toEat) error = Error(__('Not enough arguments following: %s', key))
}

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

const consumed = Math.min(available, toEat)
for (ii = i + 1; ii < (consumed + i + 1); ii++) {
setArg(key, args[ii])
Expand Down
15 changes: 15 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -1903,6 +1903,21 @@ describe('yargs-parser', function () {
result.error.message.should.equal('Not enough arguments following: foo')
})

// See: https://github.com/yargs/yargs-parser/issues/232
it('should treat flag arguments as satisfying narg requirements, if nargs-eats-options=true', function () {
var result = parser.detailed(['--foo', '--bar', '99', '--batman', 'robin'], {
narg: {
foo: 2
},
configuration: {
'nargs-eats-options': true
}
})

result.argv.foo.should.eql(['--bar', 99])
result.argv.batman.should.eql('robin')
})

it('should not consume more than configured nargs', function () {
var result = parser(['--foo', 'a', 'b'], {
narg: {
Expand Down