-
Notifications
You must be signed in to change notification settings - Fork 0
/
flag.go
49 lines (42 loc) · 854 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
package go_mode_flag
import (
"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: value,
allowed: 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, ", "),
)
}