mirror of
https://github.com/spf13/cobra
synced 2025-04-27 09:07:19 +00:00
Address comments
Signed-off-by: Marc Khouzam <marc.khouzam@gmail.com>
This commit is contained in:
parent
65d3afe0bf
commit
8c1b26b9f4
2 changed files with 8 additions and 8 deletions
|
@ -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)...)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Reference in a new issue