From 4c14674ea72704ad08db469d8f5bfcc77afe2196 Mon Sep 17 00:00:00 2001 From: Masaharu TASHIRO Date: Sat, 19 Nov 2022 00:50:16 +0900 Subject: [PATCH 1/4] fix: add a description of alias on the zsh completion --- lib/completion.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/completion.ts b/lib/completion.ts index d8d84f01c..eb4cae93c 100644 --- a/lib/completion.ts +++ b/lib/completion.ts @@ -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]; + const descFromAlias = aliasKey ? descs[aliasKey] : undefined; + const desc = descs[key] ?? descFromAlias ?? ''; completions.push( dashes + `${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}` From 8a322530510f1f3e7f3e5db7d6a7e54d98599f97 Mon Sep 17 00:00:00 2001 From: Masaharu TASHIRO Date: Sat, 19 Nov 2022 00:51:36 +0900 Subject: [PATCH 2/4] test: add a test --- test/completion.cjs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/completion.cjs b/test/completion.cjs index ff7027121..26a940e19 100644 --- a/test/completion.cjs +++ b/test/completion.cjs @@ -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']); From ad77a624725d547951dc7840240936708a1c26a0 Mon Sep 17 00:00:00 2001 From: Masaharu TASHIRO Date: Thu, 1 Dec 2022 13:45:54 +0700 Subject: [PATCH 3/4] fix: make the process finding desc from alias be a loop --- lib/completion.ts | 5 ++++- test/completion.cjs | 5 +++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/completion.ts b/lib/completion.ts index eb4cae93c..00c8ea5f2 100644 --- a/lib/completion.ts +++ b/lib/completion.ts @@ -258,7 +258,10 @@ export class Completion implements CompletionInstance { if (!this.zshShell) { completions.push(dashes + key); } else { - const aliasKey = this?.aliases?.[key]?.[0]; + 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( diff --git a/test/completion.cjs b/test/completion.cjs index 26a940e19..7f5cfa300 100644 --- a/test/completion.cjs +++ b/test/completion.cjs @@ -1062,18 +1062,19 @@ describe('Completion', () => { yargs(['./completion', '--get-yargs-completions', '-']) .options({ foo: {describe: 'Foo option', alias: 'f', type: 'string'}, - bar: {describe: 'Bar option', alias: 'b', type: 'string'}, + bar: {describe: 'Bar option', alias: ['b', 'B'], type: 'string'}, }) .help(false) .version(false) .completion().argv ); - r.logs.should.have.length(4); + 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', () => { From 7751740bdac5d06def6fec757866f44d9d781ef2 Mon Sep 17 00:00:00 2001 From: Masaharu TASHIRO Date: Wed, 28 Dec 2022 00:40:06 +0900 Subject: [PATCH 4/4] test: fix the title of the test --- test/completion.cjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/completion.cjs b/test/completion.cjs index 7f5cfa300..002bff75f 100644 --- a/test/completion.cjs +++ b/test/completion.cjs @@ -1055,7 +1055,7 @@ describe('Completion', () => { r.logs.should.include('--help:Show help'); }); - it('completes options and alias with the same description', () => { + it('completes options and aliases with the same description', () => { process.env.SHELL = '/bin/zsh'; const r = checkUsage( () =>