Skip to content

Commit

Permalink
fix: exclude positional arguments from completion output (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored and bcoe committed Aug 22, 2017
1 parent a40cbc9 commit 71c7ec7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/completion.js
Expand Up @@ -55,9 +55,10 @@ module.exports = function completion (yargs, usage, command) {
}

if (!current.match(/^-/)) {
usage.getCommands().forEach((command) => {
if (args.indexOf(command[0]) === -1) {
completions.push(command[0])
usage.getCommands().forEach((usageCommand) => {
const commandName = command.parseCommand(usageCommand[0]).cmd
if (args.indexOf(commandName) === -1) {
completions.push(commandName)
}
})
}
Expand Down
14 changes: 14 additions & 0 deletions test/completion.js
Expand Up @@ -121,6 +121,20 @@ describe('Completion', () => {
r.logs.should.include('cmd1')
})

it('does not include possitional arguments', function () {
var r = checkUsage(function () {
return yargs(['./completion', '--get-yargs-completions', 'cmd'])
.command('cmd1 [arg]', 'first command')
.command('cmd2 <arg>', 'second command')
.completion('completion', false)
.argv
})

r.logs.should.have.length(2)
r.logs.should.include('cmd1')
r.logs.should.include('cmd2')
})

it('works if command has no options', () => {
const r = checkUsage(() => yargs(['./completion', '--get-yargs-completions', 'foo', '--b'])
.help(false)
Expand Down

0 comments on commit 71c7ec7

Please sign in to comment.