diff options
Diffstat (limited to 'multi_string_flag.go')
-rw-r--r-- | multi_string_flag.go | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/multi_string_flag.go b/multi_string_flag.go index 699529a0..ab22b14d 100644 --- a/multi_string_flag.go +++ b/multi_string_flag.go @@ -7,15 +7,24 @@ import ( var errMultiStringSetEmptyValue = errors.New("value cannot be empty") +const defaultSeparator = "," + // MultiStringFlag implements the flag.Value interface and allows a string flag // to be specified multiple times on the command line. // // e.g.: -listen-http 127.0.0.1:80 -listen-http [::1]:80 -type MultiStringFlag []string +type MultiStringFlag struct { + value []string + separator string +} // String returns the list of parameters joined with a commas (",") func (s *MultiStringFlag) String() string { - return strings.Join(*s, ",") + if s.separator == "" { + s.separator = defaultSeparator + } + + return strings.Join(s.value, s.separator) } // Set appends the value to the list of parameters @@ -23,14 +32,19 @@ func (s *MultiStringFlag) Set(value string) error { if value == "" { return errMultiStringSetEmptyValue } - *s = append(*s, value) + + s.value = append(s.value, value) return nil } // Split each flag func (s *MultiStringFlag) Split() (result []string) { - for _, str := range *s { - result = append(result, strings.Split(str, ",")...) + if s.separator == "" { + s.separator = defaultSeparator + } + + for _, str := range s.value { + result = append(result, strings.Split(str, s.separator)...) } return |