Skip to content

Commit a849fce

Browse files
wmertensbcoe
authored andcommitted
feat: add halt-at-non-option configuration option (#130)
1 parent ee56e31 commit a849fce

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,27 @@ node example.js -a 1 -c 2
321321
{ _: [], a: 1, b: undefined, c: 2 }
322322
```
323323
324+
### halt at non-option
325+
326+
* default: `false`.
327+
* key: `halt-at-non-option`.
328+
329+
Should parsing stop at the first text argument? This is similar to how e.g. `ssh` parses its command line.
330+
331+
_If disabled:_
332+
333+
```sh
334+
node example.js -a run b -x y
335+
{ _: [ 'run', 'b', 'y' ], a: true, x: true }
336+
```
337+
338+
_If enabled:_
339+
340+
```sh
341+
node example.js -a run b -x y
342+
{ _: [ 'run', 'b', '-x', 'y' ], a: true }
343+
```
344+
324345
## Special Thanks
325346
326347
The yargs project evolves from optimist and minimist. It owes its

index.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function parse (args, opts) {
2222
'flatten-duplicate-arrays': true,
2323
'populate--': false,
2424
'combine-arrays': false,
25-
'set-placeholder-key': false
25+
'set-placeholder-key': false,
26+
'halt-at-non-option': false
2627
}, opts.configuration)
2728
var defaults = opts.default || {}
2829
var configObjects = opts.configObjects || []
@@ -139,10 +140,6 @@ function parse (args, opts) {
139140
})
140141

141142
var notFlags = []
142-
if (args.indexOf('--') !== -1) {
143-
notFlags = args.slice(args.indexOf('--') + 1)
144-
args = args.slice(0, args.indexOf('--'))
145-
}
146143

147144
for (var i = 0; i < args.length; i++) {
148145
var arg = args[i]
@@ -299,6 +296,12 @@ function parse (args, opts) {
299296
}
300297
}
301298
}
299+
} else if (arg === '--') {
300+
notFlags = args.slice(i + 1)
301+
break
302+
} else if (configuration['halt-at-non-option']) {
303+
notFlags = args.slice(i)
304+
break
302305
} else {
303306
argv._.push(maybeCoerceNumber('_', arg))
304307
}

test/yargs-parser.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2514,6 +2514,39 @@ describe('yargs-parser', function () {
25142514
parsed.should.not.have.property('a.b')
25152515
})
25162516
})
2517+
2518+
describe('halt-at-non-option', function () {
2519+
it('gets the entire rest of line', function () {
2520+
var parse = parser(['--foo', './file.js', '--foo', '--bar'], {
2521+
configuration: { 'halt-at-non-option': true },
2522+
boolean: ['foo', 'bar']
2523+
})
2524+
parse.should.deep.equal({ foo: true, _: ['./file.js', '--foo', '--bar'] })
2525+
})
2526+
2527+
it('is not influenced by --', function () {
2528+
var parse = parser(
2529+
['--foo', './file.js', '--foo', '--', 'barbar', '--bar'],
2530+
{ configuration: { 'halt-at-non-option': true }, boolean: ['foo', 'bar'] }
2531+
)
2532+
parse.should.deep.equal({
2533+
foo: true,
2534+
_: ['./file.js', '--foo', '--', 'barbar', '--bar']
2535+
})
2536+
})
2537+
2538+
it('is not influenced by unknown options', function () {
2539+
var parse = parser(
2540+
['-v', '--long', 'arg', './file.js', '--foo', '--', 'barbar'],
2541+
{ configuration: { 'halt-at-non-option': true }, boolean: ['foo'] }
2542+
)
2543+
parse.should.deep.equal({
2544+
v: true,
2545+
long: 'arg',
2546+
_: ['./file.js', '--foo', '--', 'barbar']
2547+
})
2548+
})
2549+
})
25172550
})
25182551

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

0 commit comments

Comments
 (0)