Skip to content

Commit

Permalink
fix: getCompletion() was not working for options (#1495)
Browse files Browse the repository at this point in the history
  • Loading branch information
mleguen authored and bcoe committed Nov 27, 2019
1 parent 10ce5df commit 463feb2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/completion.js
Expand Up @@ -8,6 +8,11 @@ module.exports = function completion (yargs, usage, command) {
completionKey: 'get-yargs-completions'
}

let aliases
self.setParsed = function setParsed (parsed) {
aliases = parsed.aliases
}

const zshShell = (process.env.SHELL && process.env.SHELL.indexOf('zsh') !== -1) ||
(process.env.ZSH_NAME && process.env.ZSH_NAME.indexOf('zsh') !== -1)
// get a list of completion commands.
Expand All @@ -16,7 +21,6 @@ module.exports = function completion (yargs, usage, command) {
const completions = []
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
Expand Down
17 changes: 16 additions & 1 deletion test/completion.js
Expand Up @@ -358,10 +358,25 @@ describe('Completion', () => {
})
})
})

r.logs.should.include('apple')
r.logs.should.include('foo')
})
it('returns default completion to callback for options', () => {
process.env.SHELL = '/bin/bash'
const r = checkUsage(() => {
yargs()
.option('apple')
.option('foo')
.completion()
.getCompletion(['$0', '-'], (completions) => {
;(completions || []).forEach((completion) => {
console.log(completion)
})
})
})
r.logs.should.include('--apple')
r.logs.should.include('--foo')
})
})
})

Expand Down
1 change: 1 addition & 0 deletions yargs.js
Expand Up @@ -581,6 +581,7 @@ function Yargs (processArgs, cwd, parentRequire) {
if (parseFn) exitProcess = false

const parsed = self._parseArgs(args, shortCircuit)
completion.setParsed(self.parsed)
if (parseFn) parseFn(exitError, parsed, output)
unfreeze()

Expand Down

0 comments on commit 463feb2

Please sign in to comment.