Skip to content

Commit 888db19

Browse files
authored
fix: parser-configuration should work well with generated completion script (#2332)
1 parent 3a40a78 commit 888db19

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

lib/yargs-factory.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,16 @@ export class YargsInstance {
20582058
this.#isGlobalContext = false;
20592059

20602060
const handlerKeys = this.#command.getCommands();
2061-
const requestCompletions = this.#completion!.completionKey in argv;
2061+
2062+
const requestCompletions = this.#completion?.completionKey
2063+
? [
2064+
this.#completion?.completionKey,
2065+
...(this.getAliases()[this.#completion?.completionKey] ?? []),
2066+
].some((key: string) =>
2067+
Object.prototype.hasOwnProperty.call(argv, key)
2068+
)
2069+
: false;
2070+
20622071
const skipRecommendation = helpOptSet || requestCompletions || helpOnly;
20632072
if (argv._.length) {
20642073
if (handlerKeys.length) {

test/completion.mjs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1189,4 +1189,26 @@ describe('Completion', () => {
11891189
});
11901190
});
11911191
});
1192+
1193+
describe('parser-configuration', () => {
1194+
const configurations = [
1195+
{'strip-dashed': true},
1196+
{'camel-case-expansion': true, 'strip-aliased': true},
1197+
];
1198+
1199+
for (const configuration of configurations) {
1200+
it(`should support ${Object.keys(configuration).join(' ')}`, () => {
1201+
process.env.SHELL = '/bin/bash';
1202+
1203+
const r = checkOutput(
1204+
() =>
1205+
yargs(['--get-yargs-completions', 'a'])
1206+
.parserConfiguration(configuration)
1207+
.command('apple', 'banana').argv
1208+
);
1209+
1210+
r.logs.should.include('apple');
1211+
});
1212+
}
1213+
});
11921214
});

0 commit comments

Comments
 (0)