diff --git a/completions.go b/completions.go index 6b8ed8c5..0862d3f6 100644 --- a/completions.go +++ b/completions.go @@ -270,10 +270,10 @@ func (c *Command) initCompleteCmd(args []string) { } } -// sliceValue is a reduced version of [pflag.SliceValue]. It is used to detect +// SliceValue is a reduced version of [pflag.SliceValue]. It is used to detect // flags that accept multiple values and therefore can provide completion // multiple times. -type sliceValue interface { +type SliceValue interface { // GetSlice returns the flag value list as an array of strings. GetSlice() []string } @@ -407,13 +407,13 @@ func (c *Command) getCompletions(args []string) (*Command, []string, ShellCompDi // If we have not found any required flags, only then can we show regular flags if len(completions) == 0 { doCompleteFlags := func(flag *pflag.Flag) { - _, acceptsMultiple := flag.Value.(sliceValue) - - if !flag.Changed || - acceptsMultiple || + _, acceptsMultiple := flag.Value.(SliceValue) + acceptsMultiple = acceptsMultiple || strings.Contains(flag.Value.Type(), "Slice") || strings.Contains(flag.Value.Type(), "Array") || - strings.HasPrefix(flag.Value.Type(), "stringTo") { + strings.HasPrefix(flag.Value.Type(), "stringTo") + + if !flag.Changed || acceptsMultiple { // If the flag is not already present, or if it can be specified multiple times (Array, Slice, or stringTo) // we suggest it as a completion completions = append(completions, getFlagNameCompletions(flag, toComplete)...) diff --git a/completions_test.go b/completions_test.go index 6e959bab..a8f378eb 100644 --- a/completions_test.go +++ b/completions_test.go @@ -675,7 +675,7 @@ func TestFlagNameCompletionInGoWithDesc(t *testing.T) { // but does not include "Slice" or "Array" in its "Type" string. type customMultiString []string -var _ sliceValue = (*customMultiString)(nil) +var _ SliceValue = (*customMultiString)(nil) func (s *customMultiString) String() string { return fmt.Sprintf("%v", *s)