Factor out completeArg
This factors a new function, completeArg which completes
a positional argument correctly depending on whether it
is a positional argument for remaining args.
diff --git a/completion.go b/completion.go
index 663ecfc..fea0e3a 100644
--- a/completion.go
+++ b/completion.go
@@ -121,13 +121,7 @@
return n
}
-func (c *completion) completeValue(value reflect.Value, prefix string, match string, isRemaining bool) []Completion {
- // For remaining positional args (that are parsed into a slice), complete
- // based on the element type.
- if isRemaining {
- value = reflect.New(value.Type().Elem())
- }
-
+func (c *completion) completeValue(value reflect.Value, prefix string, match string) []Completion {
i := value.Interface()
var ret []Completion
@@ -147,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{""}
@@ -229,7 +233,7 @@
if opt != nil {
// Completion for the argument of 'opt'
- ret = c.completeValue(opt.value, "", lastarg, false)
+ ret = c.completeValue(opt.value, "", lastarg)
} else if argumentIsOption(lastarg) {
// Complete the option
prefix, optname, islong := stripOptionPrefix(lastarg)
@@ -240,7 +244,7 @@
sname := string(rname)
if opt := s.lookup.shortNames[sname]; opt != nil && opt.canArgument() {
- ret = c.completeValue(opt.value, prefix+sname, optname[n:], false)
+ ret = c.completeValue(opt.value, prefix+sname, optname[n:])
} else {
ret = c.completeShortNames(s, prefix, optname)
}
@@ -252,7 +256,7 @@
}
if opt != nil {
- ret = c.completeValue(opt.value, prefix+optname+split, *argument, false)
+ ret = c.completeValue(opt.value, prefix+optname+split, *argument)
}
} else if islong {
ret = c.completeLongNames(s, prefix, optname)
@@ -261,7 +265,7 @@
}
} else if len(s.positional) > 0 {
// Complete for positional argument
- ret = c.completeValue(s.positional[0].value, "", lastarg, s.positional[0].isRemaining())
+ ret = c.completeArg(s.positional[0], "", lastarg)
} else if len(s.command.commands) > 0 {
// Complete for command
ret = c.completeCommands(s, lastarg)