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 10, 2023
1 parent f9ed905 commit 095fdd8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
11 changes: 10 additions & 1 deletion lib/yargs-factory.ts
Original file line number Diff line number Diff line change
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
27 changes: 17 additions & 10 deletions test/completion.cjs
Original file line number Diff line number Diff line change
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 095fdd8

Please sign in to comment.