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 2 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
4 changes: 3 additions & 1 deletion lib/completion.ts
Expand Up @@ -258,7 +258,9 @@ export class Completion implements CompletionInstance {
if (!this.zshShell) {
completions.push(dashes + key);
} else {
const desc = descs[key] || '';
const aliasKey = this?.aliases?.[key]?.[0];
Copy link
Member

Choose a reason for hiding this comment

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

what about cases where there are multiple aliases associated with a key? should this be a loop?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, Thank you. Yes, sometimes the this?.aliases?.[key]?.[0] will be another alias without a description.
I will make it a loop.

const descFromAlias = aliasKey ? descs[aliasKey] : undefined;
const desc = descs[key] ?? descFromAlias ?? '';
completions.push(
dashes +
`${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`
Expand Down
21 changes: 21 additions & 0 deletions test/completion.cjs
Expand Up @@ -1055,6 +1055,27 @@ describe('Completion', () => {
r.logs.should.include('--help:Show help');
});

it('completes options and alias 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', type: 'string'},
})
.help(false)
.version(false)
.completion().argv
);

r.logs.should.have.length(4);
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');
});

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