Skip to content

Commit

Permalink
Overwrite slice flag defaults when set
Browse files Browse the repository at this point in the history
Closes #160
  • Loading branch information
meatballhat committed Apr 5, 2016
1 parent 71f57d3 commit 867aa09
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 53 deletions.
4 changes: 2 additions & 2 deletions altsrc/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (f *StringSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputS
return err
}
if value != nil {
var sliceValue cli.StringSlice = value
var sliceValue cli.StringSlice = *(cli.NewStringSlice(value...))
eachName(f.Name, func(name string) {
underlyingFlag := f.set.Lookup(f.Name)
if underlyingFlag != nil {
Expand Down Expand Up @@ -163,7 +163,7 @@ func (f *IntSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour
return err
}
if value != nil {
var sliceValue cli.IntSlice = value
var sliceValue cli.IntSlice = *(cli.NewIntSlice(value...))
eachName(f.Name, func(name string) {
underlyingFlag := f.set.Lookup(f.Name)
if underlyingFlag != nil {
Expand Down
71 changes: 54 additions & 17 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,26 +111,39 @@ func (f GenericFlag) GetName() string {
return f.Name
}

// StringSlice is an opaque type for []string to satisfy flag.Value
type StringSlice []string
// StringSlice wraps a []string to satisfy flag.Value
type StringSlice struct {
slice []string
hasBeenSet bool
}

// NewStringSlice creates a *StringSlice with default values
func NewStringSlice(defaults ...string) *StringSlice {
return &StringSlice{slice: append([]string{}, defaults...)}
}

// Set appends the string value to the list of values
func (f *StringSlice) Set(value string) error {
*f = append(*f, value)
if !f.hasBeenSet {
f.slice = []string{}
f.hasBeenSet = true
}

f.slice = append(f.slice, value)
return nil
}

// String returns a readable representation of this value (for usage defaults)
func (f *StringSlice) String() string {
return fmt.Sprintf("%s", *f)
return fmt.Sprintf("%s", f.slice)
}

// Value returns the slice of strings set by this flag
func (f *StringSlice) Value() []string {
return *f
return f.slice
}

// StringSlice is a string flag that can be specified multiple times on the
// StringSliceFlag is a string flag that can be specified multiple times on the
// command-line
type StringSliceFlag struct {
Name string
Expand Down Expand Up @@ -163,10 +176,11 @@ func (f StringSliceFlag) Apply(set *flag.FlagSet) {
}
}

if f.Value == nil {
f.Value = &StringSlice{}
}

eachName(f.Name, func(name string) {
if f.Value == nil {
f.Value = &StringSlice{}
}
set.Var(f.Value, name, f.Usage)
})
}
Expand All @@ -175,28 +189,51 @@ func (f StringSliceFlag) GetName() string {
return f.Name
}

// StringSlice is an opaque type for []int to satisfy flag.Value
type IntSlice []int
// IntSlice wraps an []int to satisfy flag.Value
type IntSlice struct {
slice []int
hasBeenSet bool
}

// NewIntSlice makes an *IntSlice with default values
func NewIntSlice(defaults ...int) *IntSlice {
return &IntSlice{slice: append([]int{}, defaults...)}
}

// SetInt directly adds an integer to the list of values
func (i *IntSlice) SetInt(value int) {
if !i.hasBeenSet {
i.slice = []int{}
i.hasBeenSet = true
}

i.slice = append(i.slice, value)
}

// Set parses the value into an integer and appends it to the list of values
func (f *IntSlice) Set(value string) error {
func (i *IntSlice) Set(value string) error {
if !i.hasBeenSet {
i.slice = []int{}
i.hasBeenSet = true
}

tmp, err := strconv.Atoi(value)
if err != nil {
return err
} else {
*f = append(*f, tmp)
i.slice = append(i.slice, tmp)
}
return nil
}

// String returns a readable representation of this value (for usage defaults)
func (f *IntSlice) String() string {
return fmt.Sprintf("%d", *f)
func (i *IntSlice) String() string {
return fmt.Sprintf("%v", i.slice)
}

// Value returns the slice of ints set by this flag
func (f *IntSlice) Value() []int {
return *f
func (i *IntSlice) Value() []int {
return i.slice
}

// IntSliceFlag is an int flag that can be specified multiple times on the
Expand Down
Loading

0 comments on commit 867aa09

Please sign in to comment.