Skip to content

Commit

Permalink
Update slice flags
Browse files Browse the repository at this point in the history
- Add check for blank default value
- Reduce indentation of code
  • Loading branch information
asahasrabuddhe committed Jan 27, 2020
1 parent 809a71d commit 0f5ca21
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 42 deletions.
31 changes: 18 additions & 13 deletions flag_int64_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,33 @@ func (c *Context) GlobalInt64Slice(name string) []int64 {
func lookupInt64Slice(name string, set *flag.FlagSet) []int64 {
f := set.Lookup(name)
if f != nil {
if value, ok := f.Value.(*Int64Slice); ok {
// extract the slice from asserted value
parsed := value.Value()
value, ok := f.Value.(*Int64Slice)
if !ok {
return nil
}

// extract the slice from asserted value
parsed := value.Value()

// extract default value from the flag
var defaultVal []int64
for _, v := range strings.Split(f.DefValue, ",") {
// extract default value from the flag
var defaultVal []int64
for _, v := range strings.Split(f.DefValue, ",") {
if v != "" {
int64Value, err := strconv.ParseInt(v, 10, 64)
if err != nil {
panic(err)
}
defaultVal = append(defaultVal, int64Value)
}
// if the current value is not equal to the default value
// remove the default values from the flag
if !isInt64SliceEqual(parsed, defaultVal) {
for _, v := range defaultVal {
parsed = removeFromInt64Slice(parsed, v)
}
}
// if the current value is not equal to the default value
// remove the default values from the flag
if !isInt64SliceEqual(parsed, defaultVal) {
for _, v := range defaultVal {
parsed = removeFromInt64Slice(parsed, v)
}
return parsed
}
return parsed
}
return nil
}
Expand Down
30 changes: 17 additions & 13 deletions flag_int_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,28 +137,32 @@ func (c *Context) GlobalIntSlice(name string) []int {
func lookupIntSlice(name string, set *flag.FlagSet) []int {
f := set.Lookup(name)
if f != nil {
if value, ok := f.Value.(*IntSlice); ok {
// extract the slice from asserted value
slice := value.Value()
value, ok := f.Value.(*IntSlice)
if !ok {
return nil
}
// extract the slice from asserted value
slice := value.Value()

// extract default value from the flag
var defaultVal []int
for _, v := range strings.Split(f.DefValue, ",") {
// extract default value from the flag
var defaultVal []int
for _, v := range strings.Split(f.DefValue, ",") {
if v != "" {
intValue, err := strconv.Atoi(v)
if err != nil {
panic(err)
}
defaultVal = append(defaultVal, intValue)
}
// if the current value is not equal to the default value
// remove the default values from the flag
if !isIntSliceEqual(slice, defaultVal) {
for _, v := range defaultVal {
slice = removeFromIntSlice(slice, v)
}
}
// if the current value is not equal to the default value
// remove the default values from the flag
if !isIntSliceEqual(slice, defaultVal) {
for _, v := range defaultVal {
slice = removeFromIntSlice(slice, v)
}
return slice
}
return slice
}
return nil
}
Expand Down
34 changes: 18 additions & 16 deletions flag_string_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,27 @@ func (c *Context) GlobalStringSlice(name string) []string {
func lookupStringSlice(name string, set *flag.FlagSet) []string {
f := set.Lookup(name)
if f != nil {
if value, ok := f.Value.(*StringSlice); ok {
// extract the slice from asserted value
slice := value.Value()

// extract default value from the flag
var defaultVal []string
for _, v := range strings.Split(f.DefValue, ",") {
defaultVal = append(defaultVal, v)
}
value, ok := f.Value.(*StringSlice)
if !ok {
return nil
}
// extract the slice from asserted value
slice := value.Value()

// extract default value from the flag
var defaultVal []string
for _, v := range strings.Split(f.DefValue, ",") {
defaultVal = append(defaultVal, v)
}

// if the current value is not equal to the default value
// remove the default values from the flag
if !isStringSliceEqual(slice, defaultVal) {
for _, v := range defaultVal {
slice = removeFromStringSlice(slice, v)
}
// if the current value is not equal to the default value
// remove the default values from the flag
if !isStringSliceEqual(slice, defaultVal) {
for _, v := range defaultVal {
slice = removeFromStringSlice(slice, v)
}
return slice
}
return slice
}
return nil
}
Expand Down

0 comments on commit 0f5ca21

Please sign in to comment.