Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: conflicts and strip-dashed #1998

Merged
merged 7 commits into from Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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