From 8e486b31135b89908f248f492b073581bec28d5f Mon Sep 17 00:00:00 2001 From: bcoe Date: Sun, 21 Feb 2021 15:55:25 -0800 Subject: [PATCH 1/3] fix(populate--): -- should always be array --- lib/yargs-parser.ts | 2 ++ test/yargs-parser.cjs | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/lib/yargs-parser.ts b/lib/yargs-parser.ts index 02cc1272..602d0041 100644 --- a/lib/yargs-parser.ts +++ b/lib/yargs-parser.ts @@ -408,6 +408,8 @@ export class YargsParser { // '--' defaults to undefined. if (notFlagsOption && notFlags.length) argv[notFlagsArgv] = [] + // See: https://github.com/yargs/yargs/issues/1869. + if (notFlagsOption && !Array.isArray(argv[notFlagsArgv])) argv[notFlagsArgv] = [argv[notFlagsArgv]] notFlags.forEach(function (key) { argv[notFlagsArgv].push(key) }) diff --git a/test/yargs-parser.cjs b/test/yargs-parser.cjs index 63504f19..89b32157 100644 --- a/test/yargs-parser.cjs +++ b/test/yargs-parser.cjs @@ -3244,6 +3244,18 @@ describe('yargs-parser', function () { k: true }) }) + // See: https://github.com/yargs/yargs/issues/1869 + // TODO(@bcoe): add additional test cases for handling of edge cases like + // ----=hello ---=hello ---- hello. + it('should default -- to arrray when populate-- set', () => { + const argv = parser('----=test', { + configuration: { + 'populate--': true + } + }) + argv['--'].should.eql(['test']) + argv[''].should.eql(['test']) + }) // see: https://github.com/yargs/yargs/issues/1489 it('should identify "hasOwnProperty" as unknown option', () => { const argv = parser('--known-arg=1 --hasOwnProperty=33', { From 1095705b8e493c0f59013721fb4ebdb079d0abbc Mon Sep 17 00:00:00 2001 From: bcoe Date: Sun, 21 Feb 2021 17:34:38 -0800 Subject: [PATCH 2/3] fix: better fix for 1869 --- lib/yargs-parser.ts | 9 ++++++--- test/yargs-parser.cjs | 7 +++---- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/yargs-parser.ts b/lib/yargs-parser.ts index 602d0041..bc7ff878 100644 --- a/lib/yargs-parser.ts +++ b/lib/yargs-parser.ts @@ -214,6 +214,11 @@ export class YargsParser { // any unknown option (except for end-of-options, "--") if (arg !== '--' && isUnknownOptionAsArg(arg)) { pushPositional(arg) + // ---, ---=, ----, etc, + } else if (arg.match(/---+(=|$)/)) { + // options without key name are invalid. + pushPositional(arg) + continue; // -- separated by = } else if (arg.match(/^--.+=/) || ( !configuration['short-option-groups'] && arg.match(/^-.+=/) @@ -407,9 +412,7 @@ export class YargsParser { }) // '--' defaults to undefined. - if (notFlagsOption && notFlags.length) argv[notFlagsArgv] = [] - // See: https://github.com/yargs/yargs/issues/1869. - if (notFlagsOption && !Array.isArray(argv[notFlagsArgv])) argv[notFlagsArgv] = [argv[notFlagsArgv]] + if (notFlagsOption && notFlags.length) argv[notFlagsArgv] = []; notFlags.forEach(function (key) { argv[notFlagsArgv].push(key) }) diff --git a/test/yargs-parser.cjs b/test/yargs-parser.cjs index 89b32157..d8bff6e9 100644 --- a/test/yargs-parser.cjs +++ b/test/yargs-parser.cjs @@ -3245,16 +3245,15 @@ describe('yargs-parser', function () { }) }) // See: https://github.com/yargs/yargs/issues/1869 - // TODO(@bcoe): add additional test cases for handling of edge cases like // ----=hello ---=hello ---- hello. - it('should default -- to arrray when populate-- set', () => { + it('should not populate "--" for key values other than "--"', () => { const argv = parser('----=test', { configuration: { 'populate--': true } }) - argv['--'].should.eql(['test']) - argv[''].should.eql(['test']) + argv['_'].should.eql(['----=test']) + expect(argv['--']).to.equal(undefined); }) // see: https://github.com/yargs/yargs/issues/1489 it('should identify "hasOwnProperty" as unknown option', () => { From 5b29247df5ed52339b31fc2cd258fcfeacc6aa45 Mon Sep 17 00:00:00 2001 From: bcoe Date: Sun, 21 Feb 2021 18:08:45 -0800 Subject: [PATCH 3/3] lint: fix linting --- lib/yargs-parser.ts | 4 ++-- test/yargs-parser.cjs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/yargs-parser.ts b/lib/yargs-parser.ts index bc7ff878..b8bd8775 100644 --- a/lib/yargs-parser.ts +++ b/lib/yargs-parser.ts @@ -218,7 +218,7 @@ export class YargsParser { } else if (arg.match(/---+(=|$)/)) { // options without key name are invalid. pushPositional(arg) - continue; + continue // -- separated by = } else if (arg.match(/^--.+=/) || ( !configuration['short-option-groups'] && arg.match(/^-.+=/) @@ -412,7 +412,7 @@ export class YargsParser { }) // '--' defaults to undefined. - if (notFlagsOption && notFlags.length) argv[notFlagsArgv] = []; + if (notFlagsOption && notFlags.length) argv[notFlagsArgv] = [] notFlags.forEach(function (key) { argv[notFlagsArgv].push(key) }) diff --git a/test/yargs-parser.cjs b/test/yargs-parser.cjs index d8bff6e9..f45256ed 100644 --- a/test/yargs-parser.cjs +++ b/test/yargs-parser.cjs @@ -3252,8 +3252,8 @@ describe('yargs-parser', function () { 'populate--': true } }) - argv['_'].should.eql(['----=test']) - expect(argv['--']).to.equal(undefined); + argv._.should.eql(['----=test']) + expect(argv['--']).to.equal(undefined) }) // see: https://github.com/yargs/yargs/issues/1489 it('should identify "hasOwnProperty" as unknown option', () => {