Skip to content

Commit

Permalink
fix: support options/sub-commands in zsh completion
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorlinton authored and bcoe committed Feb 18, 2019
1 parent 48249a2 commit 0a96394
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion completion.zsh.hbs
Expand Up @@ -9,7 +9,7 @@ _{{app_name}}_yargs_completions()
{
local reply
local si=$IFS
IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" {{app_path}} --get-yargs-completions "$\{words[@]\}"))
IFS=$'\n' reply=($(COMP_CWORD="$((CURRENT-1))" COMP_LINE="$BUFFER" COMP_POINT="$CURSOR" {{app_path}} --get-yargs-completions "${words[@]}"))
IFS=$si
_describe 'values' reply
}
Expand Down
7 changes: 4 additions & 3 deletions lib/completion.js
Expand Up @@ -17,6 +17,7 @@ module.exports = function completion (yargs, usage, command) {
const current = args.length ? args[args.length - 1] : ''
const argv = yargs.parse(args, true)
const aliases = yargs.parsed.aliases
const parentCommands = yargs.getContext().commands

// a custom completion function can be provided
// to completion().
Expand Down Expand Up @@ -55,7 +56,7 @@ module.exports = function completion (yargs, usage, command) {
}
}

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

if (current.match(/^-/)) {
if (current.match(/^-/) || (current === '' && completions.length === 0)) {
const descs = usage.getDescriptions()
Object.keys(yargs.getOptions().key).forEach((key) => {
// If the key and its aliases aren't in 'args', add the key to 'completions'
Expand All @@ -80,7 +81,7 @@ module.exports = function completion (yargs, usage, command) {
completions.push(`--${key}`)
} else {
const desc = descs[key] || ''
completions.push(`--${key.replace(/:/g, '\\:')}:${desc}`)
completions.push(`--${key.replace(/:/g, '\\:')}:${desc.replace('__yargsString__:', '')}`)
}
}
})
Expand Down
19 changes: 18 additions & 1 deletion test/completion.js
Expand Up @@ -181,7 +181,7 @@ describe('Completion', () => {

r.logs.should.have.length(2)
r.logs.should.include('--bar:bar option')
r.logs.should.include('--help:__yargsString__:Show help')
r.logs.should.include('--help:Show help')
})

it('completes options for the correct command', () => {
Expand Down Expand Up @@ -460,4 +460,21 @@ describe('Completion', () => {
r.errors.length.should.equal(0)
r.logs.should.include('--foo:bar')
})
it('when using subcommands ensure early bailout if full command is typed (zsh)', () => {
process.env.SHELL = '/bin/zsh'
const r = checkUsage(() => {
try {
return yargs(['./completion', '--get-yargs-completions', 'dream'])
.commandDir('./fixtures/cmddir', { 'recurse': true })
.demand(1)
.strict()
.completion()
.argv
} catch (e) {
console.log(e.message)
}
})
r.errors.length.should.equal(0)
r.logs.should.include('of-memory:Dream about a specific memory')
})
})

0 comments on commit 0a96394

Please sign in to comment.