Skip to content

Commit f37ee6f

Browse files
authored
fix: copy the description of the option to its alias in completion (#2269)
1 parent 1fd530a commit f37ee6f

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/completion.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,12 @@ export class Completion implements CompletionInstance {
258258
if (!this.zshShell) {
259259
completions.push(dashes + key);
260260
} else {
261-
const desc = descs[key] || '';
261+
const aliasKey = this?.aliases?.[key].find(alias => {
262+
const desc = descs[alias];
263+
return typeof desc === 'string' && desc.length > 0;
264+
});
265+
const descFromAlias = aliasKey ? descs[aliasKey] : undefined;
266+
const desc = descs[key] ?? descFromAlias ?? '';
262267
completions.push(
263268
dashes +
264269
`${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`

test/completion.cjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,28 @@ describe('Completion', () => {
10551055
r.logs.should.include('--help:Show help');
10561056
});
10571057

1058+
it('completes options and aliases with the same description', () => {
1059+
process.env.SHELL = '/bin/zsh';
1060+
const r = checkUsage(
1061+
() =>
1062+
yargs(['./completion', '--get-yargs-completions', '-'])
1063+
.options({
1064+
foo: {describe: 'Foo option', alias: 'f', type: 'string'},
1065+
bar: {describe: 'Bar option', alias: ['b', 'B'], type: 'string'},
1066+
})
1067+
.help(false)
1068+
.version(false)
1069+
.completion().argv
1070+
);
1071+
1072+
r.logs.should.have.length(5);
1073+
r.logs.should.include('--foo:Foo option');
1074+
r.logs.should.include('-f:Foo option');
1075+
r.logs.should.include('--bar:Bar option');
1076+
r.logs.should.include('-b:Bar option');
1077+
r.logs.should.include('-B:Bar option');
1078+
});
1079+
10581080
it('replaces application variable with $0 in script', () => {
10591081
process.env.SHELL = '/bin/zsh';
10601082
const r = checkUsage(() => yargs([]).showCompletionScript(), ['ndm']);

0 commit comments

Comments
 (0)