-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathenum.go
More file actions
45 lines (38 loc) · 791 Bytes
/
enum.go
File metadata and controls
45 lines (38 loc) · 791 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package serve
import (
"fmt"
"strings"
)
// https://github.com/spf13/pflag/issues/236#issuecomment-931600452
type enum struct {
Allowed []string
Value string
}
// newEnum give a list of allowed flag parameters, where the second argument is the default
func newEnum(allowed []string, d string) *enum {
return &enum{
Allowed: allowed,
Value: d,
}
}
func (a enum) String() string {
return a.Value
}
func (a *enum) Set(p string) error {
isIncluded := func(opts []string, val string) bool {
for _, opt := range opts {
if val == opt {
return true
}
}
return false
}
if !isIncluded(a.Allowed, p) {
return fmt.Errorf("%s is not included in %s", p, strings.Join(a.Allowed, ","))
}
a.Value = p
return nil
}
func (*enum) Type() string {
return "string"
}