-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag.go
59 lines (51 loc) · 977 Bytes
/
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
package go_mode_flag
import (
"flag"
"fmt"
"strings"
)
// Flag is a custom flag type
type Flag struct {
value string
allowed []string
}
// NewFlag creates a new Flag
func NewFlag(value string, allowed []string) *Flag {
return &Flag{
value,
allowed,
}
}
// String returns the string representation of the flag value
func (f *Flag) String() string {
return f.value
}
// Value returns the flag value
func (f *Flag) Value() string {
return f.value
}
// Allowed returns the allowed values
func (f *Flag) Allowed() []string {
return f.allowed
}
// Set validates and sets the flag value
func (f *Flag) Set(value string) error {
for _, v := range f.allowed {
if value == v {
f.value = value
return nil
}
}
return fmt.Errorf(
"invalid value %q, allowed values are: %s", value,
strings.Join(f.allowed, ", "),
)
}
// SetFlag sets the mode flag
func SetFlag(value flag.Value, name string, usage string) {
flag.Var(
value,
name,
usage,
)
}