diff options
| author | Eric Paris <[email protected]> | 2015-08-12 10:19:24 -0500 |
|---|---|---|
| committer | Eric Paris <[email protected]> | 2015-08-12 10:19:24 -0500 |
| commit | 4869ec2ae0628354eaac5bf88fccf9a7265ae475 (patch) | |
| tree | a512d39ec0822c7a5ff3b0960b0b16894f2df007 /string_slice.go | |
| parent | 978c009ee41c0f14bd0c0e2927ae81713cc65864 (diff) | |
| parent | 95a6a40798df11df014298af8a8250af515fa753 (diff) | |
Merge pull request #41 from eparis/fix-slice-defaults
Do not append to default values in {String,Int}Slice
Diffstat (limited to 'string_slice.go')
| -rw-r--r-- | string_slice.go | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/string_slice.go b/string_slice.go index 2037bd8..848bea8 100644 --- a/string_slice.go +++ b/string_slice.go @@ -1,27 +1,41 @@ package pflag import ( + "fmt" "strings" ) +var _ = fmt.Fprint + // -- stringSlice Value -type stringSliceValue []string +type stringSliceValue struct { + value *[]string + changed bool +} func newStringSliceValue(val []string, p *[]string) *stringSliceValue { - *p = val - return (*stringSliceValue)(p) + ssv := new(stringSliceValue) + ssv.value = p + *ssv.value = val + return ssv } func (s *stringSliceValue) Set(val string) error { v := strings.Split(val, ",") - *s = append(*s, v...) + if !s.changed { + *s.value = v + } else { + *s.value = append(*s.value, v...) + } + s.changed = true return nil } + func (s *stringSliceValue) Type() string { return "stringSlice" } -func (s *stringSliceValue) String() string { return "[" + strings.Join(*s, ",") + "]" } +func (s *stringSliceValue) String() string { return "[" + strings.Join(*s.value, ",") + "]" } func stringSliceConv(sval string) (interface{}, error) { sval = strings.Trim(sval, "[]") |
