Skip to content

Commit

Permalink
fix: copy the description of the option to its alias in completion (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tasshi-me committed Dec 28, 2022
1 parent 1fd530a commit f37ee6f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
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

0 comments on commit f37ee6f

Please sign in to comment.