-
Notifications
You must be signed in to change notification settings - Fork 2
/
flag.go
242 lines (216 loc) · 5.75 KB
/
flag.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package xflags
import (
"strings"
)
const (
defaultMinNArgs = 0
defaultMaxNArgs = 1
)
// Flagger is an interface that describes any type that produces a Flag.
//
// The interface is implemented by both FlagBuilder and Flag so they can often
// be used interchangeably.
type Flagger interface {
Flag() (*Flag, error)
}
// TODO: mutually exclusive flags?
// TODO: error handling modes
// TODO: support aliases
// TODO: support negated bools
// Flag describes a command line flag that may be specified on the command
// line.
//
// Programs should not create Flag directly and instead use one of the
// FlagBuilders to build one with proper error checking.
type Flag struct {
Name string
ShortName string
Usage string
ShowDefault bool
Positional bool
MinCount int
MaxCount int
Hidden bool
EnvVar string
Validate ValidateFunc
Value Value
}
// Flag implements the Flagger interface.
func (c *Flag) Flag() (*Flag, error) {
if strings.HasPrefix(c.Name, "-") {
return nil, errorf("%s: invalid flag name", c.name())
}
if c.Value == nil {
return nil, errorf("%s: value cannot be nil", c.name())
}
if len(c.ShortName) > 1 {
return nil, errorf(
"short name must be one character in length: %s",
c.ShortName,
)
}
if c.MinCount < 0 ||
c.MaxCount < 0 ||
(c.MaxCount > 0 && c.MinCount > c.MaxCount) {
return nil, errorf(
"%s: invalid NArgs: %d, %d",
c.name(),
c.MinCount,
c.MaxCount,
)
}
return c, nil
}
func (c *Flag) String() string {
if c.Positional {
return strings.ToUpper(c.Name)
}
if c.Name != "" {
return "--" + c.Name
}
if c.ShortName != "" {
return "-" + c.ShortName
}
return "unknown"
}
// name returns the name or shortname of the flag in that order of precedence.
func (c *Flag) name() string {
if c.Name != "" {
return c.Name
}
return c.ShortName
}
// Set sets the value of the command-line flag.
func (c *Flag) Set(s string) error {
if c.Validate != nil {
if err := c.Validate(s); err != nil {
return err
}
}
return c.Value.Set(s)
}
// FlagGroup is a nominal grouping of flags which affects how the flags are
// shown in help messages.
type FlagGroup struct {
Name string
Usage string
Flags []*Flag
}
type flagGroupBuilder struct {
group FlagGroup
flags []Flagger
}
func newFlagGroupBuilder(name, usage string, flags ...Flagger) *flagGroupBuilder {
c := &flagGroupBuilder{
group: FlagGroup{
Name: name,
Usage: usage,
},
}
c.append(flags...)
return c
}
func (c *flagGroupBuilder) append(flags ...Flagger) {
c.flags = append(c.flags, flags...)
}
func (c *flagGroupBuilder) FlagGroup() (*FlagGroup, error) {
group := c.group
for _, flagger := range c.flags {
flag, err := flagger.Flag()
if err != nil {
return nil, err
}
group.Flags = append(group.Flags, flag)
}
return &group, nil
}
// FlagBuilder builds a Flag which defines a command line flag for a CLI command.
// All chain methods return a pointer to the same builder.
type FlagBuilder struct {
flag Flag
}
// ShowDefault specifies that the default vlaue of this flag should be show in
// the help message.
func (c *FlagBuilder) ShowDefault() *FlagBuilder {
c.flag.ShowDefault = true
return c
}
// ShortName specifies an alternative short name for a command line flag. For
// example, a command named "foo" can be specified on the command line with
// "--foo" but may also use a short name of "f" to be specified by "-f".
func (c *FlagBuilder) ShortName(name string) *FlagBuilder {
c.flag.ShortName = name
return c
}
// Position indicates that this flag is a positional argument, and therefore has
// no "-" or "--" delimeter. You cannot specify both a positional arguments and
// subcommands.
func (c *FlagBuilder) Positional() *FlagBuilder {
c.flag.Positional = true
return c
}
// NArgs indicates how many times this flag may be specified on the command
// line. Value.Set will be called once for each instance of the flag specified
// in the command arguments.
//
// To disable min or max count checking, set their value to 0.
func (c *FlagBuilder) NArgs(min, max int) *FlagBuilder {
c.flag.MinCount = min
c.flag.MaxCount = max
return c
}
// Required is shorthand for NArgs(1, 1) and indicates that this flag must be
// specified on the command line once and only once.
func (c *FlagBuilder) Required() *FlagBuilder {
return c.NArgs(1, 1)
}
// Hidden hides the command line flag from all help messages but still allows
// the flag to be specified on the command line.
func (c *FlagBuilder) Hidden() *FlagBuilder {
c.flag.Hidden = true
return c
}
// Env allows the value of the flag to be specified with an environment variable
// if it is not specified on the command line.
func (c *FlagBuilder) Env(name string) *FlagBuilder {
c.flag.EnvVar = name
return c
}
// Validate specifies a function to validate an argument for this flag before
// it is parsed. If the function returns an error, parsing will fail with the
// same error.
func (c *FlagBuilder) Validate(f ValidateFunc) *FlagBuilder {
c.flag.Validate = f
return c
}
// Choices is a convenience method that calls Validate and sets a ValidateFunc
// that enforces that the flag value must be one of the given choices.
func (c *FlagBuilder) Choices(elems ...string) *FlagBuilder {
return c.Validate(
func(arg string) error {
for _, elem := range elems {
if arg == elem {
return nil
}
}
return errorf(
"invalid argument: \"%s\", expected one of: \"%s\"",
arg,
strings.Join(elems, "\", \""),
)
},
)
}
// Flag implements the Flagger interface and produces a new Flag.
func (c *FlagBuilder) Flag() (*Flag, error) {
flag := c.flag
return flag.Flag()
}
// Must is a helper that calls Build and panics if the error is non-nil.
func (c *FlagBuilder) Must() *Flag {
flag, err := c.Flag()
if err != nil {
panic(err)
}
return flag
}