Skip to content

Commit

Permalink
Add stop-at-unknown configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens committed Aug 18, 2018
1 parent 82f4ea5 commit 50c227b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -319,6 +319,27 @@ node example.js -a 1 -c 2
{ _: [], a: 1, b: undefined, c: 2 }
```
### stop at unknown
* default: `false`.
* key: `stop-at-unknown`.
Should parsing stop at the first unknown argument?
_If disabled:_
```sh
node example.js -a b -x y
{ _: [ 'b', 'y' ], a: true, x: true }
```
_If enabled:_
```sh
node example.js -a b -x y
{ _: [ 'b', '-x', 'y' ], a: true }
```
## Special Thanks
The yargs project evolves from optimist and minimist. It owes its
Expand Down
13 changes: 8 additions & 5 deletions index.js
Expand Up @@ -21,7 +21,8 @@ function parse (args, opts) {
'flatten-duplicate-arrays': true,
'populate--': false,
'combine-arrays': false,
'set-placeholder-key': false
'set-placeholder-key': false,
'stop-at-unknown': false
}, opts.configuration)
var defaults = opts.default || {}
var configObjects = opts.configObjects || []
Expand Down Expand Up @@ -122,10 +123,6 @@ function parse (args, opts) {
})

var notFlags = []
if (args.indexOf('--') !== -1) {
notFlags = args.slice(args.indexOf('--') + 1)
args = args.slice(0, args.indexOf('--'))
}

for (var i = 0; i < args.length; i++) {
var arg = args[i]
Expand Down Expand Up @@ -282,6 +279,12 @@ function parse (args, opts) {
}
}
}
} else if (arg === '--') {
notFlags = args.slice(i + 1)
break
} else if (configuration['stop-at-unknown']) {
notFlags = args.slice(i)
break
} else {
argv._.push(maybeCoerceNumber('_', arg))
}
Expand Down
21 changes: 21 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -2383,6 +2383,27 @@ describe('yargs-parser', function () {
parsed.should.not.have.property('a.b')
})
})

describe('stop-at-unknown', function() {
it('gets the entire rest of line', function() {
var parse = parser(['--foo', './file.js', '--foo', '--bar'], {
configuration: {'stop-at-unknown': true},
boolean: ['foo', 'bar'],
})
parse.should.deep.equal({foo: true, _: ['./file.js', '--foo', '--bar']})
})

it('is not influenced by --', function() {
var parse = parser(
['--foo', './file.js', '--foo', '--', 'barbar', '--bar'],
{configuration: {'stop-at-unknown': true}, boolean: ['foo', 'bar']}
)
parse.should.deep.equal({
foo: true,
_: ['./file.js', '--foo', '--', 'barbar', '--bar'],
})
})
})
})

// addresses: https://github.com/yargs/yargs-parser/issues/41
Expand Down

0 comments on commit 50c227b

Please sign in to comment.