Skip to content

Commit

Permalink
fix: completion should play well with strip-dashed
Browse files Browse the repository at this point in the history
  • Loading branch information
AgentEnder committed May 9, 2023
1 parent f9ed905 commit e762ac2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
22 changes: 16 additions & 6 deletions lib/yargs-factory.ts
Expand Up @@ -2051,8 +2051,18 @@ export class YargsInstance {
this.#isGlobalContext = false;

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

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

const skipRecommendation =
helpOptSet || requestCompletionsArg || helpOnly;
if (argv._.length) {
if (handlerKeys.length) {
let firstUnknownCommand;
Expand Down Expand Up @@ -2105,7 +2115,7 @@ export class YargsInstance {
if (
this.#completionCommand &&
argv._.includes(this.#completionCommand) &&
!requestCompletions
!requestCompletionsArg
) {
if (this.#exitProcess) setBlocking(true);
this.showCompletionScript();
Expand All @@ -2132,14 +2142,14 @@ export class YargsInstance {

// we must run completions first, a user might
// want to complete the --help or --version option.
if (requestCompletions) {
if (requestCompletionsArg) {
if (this.#exitProcess) setBlocking(true);

// we allow for asynchronous completions,
// e.g., loading in a list of commands from an API.
args = ([] as string[]).concat(args);
const completionArgs = args.slice(
args.indexOf(`--${this.#completion!.completionKey}`) + 1
args.indexOf(`--${requestCompletionsArg}`) + 1
);
this.#completion!.getCompletion(completionArgs, (err, completions) => {
if (err) throw new YError(err.message);
Expand Down Expand Up @@ -2189,7 +2199,7 @@ export class YargsInstance {

// if we're executed via bash completion, don't
// bother with validation.
if (!requestCompletions) {
if (!requestCompletionsArg) {
const validation = this[kRunValidation](aliases, {}, parsed.error);
if (!calledFromCommand) {
argvPromise = applyMiddleware(
Expand Down
27 changes: 17 additions & 10 deletions test/completion.cjs
Expand Up @@ -1212,17 +1212,24 @@ describe('Completion', () => {
});

describe('parser-configuration', () => {
it('should support strip-dashed', () => {
process.env.SHELL = '/bin/bash';
const configurations = [
{'strip-dashed': true},
{'camel-case-expansion': true, 'strip-aliased': true},
];

const r = checkUsage(
() =>
yargs(['--get-yargs-completions', 'a'])
.parserConfiguration({'strip-dashed': true})
.command('apple', 'banana').argv
);
for (const configuration of configurations) {
it(`should support ${Object.keys(configuration).join(' ')}`, () => {
process.env.SHELL = '/bin/bash';

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

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

0 comments on commit e762ac2

Please sign in to comment.