Skip to content

Commit

Permalink
Reverse the application of the Trimming methods
Browse files Browse the repository at this point in the history
  • Loading branch information
xitonix committed Jul 19, 2019
1 parent c827a35 commit 5092e07
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 51 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func main() {

// You have full control over the acceptable values for each flag. You can either provide
// a list of allowed values using WithValidRange method:
weekend := flags.StringSlice("weekends", "Weekends").WithValidRange(true, "Sat, Sun").WithTrimming()
weekend := flags.StringSlice("weekends", "Weekends").WithValidRange(true, "Sat, Sun")

// or set a fully customisable validator using WithValidationCallback method:
numRange := flags.Int8("number", "A flag with validation callback").
Expand Down
8 changes: 4 additions & 4 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,10 @@ func (b *Bucket) Time(longName, usage string) *core.TimeFlag {
//
// A custom delimiter string can be defined using WithDelimiter() method.
//
// You can also trim the leading and trailing white spaces from each list item by enabling the feature
// using WithTrimming() method. With trimming enabled, --weekends "Sat, Sun" will be parsed into
// {"Sat", "Sun"} instead of {"Sat", " Sun"}.
// Notice that the leading white space before " Sun" has been removed.
// By default, the leading and trailing white spaces will be automatically trimmed from each list item
// With trimming enabled, --weekends "Sat, Sun" will be parsed into
// {"Sat", "Sun"} instead of {"Sat", " Sun"}. Notice that the leading white space before " Sun" has been removed.
// Trimming can be disabled by calling the DisableTrimming() method.
func (b *Bucket) StringSlice(longName, usage string) *core.StringSliceFlag {
f := core.NewStringSlice(longName, usage)
b.flags = append(b.flags, f)
Expand Down
12 changes: 12 additions & 0 deletions core/string_map_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import (
//
// The value of a string map flag can be set using map initialisation literals.
// For example --mappings "key1:value1, key2:value2"
//
// By default, the leading and trailing white spaces will be automatically trimmed from each key/value pairs.
// With trimming enabled, "key1 : value1 , key2: value2 " will be parsed into
// {"key1", "value1", "key2":"value2"} instead of {"key1 ", " value1 ", " key2":" value2 "}.
// Notice that all the leading/trailing white space characters have been removed from all the keys and the values.
// Trimming can be disabled by calling the DisableKeyTrimming(), DisableValueTrimming() methods.
type StringMapFlag struct {
key *Key
defaultValue, value map[string]string
Expand Down Expand Up @@ -178,6 +184,12 @@ func (f *StringMapFlag) WithValidationCallback(validate func(key, value string)
//
// The value of a string map flag can be set using map initialisation literals.
// For example --mappings "key1:value1, key2:value2"
//
// By default, the leading and trailing white spaces will be automatically trimmed from each key/value pairs.
// With trimming enabled, "key1 : value1 , key2: value2 " will be parsed into
// {"key1", "value1", "key2":"value2"} instead of {"key1 ", " value1 ", " key2":" value2 "}.
// Notice that all the leading/trailing white space characters have been removed from all the keys and the values.
// Trimming can be disabled by calling the DisableKeyTrimming(), DisableValueTrimming() methods.
func (f *StringMapFlag) Set(value string) error {
if internal.IsEmpty(value) {
f.set(make(map[string]string))
Expand Down
33 changes: 17 additions & 16 deletions core/string_slice_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
//
// A custom delimiter string can be defined using WithDelimiter() method.
//
// You can also trim the leading and trailing white spaces from each list item by enabling the feature
// using WithTrimming() method. With trimming enabled, --weekends "Sat, Sun" will be parsed into
// {"Sat", "Sun"} instead of {"Sat", " Sun"}.
// Notice that the leading white space before " Sun" has been removed.
// By default, the leading and trailing white spaces will be automatically trimmed from each list item
// With trimming enabled, --weekends "Sat, Sun" will be parsed into
// {"Sat", "Sun"} instead of {"Sat", " Sun"}. Notice that the leading white space before " Sun" has been removed.
// Trimming can be disabled by calling the DisableTrimming() method.
type StringSliceFlag struct {
key *Key
defaultValue, value []string
Expand All @@ -39,11 +39,12 @@ type StringSliceFlag struct {
// NewStringSlice creates a new string slice flag.
func NewStringSlice(name, usage string) *StringSliceFlag {
f := &StringSliceFlag{
key: &Key{},
long: internal.SanitiseLongName(name),
usage: usage,
ptr: new([]string),
delimiter: DefaultDelimiter,
key: &Key{},
long: internal.SanitiseLongName(name),
usage: usage,
ptr: new([]string),
delimiter: DefaultDelimiter,
trimSpaces: true,
}
f.set(make([]string, 0))
return f
Expand Down Expand Up @@ -182,9 +183,9 @@ func (f *StringSliceFlag) WithDelimiter(delimiter string) *StringSliceFlag {
return f
}

// WithTrimming enables trimming the leading and trailing white space characters from each list item.
func (f *StringSliceFlag) WithTrimming() *StringSliceFlag {
f.trimSpaces = true
// DisableTrimming disables trimming the leading and trailing white space characters from each list item.
func (f *StringSliceFlag) DisableTrimming() *StringSliceFlag {
f.trimSpaces = false
return f
}

Expand Down Expand Up @@ -230,10 +231,10 @@ func (f *StringSliceFlag) WithValidRange(ignoreCase bool, valid ...string) *Stri
//
// A custom delimiter string can be defined using WithDelimiter() method.
//
// You can also trim the leading and trailing white spaces from each list item by enabling the feature
// using WithTrimming() method. With trimming enabled, --weekends "Sat, Sun" will be parsed into
// {"Sat", "Sun"} instead of {"Sat", " Sun"}.
// Notice that the leading white space before " Sun" has been removed.
// By default, the leading and trailing white spaces will be automatically trimmed from each list item
// With trimming enabled, --weekends "Sat, Sun" will be parsed into
// {"Sat", "Sun"} instead of {"Sat", " Sun"}. Notice that the leading white space before " Sun" has been removed.
// Trimming can be disabled by calling the DisableTrimming() method.
func (f *StringSliceFlag) Set(value string) error {
var parts []string
if len(value) == 0 {
Expand Down
66 changes: 40 additions & 26 deletions core/string_slice_flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,44 +349,44 @@ func TestStringSliceFlag_WithDelimiter(t *testing.T) {
}
}

func TestStringSliceFlag_WithTrimming(t *testing.T) {
func TestStringSliceFlag_DisableTrimming(t *testing.T) {
testCases := []struct {
title string
value string
enableTrimming bool
expectedValue []string
title string
value string
disableTrimming bool
expectedValue []string
}{
{
title: "without trimming",
enableTrimming: false,
value: " abc , xyz ",
expectedValue: []string{" abc ", " xyz "},
title: "without trimming",
disableTrimming: true,
value: " abc , xyz ",
expectedValue: []string{" abc ", " xyz "},
},
{
title: "with trimming",
enableTrimming: true,
value: " abc , xyz ",
expectedValue: []string{"abc", "xyz"},
title: "with trimming",
disableTrimming: false,
value: " abc , xyz ",
expectedValue: []string{"abc", "xyz"},
},
{
title: "only white space input without trimming",
enableTrimming: false,
value: " ",
expectedValue: []string{" "},
title: "only white space input without trimming",
disableTrimming: true,
value: " ",
expectedValue: []string{" "},
},
{
title: "only white space input with trimming",
enableTrimming: true,
value: " , ",
expectedValue: []string{"", ""},
title: "only white space input with trimming",
disableTrimming: false,
value: " , ",
expectedValue: []string{"", ""},
},
}

for _, tc := range testCases {
t.Run(tc.title, func(t *testing.T) {
f := flags.StringSlice("long", "usage")
if tc.enableTrimming {
f = f.WithTrimming()
if tc.disableTrimming {
f = f.DisableTrimming()
}
fVar := f.Var()
err := f.Set(tc.value)
Expand All @@ -410,12 +410,12 @@ func TestStringSliceFlag_Set(t *testing.T) {
{
title: "white space value",
value: " ",
expectedValue: []string{" "},
expectedValue: []string{""},
},
{
title: "value with white space",
value: " abc ",
expectedValue: []string{" abc "},
expectedValue: []string{"abc"},
},
{
title: "value with no white space",
Expand All @@ -430,7 +430,7 @@ func TestStringSliceFlag_Set(t *testing.T) {
{
title: "comma separated value with white space",
value: " abc , efg ",
expectedValue: []string{" abc ", " efg "},
expectedValue: []string{"abc", "efg"},
},
}

Expand All @@ -456,6 +456,7 @@ func TestStringSliceFlag_Validation(t *testing.T) {
setValidationList bool
ignoreCase bool
expectedError string
disableTrimming bool
}{
{
title: "nil validation callback",
Expand Down Expand Up @@ -557,6 +558,16 @@ func TestStringSliceFlag_Validation(t *testing.T) {
setValidationList: true,
value: " ",
expectedError: "",
expectedValue: []string{""},
},
{
title: "acceptable white space value with trimming",
validationList: []string{"Green", "Red", " "},
ignoreCase: false,
setValidationList: true,
disableTrimming: true,
value: " ",
expectedError: "",
expectedValue: []string{" "},
},
{
Expand Down Expand Up @@ -624,6 +635,9 @@ func TestStringSliceFlag_Validation(t *testing.T) {
if tc.setValidationCB {
f = f.WithValidationCallback(tc.validationCB)
}
if tc.disableTrimming {
f = f.DisableTrimming()
}
if tc.setValidationList {
f = f.WithValidRange(tc.ignoreCase, tc.validationList...)
}
Expand Down
14 changes: 10 additions & 4 deletions global_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,10 @@ func Time(longName, usage string) *core.TimeFlag {
//
// A custom delimiter string can be defined using WithDelimiter() method.
//
// You can also trim the leading and trailing white spaces from each list item by enabling the feature
// using WithTrimming() method. With trimming enabled, --weekends "Sat, Sun" will be parsed into
// {"Sat", "Sun"} instead of {"Sat", " Sun"}.
// Notice that the leading white space before " Sun" has been removed.
// By default, the leading and trailing white spaces will be automatically trimmed from each list item
// With trimming enabled, --weekends "Sat, Sun" will be parsed into
// {"Sat", "Sun"} instead of {"Sat", " Sun"}. Notice that the leading white space before " Sun" has been removed.
// Trimming can be disabled by calling the DisableTrimming() method.
func StringSlice(longName, usage string) *core.StringSliceFlag {
return DefaultBucket.StringSlice(longName, usage)
}
Expand Down Expand Up @@ -475,6 +475,12 @@ func CIDRSlice(longName, usage string) *core.CIDRSliceFlag {
//
// The value of a string map flag can be set using map initialisation literals.
// For example --mappings "key1:value1, key2:value2"
//
// By default, the leading and trailing white spaces will be automatically trimmed from each key/value pairs.
// With trimming enabled, "key1 : value1 , key2: value2 " will be parsed into
// {"key1", "value1", "key2":"value2"} instead of {"key1 ", " value1 ", " key2":" value2 "}.
// Notice that all the leading/trailing white space characters have been removed from all the keys and the values.
// Trimming can be disabled by calling the DisableKeyTrimming(), DisableValueTrimming() methods.
func StringMap(longName, usage string) *core.StringMapFlag {
return DefaultBucket.StringMap(longName, usage)
}

0 comments on commit 5092e07

Please sign in to comment.