diff --git a/lib/completion.ts b/lib/completion.ts index 1a9075696..19e7ab7c4 100644 --- a/lib/completion.ts +++ b/lib/completion.ts @@ -105,13 +105,20 @@ export class Completion implements CompletionInstance { ) { if (current.match(/^-/) || (current === '' && completions.length === 0)) { const options = this.yargs.getOptions(); + const positionalKeys = + this.yargs.getGroups()[this.usage.getPositionalGroupName()] || []; + Object.keys(options.key).forEach(key => { const negable = !!options.configuration['boolean-negation'] && options.boolean.includes(key); + const isPositionalKey = positionalKeys.includes(key); - // If the key and its aliases aren't in 'args', add the key to 'completions' - if (!this.argsContainKey(args, argv, key, negable)) { + // If the key is not positional and its aliases aren't in 'args', add the key to 'completions' + if ( + !isPositionalKey && + !this.argsContainKey(args, argv, key, negable) + ) { this.completeOptionKey(key, completions, current); if (negable && !!options.default[key]) this.completeOptionKey(`no-${key}`, completions, current); diff --git a/test/completion.cjs b/test/completion.cjs index 10b90661c..f99d9b0b5 100644 --- a/test/completion.cjs +++ b/test/completion.cjs @@ -214,6 +214,29 @@ describe('Completion', () => { r.logs.should.include('--opt2'); }); + it('ignores positionals for the correct command', () => { + process.env.SHELL = '/bin/bash'; + const r = checkUsage( + () => + yargs(['./completion', '--get-yargs-completions', 'cmd', '--o']) + .help(false) + .version(false) + .command('cmd', 'command', subYargs => { + subYargs + .options({ + opt: { + describe: 'option', + }, + }) + .positional('pos-opt', {type: 'string'}); + }).argv + ); + + r.logs.should.have.length(1); + r.logs.should.include('--opt'); + r.logs.should.not.include('--pos-opt'); + }); + it('does not complete hidden commands', () => { process.env.SHELL = '/bin/bash'; const r = checkUsage(