diff --git a/cli/parse.go b/cli/parse.go index ae74d987..eebd6b43 100644 --- a/cli/parse.go +++ b/cli/parse.go @@ -100,7 +100,7 @@ func setOpts(kv kv, kvType reflect.Kind, opts cmds.OptMap) error { if kvType == cmds.Strings { res, _ := opts[kv.Key].([]string) - opts[kv.Key] = append(res, kv.Value.(string)) + opts[kv.Key] = append(res, kv.Value.([]string)...) } else if _, exists := opts[kv.Key]; !exists { opts[kv.Key] = kv.Value } else { diff --git a/option.go b/option.go index 68962bc4..dcf4e0b1 100644 --- a/option.go +++ b/option.go @@ -184,6 +184,38 @@ func FloatOption(names ...string) Option { func StringOption(names ...string) Option { return NewOption(String, names...) } + +// StringsOption is a command option that takes which returns a slice of strings. When parsing func StringsOption(names ...string) Option { - return NewOption(Strings, names...) + return DelimitedStringsOption("", names...) +} + +func DelimitedStringsOption(delimiter string, names ...string) Option { + 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.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 }