Skip to content

Commit

Permalink
Improve completions for arguments that can accept multiple options
Browse files Browse the repository at this point in the history
  • Loading branch information
bdhess committed Jul 9, 2017
1 parent 5c20116 commit ccc9c5d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
28 changes: 17 additions & 11 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type cmdMixin struct {
func (c *cmdMixin) CmdCompletion(context *ParseContext) completion {
var options completion

// Count args already satisfied - we won't complete those, and add any
// default commands' alternatives, since they weren't listed explicitly
// and the user may want to explicitly list something else.
// Count args already satisfied - we won't complete those unless they consume
// the remainder. Also, add any default commands' alternatives, since they
// weren't listed explicitly, and the user may want to explicitly list something else.
argsSatisfied := 0
for _, el := range context.Elements {
switch {
Expand All @@ -35,16 +35,22 @@ func (c *cmdMixin) CmdCompletion(context *ParseContext) completion {

if argsSatisfied < len(c.argGroup.args) {
// Since not all args have been satisfied, show options for the current one
options = mergeCompletions(options, c.argGroup.args[argsSatisfied].resolveCompletions())
} else {
// If all args are satisfied, then go back to completing commands
for _, cmd := range c.cmdGroup.commandOrder {
if !cmd.hidden {
options.words = append(options.words, cmd.name)
}
}
return mergeCompletions(options, c.argGroup.args[argsSatisfied].resolveCompletions())
}

// If all args are satisfied, check to see if the last consumes the remainder, and if
// so, return its completions
lastArgIdx := len(c.argGroup.args) - 1
if argsSatisfied > 0 && c.argGroup.args[lastArgIdx].consumesRemainder() {
return mergeCompletions(options, c.argGroup.args[lastArgIdx].resolveCompletions())
}

// Otherwise, go back to completing commands
for _, cmd := range c.cmdGroup.commandOrder {
if !cmd.hidden {
options.words = append(options.words, cmd.name)
}
}
return options
}

Expand Down
18 changes: 18 additions & 0 deletions cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,21 @@ func TestMixArgWithCommand(t *testing.T) {
assert.Equal(t, "cmd", selected)
assert.Equal(t, "c", *arg2)
}

func TestMultiArgCmdCompletion(t *testing.T) {
app := newTestApp()
cmd := app.Command("cmd", "")
cmd.Arg("arg", "").HintOptions("opt1", "opt2", "opt3").Strings()

result := complete(t, app, "cmd")
assert.Empty(t, result.flags)
assert.Equal(t, []string{"opt1", "opt2", "opt3"}, result.words)

result = complete(t, app, "cmd", "opt1")
assert.Empty(t, result.flags)
assert.Equal(t, []string{"opt1", "opt2", "opt3"}, result.words)

result = complete(t, app, "cmd", "opt3", "opt2")
assert.Empty(t, result.flags)
assert.Equal(t, []string{"opt1", "opt2", "opt3"}, result.words)
}

0 comments on commit ccc9c5d

Please sign in to comment.