diff --git a/lib/completion.ts b/lib/completion.ts index 4b850a2d9..472270617 100644 --- a/lib/completion.ts +++ b/lib/completion.ts @@ -32,6 +32,7 @@ export class Completion implements CompletionInstance { private aliases: DetailedArguments['aliases'] | null = null; private customCompletionFunction: CompletionFunction | null = null; + private indexAfterLastReset = 0; private readonly zshShell: boolean; constructor( @@ -57,6 +58,7 @@ export class Completion implements CompletionInstance { if (handlers[args[i]] && handlers[args[i]].builder) { const builder = handlers[args[i]].builder; if (isCommandBuilderCallback(builder)) { + this.indexAfterLastReset = i + 1; const y = this.yargs.getInternalMethods().reset(); builder(y, true); return y.argv; @@ -146,6 +148,27 @@ export class Completion implements CompletionInstance { completions.push(...choices); } } + + const positionalKeys = + this.yargs.getGroups()[this.usage.getPositionalGroupName()] || []; + const offset = Math.max( + this.indexAfterLastReset, + this.yargs.getInternalMethods().getContext().commands.length + + /* name of the script is first param */ 1 + ); + const positionalValues = argv._.slice(offset); + + const positionalKey = positionalKeys[positionalValues.length - 1]; + + if (!positionalKey) { + return; + } + const choices = this.yargs.getOptions().choices[positionalKey] || []; + for (const choice of choices) { + if (choice.startsWith(current)) { + completions.push(choice); + } + } } private getPreviousArgChoices(args: string[]): string[] | void { diff --git a/test/completion.cjs b/test/completion.cjs index 69d8c8e0f..498c57073 100644 --- a/test/completion.cjs +++ b/test/completion.cjs @@ -401,6 +401,125 @@ describe('Completion', () => { r.logs.should.not.include('banana'); r.logs.should.not.include('pear'); }); + + it('completes choices for first positional', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage( + () => + yargs([...firstArguments, './completion', 'cmd', '']) + .help(false) + .version(false) + .command('cmd [fruit] [fruit2]', 'command', subYargs => { + subYargs + .positional('fruit', {choices: ['apple', 'banana', 'pear']}) + .positional('fruit2', { + choices: ['apple2', 'banana2', 'pear2'], + }) + .options({amount: {describe: 'amount', type: 'number'}}); + }).argv + ); + + r.logs.should.have.length(4); + r.logs.should.include('apple'); + r.logs.should.include('banana'); + r.logs.should.include('pear'); + r.logs.should.include('--amount'); + }); + + it('completes choices for positional with prefix', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage( + () => + yargs([...firstArguments, './completion', 'cmd', 'a']) + .help(false) + .version(false) + .command('cmd [fruit] [fruit2]', 'command', subYargs => { + subYargs + .positional('fruit', { + choices: ['apple1', 'banana1', 'pear1'], + }) + .positional('fruit2', { + choices: ['apple2', 'banana2', 'pear2'], + }) + .options({amount: {describe: 'amount', type: 'number'}}); + }).argv + ); + + r.logs.should.have.length(1); + r.logs.should.include('apple1'); + }); + + it('completes choices for second positional after option', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage( + () => + yargs([ + ...firstArguments, + './completion', + 'cmd', + 'apple', + '--amount', + '1', + '', + ]) + .help(false) + .version(false) + .command('cmd [fruit] [fruit2]', 'command', subYargs => { + subYargs + .positional('fruit', { + choices: ['apple1', 'banana1', 'pear1'], + }) + .positional('fruit2', { + choices: ['apple2', 'banana2', 'pear2'], + }) + .options({amount: {describe: 'amount', type: 'number'}}); + }).argv + ); + + r.logs.should.have.length(3); + r.logs.should.include('apple2'); + r.logs.should.include('banana2'); + r.logs.should.include('pear2'); + }); + + it('completes choices for nested command', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage( + () => + yargs([...firstArguments, './completion', 'wrapper', 'cmd', '']) + .help(false) + .version(false) + .command({ + command: 'wrapper', + builder: subYargs => + subYargs.command('cmd [fruit]', 'command', subYargs2 => { + subYargs2.positional('fruit', {choices: ['apple']}); + }), + }).argv + ); + + r.logs.should.have.length(1); + r.logs.should.include('apple'); + }); + + it('works if positional has no choices', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage( + () => + yargs([...firstArguments, './completion', 'wrapper', 'cmd', 'a']) + .help(false) + .version(false) + .command({ + command: 'wrapper', + builder: subYargs => + subYargs.command('cmd [fruit]', 'command', subYargs2 => { + subYargs2.positional('fruit', {}); + }), + }).argv + ); + + r.logs.should.have.length(0); + }); }); } });