Fix multiple flag options completion
diff --git a/completion.go b/completion.go
index 708fa9e..6a0432b 100644
--- a/completion.go
+++ b/completion.go
@@ -120,6 +120,9 @@
}
func (c *completion) completeValue(value reflect.Value, prefix string, match string) []Completion {
+ if value.Kind() == reflect.Slice {
+ value = reflect.New(value.Type().Elem())
+ }
i := value.Interface()
var ret []Completion
@@ -139,16 +142,6 @@
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{""}
@@ -263,7 +256,7 @@
}
} else if len(s.positional) > 0 {
// Complete for positional argument
- ret = c.completeArg(s.positional[0], "", lastarg)
+ ret = c.completeValue(s.positional[0].value, "", 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 72ee158..af81993 100644
--- a/completion_test.go
+++ b/completion_test.go
@@ -52,8 +52,13 @@
Positional struct {
Filename []Filename
} `positional-args:"yes"`
+ Extra []Filename `short:"f"`
} `command:"add-multi" description:"add multiple items"`
+ AddMultiCommandFlag struct {
+ Files []Filename `short:"f"`
+ } `command:"add-multi-flag" description:"add multiple items via flags"`
+
RemoveCommand struct {
Other bool `short:"o"`
File Filename `short:"f" long:"filename"`
@@ -122,7 +127,7 @@
{
// Commands
[]string{""},
- []string{"add", "add-multi", "rename", "rm"},
+ []string{"add", "add-multi", "add-multi-flag", "rename", "rm"},
false,
},
@@ -130,10 +135,11 @@
// Commands with descriptions
[]string{""},
[]string{
- "add # add an item",
- "add-multi # add multiple items",
- "rename # rename an item",
- "rm # remove an item",
+ "add # add an item",
+ "add-multi # add multiple items",
+ "add-multi-flag # add multiple items via flags",
+ "rename # rename an item",
+ "rm # remove an item",
},
true,
},
@@ -219,6 +225,12 @@
[]string{"hello universe"},
false,
},
+ {
+ // Multiple flag filename
+ []string{"add-multi-flag", "-f", filepath.Join(completionTestSourcedir, "completion")},
+ completionTestFilename,
+ false,
+ },
}
}