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 5 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
19 changes: 19 additions & 0 deletions lib/utils/camel-case.ts
@@ -0,0 +1,19 @@
export function camelCase(s: string) {
if (!s) return '';
// split camel case, then match ascii words
const words = asciiWords(s.replace(/([a-z0-9])([A-Z])/g, '$1 $2'));
return words.length > 1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should have access to camelCase already through the yargs-parser dependency:

https://github.com/yargs/yargs-parser/blob/main/lib/string-utils.ts#L7

I believe I already use one or two methods off of it elsewhere.

? words[0].toLowerCase() + words.slice(1).map(capitalizeFirst).join('')
: words[0].toLowerCase();
}

function capitalizeFirst(word: string): string {
return word ? word[0].toUpperCase() + word.slice(1).toLowerCase() : '';
}

// eslint-disable-next-line no-control-regex
const reAsciiWord = /[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/g;

function asciiWords(s: string) {
return s.match(reAsciiWord) || [];
}
19 changes: 19 additions & 0 deletions lib/validation.ts
Expand Up @@ -6,6 +6,7 @@ import {
} from './typings/common-types.js';
import {levenshtein as distance} from './utils/levenshtein.js';
import {objFilter} from './utils/obj-filter.js';
import {camelCase} from './utils/camel-case.js';
import {UsageInstance} from './usage.js';
import {YargsInstance, Arguments} from './yargs-factory.js';
import {DetailedArguments} from './typings/yargs-parser-types.js';
Expand Down Expand Up @@ -411,6 +412,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[camelCase(key)] !== undefined &&
argv[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