From 59a86fb83cfeb8533c6dd446c73cf4166cc455f2 Mon Sep 17 00:00:00 2001 From: Landon Yarrington <33426811+jly36963@users.noreply.github.com> Date: Tue, 7 Sep 2021 16:35:43 -0600 Subject: [PATCH] fix: conflicts and strip-dashed (#1998) --- lib/validation.ts | 18 ++++++++++++++++++ test/validation.cjs | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/validation.ts b/lib/validation.ts index 71dd77db0..0a5b334a3 100644 --- a/lib/validation.ts +++ b/lib/validation.ts @@ -411,6 +411,24 @@ export function validation( }); } }); + + // When strip-dashed is true, match conflicts (kebab) with argv (camel) + // Addresses: https://github.com/yargs/yargs/issues/1952 + if (yargs.getInternalMethods().getParserConfiguration()['strip-dashed']) { + Object.keys(conflicting).forEach(key => { + conflicting[key].forEach(value => { + if ( + value && + argv[shim.Parser.camelCase(key)] !== undefined && + argv[shim.Parser.camelCase(value)] !== undefined + ) { + usage.fail( + __('Arguments %s and %s are mutually exclusive', key, value) + ); + } + }); + }); + } }; self.recommendCommands = function recommendCommands(cmd, potentialCommands) { diff --git a/test/validation.cjs b/test/validation.cjs index 6e15ab4f9..8a116fdab 100644 --- a/test/validation.cjs +++ b/test/validation.cjs @@ -204,6 +204,27 @@ describe('validation tests', () => { .parse(); }); + // Addresses: https://github.com/yargs/yargs/issues/1952 + it('fails if conflicting arguments are provided, and strip-dashed is enabled', () => { + yargs() + .option('foo-foo', { + description: 'a foo-foo', + type: 'string', + }) + .option('bar-bar', { + description: 'a bar-bar', + type: 'string', + }) + .conflicts({'foo-foo': 'bar-bar'}) + .parserConfiguration({'strip-dashed': true}) + .parse('--foo-foo a --bar-bar b', (error, argv, output) => { + expect(error).to.not.equal(null); + error.message.should.match( + /Arguments foo-foo and bar-bar are mutually exclusive/ + ); + }); + }); + it('should not fail if no conflicting arguments are provided', () => { yargs(['-b', '-c']) .conflicts('f', ['b', 'c'])