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: parser-configuration should work well with generated completion script #2332

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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: 10 additions & 1 deletion lib/yargs-factory.ts
Expand Up @@ -2051,7 +2051,16 @@ export class YargsInstance {
this.#isGlobalContext = false;

const handlerKeys = this.#command.getCommands();
const requestCompletions = this.#completion!.completionKey in argv;

const requestCompletions = this.#completion?.completionKey
? [
this.#completion?.completionKey,
...(this.getAliases()[this.#completion?.completionKey] ?? []),
].some((key: string) =>
Object.prototype.hasOwnProperty.call(argv, key)
)
: false;

const skipRecommendation = helpOptSet || requestCompletions || helpOnly;
if (argv._.length) {
if (handlerKeys.length) {
Expand Down
22 changes: 22 additions & 0 deletions test/completion.cjs
Expand Up @@ -1210,4 +1210,26 @@ describe('Completion', () => {
});
});
});

describe('parser-configuration', () => {
const configurations = [
{'strip-dashed': true},
{'camel-case-expansion': true, 'strip-aliased': true},
];

for (const configuration of configurations) {
it(`should support ${Object.keys(configuration).join(' ')}`, () => {
process.env.SHELL = '/bin/bash';

const r = checkUsage(
() =>
yargs(['--get-yargs-completions', 'a'])
.parserConfiguration(configuration)
.command('apple', 'banana').argv
);

r.logs.should.include('apple');
});
}
});
});