diff --git a/lib/completion.ts b/lib/completion.ts index d8d84f01c..00c8ea5f2 100644 --- a/lib/completion.ts +++ b/lib/completion.ts @@ -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__:', '')}` diff --git a/test/completion.cjs b/test/completion.cjs index ff7027121..002bff75f 100644 --- a/test/completion.cjs +++ b/test/completion.cjs @@ -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']);