Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: exclude positionals from default completion #1881

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/completion.ts
Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions test/completion.cjs
Expand Up @@ -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(
Expand Down