From b43685c9e3a83579d2939a3b633a5e7d22f23c8e Mon Sep 17 00:00:00 2001 From: Wout Mertens Date: Tue, 9 Oct 2018 12:28:19 +0200 Subject: [PATCH] rename to halt-at-non-option --- README.md | 14 +++++++------- index.js | 4 ++-- test/yargs-parser.js | 20 ++++++++++++++++---- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index c60a25cf..85bd874e 100644 --- a/README.md +++ b/README.md @@ -319,25 +319,25 @@ node example.js -a 1 -c 2 { _: [], a: 1, b: undefined, c: 2 } ``` -### stop at unknown +### halt at non-option * default: `false`. -* key: `stop-at-unknown`. +* key: `halt-at-non-option`. -Should parsing stop at the first unknown argument? +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 b -x y -{ _: [ 'b', 'y' ], a: true, x: true } +node example.js -a run b -x y +{ _: [ 'run', 'b', 'y' ], a: true, x: true } ``` _If enabled:_ ```sh -node example.js -a b -x y -{ _: [ 'b', '-x', 'y' ], a: true } +node example.js -a run b -x y +{ _: [ 'run', 'b', '-x', 'y' ], a: true } ``` ## Special Thanks diff --git a/index.js b/index.js index 71300f7f..829b376e 100644 --- a/index.js +++ b/index.js @@ -22,7 +22,7 @@ function parse (args, opts) { 'populate--': false, 'combine-arrays': false, 'set-placeholder-key': false, - 'stop-at-unknown': false + 'halt-at-non-option': false }, opts.configuration) var defaults = opts.default || {} var configObjects = opts.configObjects || [] @@ -282,7 +282,7 @@ function parse (args, opts) { } else if (arg === '--') { notFlags = args.slice(i + 1) break - } else if (configuration['stop-at-unknown']) { + } else if (configuration['halt-at-non-option']) { notFlags = args.slice(i) break } else { diff --git a/test/yargs-parser.js b/test/yargs-parser.js index 01177651..a6427c36 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -2384,25 +2384,37 @@ describe('yargs-parser', function () { }) }) - describe('stop-at-unknown', function () { + describe('halt-at-non-option', function () { it('gets the entire rest of line', function () { var parse = parser(['--foo', './file.js', '--foo', '--bar'], { - configuration: {'stop-at-unknown': true}, + configuration: { 'halt-at-non-option': true }, boolean: ['foo', 'bar'] }) - parse.should.deep.equal({foo: true, _: ['./file.js', '--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']} + { 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'] + }) + }) }) })