Skip to content

Commit

Permalink
fix: conflicts and strip-dashed (#1998)
Browse files Browse the repository at this point in the history
  • Loading branch information
jly36963 committed Sep 7, 2021
1 parent 5c54e89 commit 59a86fb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
18 changes: 18 additions & 0 deletions lib/validation.ts
Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions test/validation.cjs
Expand Up @@ -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'])
Expand Down

0 comments on commit 59a86fb

Please sign in to comment.