Skip to content

Commit 036e7c5

Browse files
evocateurbcoe
authored andcommitted
fix(completion): Avoid default command and recommendations during completion (#1123)
1 parent f13ebf4 commit 036e7c5

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

test/completion.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,37 @@ describe('Completion', () => {
2626
r.logs.should.include('foo')
2727
})
2828

29+
it('avoids interruption from command recommendations', () => {
30+
const r = checkUsage(() =>
31+
yargs(['./completion', '--get-yargs-completions', './completion', 'a'])
32+
.command('apple', 'fruit')
33+
.command('aardvark', 'animal')
34+
.recommendCommands()
35+
.completion()
36+
.argv
37+
)
38+
39+
r.errors.should.deep.equal([])
40+
r.logs.should.include('apple')
41+
r.logs.should.include('aardvark')
42+
})
43+
44+
it('avoids interruption from default command', () => {
45+
const r = checkUsage(() =>
46+
yargs(['./usage', '--get-yargs-completions', './usage', ''])
47+
.usage('$0 [thing]', 'skipped', subYargs => {
48+
subYargs.command('aardwolf', 'is a thing according to google')
49+
})
50+
.command('aardvark', 'animal')
51+
.completion()
52+
.argv
53+
)
54+
55+
r.errors.should.deep.equal([])
56+
r.logs.should.not.include('aardwolf')
57+
r.logs.should.include('aardvark')
58+
})
59+
2960
it('avoids repeating already included commands', () => {
3061
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'apple'])
3162
.command('foo', 'bar')

yargs.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,8 +1036,11 @@ function Yargs (processArgs, cwd, parentRequire) {
10361036
argv[helpOpt] = true
10371037
}
10381038
}
1039+
10391040
const handlerKeys = command.getCommands()
1040-
const skipDefaultCommand = argv[helpOpt] && (handlerKeys.length > 1 || handlerKeys[0] !== '$0')
1041+
const requestCompletions = completion.completionKey in argv
1042+
const skipRecommendation = argv[helpOpt] || requestCompletions
1043+
const skipDefaultCommand = skipRecommendation && (handlerKeys.length > 1 || handlerKeys[0] !== '$0')
10411044

10421045
if (argv._.length) {
10431046
if (handlerKeys.length) {
@@ -1064,13 +1067,13 @@ function Yargs (processArgs, cwd, parentRequire) {
10641067

10651068
// recommend a command if recommendCommands() has
10661069
// been enabled, and no commands were found to execute
1067-
if (recommendCommands && firstUnknownCommand && !argv[helpOpt]) {
1070+
if (recommendCommands && firstUnknownCommand && !skipRecommendation) {
10681071
validation.recommendCommands(firstUnknownCommand, handlerKeys)
10691072
}
10701073
}
10711074

10721075
// generate a completion script for adding to ~/.bashrc.
1073-
if (completionCommand && ~argv._.indexOf(completionCommand) && !argv[completion.completionKey]) {
1076+
if (completionCommand && ~argv._.indexOf(completionCommand) && !requestCompletions) {
10741077
if (exitProcess) setBlocking(true)
10751078
self.showCompletionScript()
10761079
self.exit(0)
@@ -1082,7 +1085,7 @@ function Yargs (processArgs, cwd, parentRequire) {
10821085

10831086
// we must run completions first, a user might
10841087
// want to complete the --help or --version option.
1085-
if (completion.completionKey in argv) {
1088+
if (requestCompletions) {
10861089
if (exitProcess) setBlocking(true)
10871090

10881091
// we allow for asynchronous completions,
@@ -1130,7 +1133,7 @@ function Yargs (processArgs, cwd, parentRequire) {
11301133

11311134
// if we're executed via bash completion, don't
11321135
// bother with validation.
1133-
if (!argv[completion.completionKey]) {
1136+
if (!requestCompletions) {
11341137
self._runValidation(argv, aliases, {}, parsed.error)
11351138
}
11361139
}

0 commit comments

Comments
 (0)