Skip to content

Commit

Permalink
feat: introduce nargs-eats-options config option (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe committed Feb 10, 2020
1 parent 0dea619 commit d50822a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
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

0 comments on commit d50822a

Please sign in to comment.