forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
58 lines (51 loc) · 990 Bytes
/
flags.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
package cfgfile
import (
"flag"
"strings"
)
type argList struct {
list []string
isDefault bool
f *flag.Flag
}
func flagArgList(name string, def string, usage string) *argList {
l := &argList{
list: []string{def},
isDefault: true,
}
flag.Var(l, name, usage)
l.f = flag.Lookup(name)
if l.f == nil {
panic("Failed to lookup registered flag")
}
return l
}
func (l *argList) SetDefault(v string) {
l.f.DefValue = v
// Only update value if we are still in the default
if l.isDefault {
l.list = []string{v}
l.isDefault = true
}
}
func (l *argList) String() string {
return strings.Join(l.list, ", ")
}
func (l *argList) Set(v string) error {
if l.isDefault {
l.list = []string{v}
} else {
// Ignore duplicates, can be caused by multiple flag parses
for _, f := range l.list {
if f == v {
return nil
}
}
l.list = append(l.list, v)
}
l.isDefault = false
return nil
}
func (l *argList) Get() interface{} {
return l.list
}