-
Notifications
You must be signed in to change notification settings - Fork 4
/
string_slice_uniq.go
49 lines (39 loc) · 1.09 KB
/
string_slice_uniq.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 conflag
type stringSliceUniqValue struct {
*stringSliceValue
}
func newStringSliceUniqValue(val []string, p *[]string) *stringSliceUniqValue {
return &stringSliceUniqValue{stringSliceValue: newStringSliceValue(val, p)}
}
func (s *stringSliceUniqValue) Set(val string) error {
if !s.changed {
*s.value = []string{val}
s.changed = true
}
dup := false
for _, v := range *s.value {
if v == val {
dup = true
}
}
if !dup {
*s.value = append(*s.value, val)
}
return nil
}
func (s *stringSliceUniqValue) Type() string {
return "stringSliceUniq"
}
func (s *stringSliceUniqValue) String() string {
return ""
}
// StringSliceUniqVar works like StringSliceVar but the items in slice are unique.
func (c *Conflag) StringSliceUniqVar(p *[]string, name string, value []string, usage string) {
c.Var(newStringSliceUniqValue(value, p), name, usage)
}
// StringSliceUniq works like StringSlice but the items in slice are unique.
func (c *Conflag) StringSliceUniq(name string, value []string, usage string) *[]string {
p := []string{}
c.StringSliceUniqVar(&p, name, value, usage)
return &p
}