-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
StringsOption with delimiter #204
Changes from all commits
382fd11
cde56b1
d48b43d
16d8195
e2c3e22
c6690cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,6 +149,18 @@ func NewOption(kind reflect.Kind, names ...string) Option { | |
} | ||
|
||
func (o *option) WithDefault(v interface{}) Option { | ||
if v == nil { | ||
panic(fmt.Errorf("cannot use nil as a default")) | ||
} | ||
Comment on lines
+152
to
+154
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not seeing any problem with this ATM, it seems like so far all of our types are concrete and even for the pointer types we do have (i.e. []string) as long as people pass in |
||
|
||
// if type of value does not match the option type | ||
if vKind, oKind := reflect.TypeOf(v).Kind(), o.Type(); vKind != oKind { | ||
aschmahmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// if the reason they do not match is not because of Slice vs Array equivalence | ||
// Note: Figuring out if the type of Slice/Array matches is not done in this function | ||
if !((vKind == reflect.Array || vKind == reflect.Slice) && (oKind == reflect.Array || oKind == reflect.Slice)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed, but basically is just a hack for StringsOption so that it can still opaquely call this function and everything works. Doesn't seem too unreasonable though. |
||
panic(fmt.Errorf("invalid default for the given type, expected %s got %s", o.Type(), vKind)) | ||
} | ||
} | ||
o.defaultVal = v | ||
return o | ||
} | ||
|
@@ -184,6 +196,50 @@ func FloatOption(names ...string) Option { | |
func StringOption(names ...string) Option { | ||
return NewOption(String, names...) | ||
} | ||
|
||
// StringsOption is a command option that can handle a slice of strings | ||
func StringsOption(names ...string) Option { | ||
return NewOption(Strings, names...) | ||
return &stringsOption{ | ||
Option: NewOption(Strings, names...), | ||
delimiter: "", | ||
} | ||
} | ||
|
||
// DelimitedStringsOption like StringsOption is a command option that can handle a slice of strings. | ||
// However, DelimitedStringsOption will automatically break up the associated CLI inputs based on the delimiter. | ||
// For example, instead of passing `command --option=val1 --option=val2` you can pass `command --option=val1,val2` or | ||
// even `command --option=val1,val2 --option=val3,val4`. | ||
// | ||
// A delimiter of "" is invalid | ||
func DelimitedStringsOption(delimiter string, names ...string) Option { | ||
aschmahmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if delimiter == "" { | ||
panic("cannot create a DelimitedStringsOption with no delimiter") | ||
} | ||
return &stringsOption{ | ||
Option: NewOption(Strings, names...), | ||
delimiter: delimiter, | ||
} | ||
} | ||
|
||
type stringsOption struct { | ||
Option | ||
delimiter string | ||
} | ||
|
||
func (s *stringsOption) WithDefault(v interface{}) Option { | ||
if v == nil { | ||
return s.Option.WithDefault(v) | ||
} | ||
|
||
defVal := v.([]string) | ||
s.Option = s.Option.WithDefault(defVal) | ||
return s | ||
} | ||
|
||
func (s *stringsOption) Parse(v string) (interface{}, error) { | ||
if s.delimiter == "" { | ||
return []string{v}, nil | ||
} | ||
|
||
return strings.Split(v, s.delimiter), nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was just extracted from the function below