forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflags.go
More file actions
122 lines (101 loc) · 2.87 KB
/
flags.go
File metadata and controls
122 lines (101 loc) · 2.87 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package cmdutil
import (
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
// NilStringFlag defines a new flag with a string pointer receiver. This is useful for differentiating
// between the flag being set to a blank value and the flag not being passed at all.
func NilStringFlag(cmd *cobra.Command, p **string, name string, shorthand string, usage string) *pflag.Flag {
return cmd.Flags().VarPF(newStringValue(p), name, shorthand, usage)
}
// NilBoolFlag defines a new flag with a bool pointer receiver. This is useful for differentiating
// between the flag being explicitly set to a false value and the flag not being passed at all.
func NilBoolFlag(cmd *cobra.Command, p **bool, name string, shorthand string, usage string) *pflag.Flag {
f := cmd.Flags().VarPF(newBoolValue(p), name, shorthand, usage)
f.NoOptDefVal = "true"
return f
}
// StringEnumFlag defines a new string flag that only allows values listed in options.
func StringEnumFlag(cmd *cobra.Command, p *string, name, shorthand, defaultValue string, options []string, usage string) *pflag.Flag {
*p = defaultValue
val := &enumValue{string: p, options: options}
f := cmd.Flags().VarPF(val, name, shorthand, fmt.Sprintf("%s: %s", usage, formatValuesForUsageDocs(options)))
_ = cmd.RegisterFlagCompletionFunc(name, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return options, cobra.ShellCompDirectiveNoFileComp
})
return f
}
func formatValuesForUsageDocs(values []string) string {
return fmt.Sprintf("{%s}", strings.Join(values, "|"))
}
type stringValue struct {
string **string
}
func newStringValue(p **string) *stringValue {
return &stringValue{p}
}
func (s *stringValue) Set(value string) error {
*s.string = &value
return nil
}
func (s *stringValue) String() string {
if s.string == nil || *s.string == nil {
return ""
}
return **s.string
}
func (s *stringValue) Type() string {
return "string"
}
type boolValue struct {
bool **bool
}
func newBoolValue(p **bool) *boolValue {
return &boolValue{p}
}
func (b *boolValue) Set(value string) error {
v, err := strconv.ParseBool(value)
*b.bool = &v
return err
}
func (b *boolValue) String() string {
if b.bool == nil || *b.bool == nil {
return "false"
} else if **b.bool {
return "true"
}
return "false"
}
func (b *boolValue) Type() string {
return "bool"
}
func (b *boolValue) IsBoolFlag() bool {
return true
}
type enumValue struct {
string *string
options []string
}
func (e *enumValue) Set(value string) error {
found := false
for _, opt := range e.options {
if strings.EqualFold(opt, value) {
found = true
break
}
}
if !found {
return fmt.Errorf("valid values are %s", formatValuesForUsageDocs(e.options))
}
*e.string = value
return nil
}
func (e *enumValue) String() string {
return *e.string
}
func (e *enumValue) Type() string {
return "string"
}