Skip to content

Commit 0a96394

Browse files
trevorlintonbcoe
authored andcommittedFeb 18, 2019
fix: support options/sub-commands in zsh completion
1 parent 48249a2 commit 0a96394

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed
 

‎completion.zsh.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ _{{app_name}}_yargs_completions()
99
{
1010
local reply
1111
local si=$IFS
12-
IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" {{app_path}} --get-yargs-completions "$\{words[@]\}"))
12+
IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" {{app_path}} --get-yargs-completions "${words[@]}"))
1313
IFS=$si
1414
_describe 'values' reply
1515
}

‎lib/completion.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = function completion (yargs, usage, command) {
1717
const current = args.length ? args[args.length - 1] : ''
1818
const argv = yargs.parse(args, true)
1919
const aliases = yargs.parsed.aliases
20+
const parentCommands = yargs.getContext().commands
2021

2122
// a custom completion function can be provided
2223
// to completion().
@@ -55,7 +56,7 @@ module.exports = function completion (yargs, usage, command) {
5556
}
5657
}
5758

58-
if (!current.match(/^-/)) {
59+
if (!current.match(/^-/) && parentCommands[parentCommands.length - 1] !== current) {
5960
usage.getCommands().forEach((usageCommand) => {
6061
const commandName = command.parseCommand(usageCommand[0]).cmd
6162
if (args.indexOf(commandName) === -1) {
@@ -69,7 +70,7 @@ module.exports = function completion (yargs, usage, command) {
6970
})
7071
}
7172

72-
if (current.match(/^-/)) {
73+
if (current.match(/^-/) || (current === '' && completions.length === 0)) {
7374
const descs = usage.getDescriptions()
7475
Object.keys(yargs.getOptions().key).forEach((key) => {
7576
// If the key and its aliases aren't in 'args', add the key to 'completions'
@@ -80,7 +81,7 @@ module.exports = function completion (yargs, usage, command) {
8081
completions.push(`--${key}`)
8182
} else {
8283
const desc = descs[key] || ''
83-
completions.push(`--${key.replace(/:/g, '\\:')}:${desc}`)
84+
completions.push(`--${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`)
8485
}
8586
}
8687
})

‎test/completion.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe('Completion', () => {
181181

182182
r.logs.should.have.length(2)
183183
r.logs.should.include('--bar:bar option')
184-
r.logs.should.include('--help:__yargsString__:Show help')
184+
r.logs.should.include('--help:Show help')
185185
})
186186

187187
it('completes options for the correct command', () => {
@@ -460,4 +460,21 @@ describe('Completion', () => {
460460
r.errors.length.should.equal(0)
461461
r.logs.should.include('--foo:bar')
462462
})
463+
it('when using subcommands ensure early bailout if full command is typed (zsh)', () => {
464+
process.env.SHELL = '/bin/zsh'
465+
const r = checkUsage(() => {
466+
try {
467+
return yargs(['./completion', '--get-yargs-completions', 'dream'])
468+
.commandDir('./fixtures/cmddir', { 'recurse': true })
469+
.demand(1)
470+
.strict()
471+
.completion()
472+
.argv
473+
} catch (e) {
474+
console.log(e.message)
475+
}
476+
})
477+
r.errors.length.should.equal(0)
478+
r.logs.should.include('of-memory:Dream about a specific memory')
479+
})
463480
})

0 commit comments

Comments
 (0)
Please sign in to comment.