Skip to content

Commit

Permalink
completion for positional parameters with options
Browse files Browse the repository at this point in the history
  • Loading branch information
eforks committed Jan 3, 2022
1 parent aa2d6d0 commit c195db4
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/completion.ts
Expand Up @@ -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(
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
119 changes: 119 additions & 0 deletions test/completion.cjs
Expand Up @@ -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);
});
});
}
});
Expand Down

0 comments on commit c195db4

Please sign in to comment.