Skip to content

Commit

Permalink
feat: add halt-at-non-option configuration option (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
wmertens authored and bcoe committed Nov 9, 2018
1 parent ee56e31 commit a849fce
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -321,6 +321,27 @@ node example.js -a 1 -c 2
{ _: [], a: 1, b: undefined, c: 2 }
```
### halt at non-option
* default: `false`.
* key: `halt-at-non-option`.
Should parsing stop at the first text argument? This is similar to how e.g. `ssh` parses its command line.
_If disabled:_
```sh
node example.js -a run b -x y
{ _: [ 'run', 'b', 'y' ], a: true, x: true }
```
_If enabled:_
```sh
node example.js -a run b -x y
{ _: [ 'run', '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 @@ -22,7 +22,8 @@ function parse (args, opts) {
'flatten-duplicate-arrays': true,
'populate--': false,
'combine-arrays': false,
'set-placeholder-key': false
'set-placeholder-key': false,
'halt-at-non-option': false
}, opts.configuration)
var defaults = opts.default || {}
var configObjects = opts.configObjects || []
Expand Down Expand Up @@ -139,10 +140,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 @@ -299,6 +296,12 @@ function parse (args, opts) {
}
}
}
} else if (arg === '--') {
notFlags = args.slice(i + 1)
break
} else if (configuration['halt-at-non-option']) {
notFlags = args.slice(i)
break
} else {
argv._.push(maybeCoerceNumber('_', arg))
}
Expand Down
33 changes: 33 additions & 0 deletions test/yargs-parser.js
Expand Up @@ -2514,6 +2514,39 @@ describe('yargs-parser', function () {
parsed.should.not.have.property('a.b')
})
})

describe('halt-at-non-option', function () {
it('gets the entire rest of line', function () {
var parse = parser(['--foo', './file.js', '--foo', '--bar'], {
configuration: { 'halt-at-non-option': 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: { 'halt-at-non-option': true }, boolean: ['foo', 'bar'] }
)
parse.should.deep.equal({
foo: true,
_: ['./file.js', '--foo', '--', 'barbar', '--bar']
})
})

it('is not influenced by unknown options', function () {
var parse = parser(
['-v', '--long', 'arg', './file.js', '--foo', '--', 'barbar'],
{ configuration: { 'halt-at-non-option': true }, boolean: ['foo'] }
)
parse.should.deep.equal({
v: true,
long: 'arg',
_: ['./file.js', '--foo', '--', 'barbar']
})
})
})
})

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

0 comments on commit a849fce

Please sign in to comment.