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(completion): don't show positional args choices with option choices #2148

Merged
merged 2 commits into from Mar 23, 2022
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
17 changes: 12 additions & 5 deletions lib/completion.ts
Expand Up @@ -147,7 +147,7 @@ export class Completion implements CompletionInstance {
if (this.previousArgHasChoices(args)) {
const choices = this.getPreviousArgChoices(args);
if (choices && choices.length > 0) {
completions.push(...choices);
completions.push(...choices.map(c => c.replace(/:/g, '\\:')));
}
}
}
Expand All @@ -158,24 +158,31 @@ export class Completion implements CompletionInstance {
argv: Arguments,
current: string
) {
if (
current === '' &&
completions.length > 0 &&
this.previousArgHasChoices(args)
) {
return;
}

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed since this is a redundant slice.


const positionalKey = positionalKeys[positionalValues.length - 1];

const positionalKey = positionalKeys[argv._.length - offset - 1];
if (!positionalKey) {
return;
}

const choices = this.yargs.getOptions().choices[positionalKey] || [];
for (const choice of choices) {
if (choice.startsWith(current)) {
completions.push(choice);
completions.push(choice.replace(/:/g, '\\:'));
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/completion.cjs
Expand Up @@ -426,6 +426,36 @@ describe('Completion', () => {
r.logs.should.include('--amount');
});

it('options choices should not be display with positional choices', () => {
process.env.SHELL = '/bin/bash';
const r = checkUsage(
() =>
yargs([
...firstArguments,
'./completion',
'cmd',
'apple',
'--foo',
'',
])
.help(false)
.version(false)
.command('cmd [fruit]', 'command', subYargs => {
subYargs
.positional('fruit', {choices: ['apple', 'banana', 'pear']})
.options('foo', {
choices: ['bar', 'buz'],
})
.options({amount: {describe: 'amount', type: 'number'}});
}).argv
);

r.logs.should.have.length(2);
r.logs.should.include('bar');
r.logs.should.include('buz');
r.logs.should.not.include('apple');
});

it('completes choices for positional with prefix', () => {
process.env.SHELL = '/bin/bash';
const r = checkUsage(
Expand Down