From f7e15b9800900b9856acac1a830a5f35847be73e Mon Sep 17 00:00:00 2001 From: Mael Le Guen Date: Thu, 16 Apr 2020 21:42:27 +0200 Subject: [PATCH] fix(setArg): options using camel-case and dot-notation populated twice (#268) --- index.js | 6 +++++- test/yargs-parser.js | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 4fe26632..c14c1fc7 100644 --- a/index.js +++ b/index.js @@ -480,7 +480,11 @@ function parse (args, opts) { a.shift() // nuke the old key. x = x.concat(a) - setKey(argv, x, value) + // populate alias only if is not already an alias of the full key + // (already populated above) + if (!(flags.aliases[key] || []).includes(x.join('.'))) { + setKey(argv, x, value) + } }) } diff --git a/test/yargs-parser.js b/test/yargs-parser.js index aa7399ee..f5cbc183 100644 --- a/test/yargs-parser.js +++ b/test/yargs-parser.js @@ -959,6 +959,20 @@ describe('yargs-parser', function () { }) argv.f.bar.should.eql(99) + argv.foo.bar.should.eql(99) + }) + + // see #267 + it('should populate aliases when dot notation is used on camel-cased option', function () { + var argv = parser(['--foo-baz.bar', '99'], { + alias: { + 'foo-baz': ['f'] + } + }) + + argv.f.bar.should.eql(99) + argv['foo-baz'].bar.should.eql(99) + argv.fooBaz.bar.should.eql(99) }) it('should populate aliases when nested dot notation is used', function () {