-
Notifications
You must be signed in to change notification settings - Fork 11
/
flag.go
75 lines (63 loc) · 1.42 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
package exhaustive
import (
"flag"
"regexp"
"strings"
)
var _ flag.Value = (*regexpFlag)(nil)
var _ flag.Value = (*stringsFlag)(nil)
// regexpFlag implements flag.Value for parsing
// regular expression flag inputs.
type regexpFlag struct{ re *regexp.Regexp }
func (f *regexpFlag) String() string {
if f == nil || f.re == nil {
return ""
}
return f.re.String()
}
func (f *regexpFlag) Set(expr string) error {
if expr == "" {
f.re = nil
return nil
}
re, err := regexp.Compile(expr)
if err != nil {
return err
}
f.re = re
return nil
}
// stringsFlag implements flag.Value for parsing a comma-separated string
// list. Surrounding whitespace is stripped from the input and from each
// element. If filter is non-nil it is called for each element in the input.
type stringsFlag struct {
elements []string
filter func(string) error
}
func (f *stringsFlag) String() string {
if f == nil {
return ""
}
return strings.Join(f.elements, ",")
}
func (f *stringsFlag) filterFunc() func(string) error {
if f.filter != nil {
return f.filter
}
return func(_ string) error { return nil }
}
func (f *stringsFlag) Set(input string) error {
input = strings.TrimSpace(input)
if input == "" {
f.elements = nil
return nil
}
for _, el := range strings.Split(input, ",") {
el = strings.TrimSpace(el)
if err := f.filterFunc()(el); err != nil {
return err
}
f.elements = append(f.elements, el)
}
return nil
}