aboutsummaryrefslogtreecommitdiff
path: root/int_slice.go
diff options
context:
space:
mode:
authorEric Paris <[email protected]>2015-08-12 10:54:26 -0400
committerEric Paris <[email protected]>2015-08-12 11:06:13 -0400
commit95a6a40798df11df014298af8a8250af515fa753 (patch)
treea06e3ca530e046d0b72d55ffa06db19d75a34bb0 /int_slice.go
parent41e9136667ee4f9ffd03da380e24629d7eccace4 (diff)
Do not append to default values in {String,Int}Slice
I added the ability to do: `-s=bob -s=john` and get `[]string{"bob", "john"}` But if a default value was set to say `[]string{"eric"}` the above operation was mistakenly resulting in: `[]string{"eric", "bob", "john"} which was obviously not what was intended. This is fixed by tracking if a value was parsed and overwriting the default on the first time -s is found, but we append the 2+ time.
Diffstat (limited to 'int_slice.go')
-rw-r--r--int_slice.go22
1 files changed, 16 insertions, 6 deletions
diff --git a/int_slice.go b/int_slice.go
index b28353d..7c6ed79 100644
--- a/int_slice.go
+++ b/int_slice.go
@@ -7,11 +7,16 @@ import (
)
// -- intSlice Value
-type intSliceValue []int
+type intSliceValue struct {
+ value *[]int
+ changed bool
+}
func newIntSliceValue(val []int, p *[]int) *intSliceValue {
- *p = val
- return (*intSliceValue)(p)
+ isv := new(intSliceValue)
+ isv.value = p
+ *isv.value = val
+ return isv
}
func (s *intSliceValue) Set(val string) error {
@@ -25,7 +30,12 @@ func (s *intSliceValue) Set(val string) error {
}
}
- *s = append(*s, out...)
+ if !s.changed {
+ *s.value = out
+ } else {
+ *s.value = append(*s.value, out...)
+ }
+ s.changed = true
return nil
}
@@ -34,8 +44,8 @@ func (s *intSliceValue) Type() string {
}
func (s *intSliceValue) String() string {
- out := make([]string, len(*s))
- for i, d := range *s {
+ out := make([]string, len(*s.value))
+ for i, d := range *s.value {
out[i] = fmt.Sprintf("%d", d)
}
return "[" + strings.Join(out, ",") + "]"