-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflagvalidvalues.go
More file actions
29 lines (24 loc) · 799 Bytes
/
flagvalidvalues.go
File metadata and controls
29 lines (24 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package myflags
import (
"strings"
"github.com/spf13/cobra"
)
// validFlagValues is a helper struct used to support ValidValuesTag
type validFlagValues struct {
values []string
}
// strlist is a list of options, separated by comma
func newValidFlagValues(strlist string) *validFlagValues {
slist := strings.FieldsFunc(strlist, func(r rune) bool { return r == ',' })
rlist := []string{}
for i := range slist {
if val := strings.TrimSpace(slist[i]); val != "" {
rlist = append(rlist, val)
}
}
return &validFlagValues{values: rlist}
}
// complete implements cobra flag completion function
func (helper *validFlagValues) complete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return helper.values, cobra.ShellCompDirectiveNoFileComp
}