From a849fce0ba37414b3f3c2e01a547654dc1035623 Mon Sep 17 00:00:00 2001 From: Wout Mertens Date: Fri, 9 Nov 2018 17:14:21 +0100 Subject: [PATCH] feat: add halt-at-non-option configuration option (#130) --- README.md | 21 +++++++++++++++++++++ index.js | 13 ++++++++----- test/yargs-parser.js | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 1cccd3ad..ca926583 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.js b/index.js index 98ee7ae2..917189a9 100644 --- a/index.js +++ b/index.js @@ -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 || [] @@ -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] @@ -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)) } diff --git a/test/yargs-parser.js b/test/yargs-parser.js index b0e8555b..a1d17de2 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -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