Skip to content

Commit 81a5db6

Browse files
authored
Merge pull request #6030 from thaJeztah/flag_multiple_completion
opts: ListOpts: implement cobra.SliceValue to fix shell completion
2 parents 11fea00 + 572e3f1 commit 81a5db6

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

opts/opts.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ func (opts *ListOpts) GetAll() []string {
8484
return *opts.values
8585
}
8686

87+
// GetSlice returns the values of slice.
88+
//
89+
// It implements [cobra.SliceValue] to allow shell completion to be provided
90+
// multiple times.
91+
//
92+
// [cobra.SliceValue]: https://pkg.go.dev/github.com/spf13/cobra@v1.9.1#SliceValue
93+
func (opts *ListOpts) GetSlice() []string {
94+
return *opts.values
95+
}
96+
8797
// GetAllOrEmpty returns the values of the slice
8898
// or an empty slice when there are no values.
8999
func (opts *ListOpts) GetAllOrEmpty() []string {

opts/opts_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ func TestMapOpts(t *testing.T) {
112112
}
113113
}
114114

115+
//nolint:gocyclo // ignore "cyclomatic complexity 17 is too high"
115116
func TestListOptsWithoutValidator(t *testing.T) {
116117
o := NewListOpts(nil)
117118
err := o.Set("foo")
@@ -145,12 +146,13 @@ func TestListOptsWithoutValidator(t *testing.T) {
145146
if o.String() != "[bar bar]" {
146147
t.Errorf("%s != [bar bar]", o.String())
147148
}
148-
listOpts := o.GetAll()
149-
if len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
149+
if listOpts := o.GetAll(); len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
150150
t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
151151
}
152-
mapListOpts := o.GetMap()
153-
if len(mapListOpts) != 1 {
152+
if listOpts := o.GetSlice(); len(listOpts) != 2 || listOpts[0] != "bar" || listOpts[1] != "bar" {
153+
t.Errorf("Expected [[bar bar]], got [%v]", listOpts)
154+
}
155+
if mapListOpts := o.GetMap(); len(mapListOpts) != 1 {
154156
t.Errorf("Expected [map[bar:{}]], got [%v]", mapListOpts)
155157
}
156158
}

0 commit comments

Comments
 (0)