Merge branch 'complete-positional-rest-args'
diff --git a/completion.go b/completion.go
index 2f730d9..fea0e3a 100644
--- a/completion.go
+++ b/completion.go
@@ -141,6 +141,16 @@
return ret
}
+func (c *completion) completeArg(arg *Arg, prefix string, match string) []Completion {
+ if arg.isRemaining() {
+ // For remaining positional args (that are parsed into a slice), complete
+ // based on the element type.
+ return c.completeValue(reflect.New(arg.value.Type().Elem()), prefix, match)
+ }
+
+ return c.completeValue(arg.value, prefix, match)
+}
+
func (c *completion) complete(args []string) []Completion {
if len(args) == 0 {
args = []string{""}
@@ -205,7 +215,11 @@
}
} else {
if len(s.positional) > 0 {
- s.positional = s.positional[1:]
+ if !s.positional[0].isRemaining() {
+ // Don't advance beyond a remaining positional arg (because
+ // it consumes all subsequent args).
+ s.positional = s.positional[1:]
+ }
} else if cmd, ok := s.lookup.commands[arg]; ok {
cmd.fillParseState(s)
}
@@ -251,7 +265,7 @@
}
} else if len(s.positional) > 0 {
// Complete for positional argument
- ret = c.completeValue(s.positional[0].value, "", lastarg)
+ ret = c.completeArg(s.positional[0], "", lastarg)
} else if len(s.command.commands) > 0 {
// Complete for command
ret = c.completeCommands(s, lastarg)
diff --git a/completion_test.go b/completion_test.go
index f51949e..908e421 100644
--- a/completion_test.go
+++ b/completion_test.go
@@ -43,6 +43,12 @@
} `positional-args:"yes"`
} `command:"add"`
+ AddMultiCommand struct {
+ Positional struct {
+ Filename []Filename
+ } `positional-args:"yes"`
+ } `command:"add-multi"`
+
RemoveCommand struct {
Other bool `short:"o"`
File Filename `short:"f" long:"filename"`
@@ -90,7 +96,7 @@
{
// Commands
[]string{""},
- []string{"add", "rename", "rm"},
+ []string{"add", "add-multi", "rename", "rm"},
},
{
@@ -106,6 +112,22 @@
},
{
+ // Multiple positional filename (1 arg)
+ []string{"add-multi", filepath.Join(sourcedir, "completion")},
+ excompl,
+ },
+ {
+ // Multiple positional filename (2 args)
+ []string{"add-multi", filepath.Join(sourcedir, "completion.go"), filepath.Join(sourcedir, "completion")},
+ excompl,
+ },
+ {
+ // Multiple positional filename (3 args)
+ []string{"add-multi", filepath.Join(sourcedir, "completion.go"), filepath.Join(sourcedir, "completion.go"), filepath.Join(sourcedir, "completion")},
+ excompl,
+ },
+
+ {
// Flag filename
[]string{"rm", "-f", path.Join(sourcedir, "completion")},
excompl,
diff --git a/flags.go b/flags.go
index 85f124d..923379b 100644
--- a/flags.go
+++ b/flags.go
@@ -231,6 +231,7 @@
Customized completion for argument values is supported by implementing
the flags.Completer interface for the argument value type. An example
of a type which does so is the flags.Filename type, an alias of string
-allowing simple filename completion.
+allowing simple filename completion. A slice or array argument value
+whose element type implements flags.Completer will also be completed.
*/
package flags