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: copy the description of the option to its alias in completion #2269

Merged
merged 5 commits into from Dec 28, 2022
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
7 changes: 6 additions & 1 deletion lib/completion.ts
Expand Up @@ -258,7 +258,12 @@ export class Completion implements CompletionInstance {
if (!this.zshShell) {
completions.push(dashes + key);
} else {
const desc = descs[key] || '';
const aliasKey = this?.aliases?.[key].find(alias => {
const desc = descs[alias];
return typeof desc === 'string' && desc.length > 0;
});
const descFromAlias = aliasKey ? descs[aliasKey] : undefined;
const desc = descs[key] ?? descFromAlias ?? '';
completions.push(
dashes +
`${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`
Expand Down
22 changes: 22 additions & 0 deletions test/completion.cjs
Expand Up @@ -1055,6 +1055,28 @@ describe('Completion', () => {
r.logs.should.include('--help:Show help');
});

it('completes options and aliases with the same description', () => {
process.env.SHELL = '/bin/zsh';
const r = checkUsage(
() =>
yargs(['./completion', '--get-yargs-completions', '-'])
.options({
foo: {describe: 'Foo option', alias: 'f', type: 'string'},
bar: {describe: 'Bar option', alias: ['b', 'B'], type: 'string'},
})
.help(false)
.version(false)
.completion().argv
);

r.logs.should.have.length(5);
r.logs.should.include('--foo:Foo option');
r.logs.should.include('-f:Foo option');
r.logs.should.include('--bar:Bar option');
r.logs.should.include('-b:Bar option');
r.logs.should.include('-B:Bar option');
});

it('replaces application variable with $0 in script', () => {
process.env.SHELL = '/bin/zsh';
const r = checkUsage(() => yargs([]).showCompletionScript(), ['ndm']);
Expand Down