-
Notifications
You must be signed in to change notification settings - Fork 16
/
string_slice.go
43 lines (36 loc) · 975 Bytes
/
string_slice.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
package goflags
import (
sliceutil "github.com/projectdiscovery/utils/slice"
)
var (
optionMap map[*StringSlice]Options
optionDefaultValues map[*StringSlice][]string
)
func init() {
optionMap = make(map[*StringSlice]Options)
optionDefaultValues = make(map[*StringSlice][]string)
}
// StringSlice is a slice of strings
type StringSlice []string
// Set appends a value to the string slice.
func (stringSlice *StringSlice) Set(value string) error {
option, ok := optionMap[stringSlice]
if !ok {
option = StringSliceOptions
}
values, err := ToStringSlice(value, option)
if err != nil {
return err
}
// if new values are provided, we remove default ones
if defaultValue, ok := optionDefaultValues[stringSlice]; ok {
if sliceutil.Equal(*stringSlice, defaultValue) {
*stringSlice = []string{}
}
}
*stringSlice = append(*stringSlice, values...)
return nil
}
func (stringSlice StringSlice) String() string {
return ToString(stringSlice)
}