From 463feb2870158eb9df670222b0f0a40a05cf18d0 Mon Sep 17 00:00:00 2001 From: Mael Le Guen Date: Wed, 27 Nov 2019 20:08:01 +0100 Subject: [PATCH] fix: getCompletion() was not working for options (#1495) --- lib/completion.js | 6 +++++- test/completion.js | 17 ++++++++++++++++- yargs.js | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/completion.js b/lib/completion.js index 3f3bf16e1..8c2363e3d 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -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. @@ -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 diff --git a/test/completion.js b/test/completion.js index 4324fc5ca..a2036af83 100644 --- a/test/completion.js +++ b/test/completion.js @@ -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') + }) }) }) diff --git a/yargs.js b/yargs.js index e7b412ae2..9ccb3041a 100644 --- a/yargs.js +++ b/yargs.js @@ -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()