Skip to content

Commit 59a86fb

Browse files
authored
fix: conflicts and strip-dashed (#1998)
1 parent 5c54e89 commit 59a86fb

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

lib/validation.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,24 @@ export function validation(
411411
});
412412
}
413413
});
414+
415+
// When strip-dashed is true, match conflicts (kebab) with argv (camel)
416+
// Addresses: https://github.com/yargs/yargs/issues/1952
417+
if (yargs.getInternalMethods().getParserConfiguration()['strip-dashed']) {
418+
Object.keys(conflicting).forEach(key => {
419+
conflicting[key].forEach(value => {
420+
if (
421+
value &&
422+
argv[shim.Parser.camelCase(key)] !== undefined &&
423+
argv[shim.Parser.camelCase(value)] !== undefined
424+
) {
425+
usage.fail(
426+
__('Arguments %s and %s are mutually exclusive', key, value)
427+
);
428+
}
429+
});
430+
});
431+
}
414432
};
415433

416434
self.recommendCommands = function recommendCommands(cmd, potentialCommands) {

test/validation.cjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,27 @@ describe('validation tests', () => {
204204
.parse();
205205
});
206206

207+
// Addresses: https://github.com/yargs/yargs/issues/1952
208+
it('fails if conflicting arguments are provided, and strip-dashed is enabled', () => {
209+
yargs()
210+
.option('foo-foo', {
211+
description: 'a foo-foo',
212+
type: 'string',
213+
})
214+
.option('bar-bar', {
215+
description: 'a bar-bar',
216+
type: 'string',
217+
})
218+
.conflicts({'foo-foo': 'bar-bar'})
219+
.parserConfiguration({'strip-dashed': true})
220+
.parse('--foo-foo a --bar-bar b', (error, argv, output) => {
221+
expect(error).to.not.equal(null);
222+
error.message.should.match(
223+
/Arguments foo-foo and bar-bar are mutually exclusive/
224+
);
225+
});
226+
});
227+
207228
it('should not fail if no conflicting arguments are provided', () => {
208229
yargs(['-b', '-c'])
209230
.conflicts('f', ['b', 'c'])

0 commit comments

Comments
 (0)