Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: exclude positionals from default completion (#1881)
  • Loading branch information
jacobator committed Mar 8, 2021
1 parent 36abf26 commit 0175677
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
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

0 comments on commit 0175677

Please sign in to comment.