Skip to content
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

Add strings option #4012

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions commands/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,16 @@ func parseOpts(args []string, root *cmds.Command) (
// parseFlag checks that a flag is valid and saves it into opts
// Returns true if the optional second argument is used
parseFlag := func(name string, arg *string, mustUse bool) (bool, error) {
if _, ok := opts[name]; ok {
return false, fmt.Errorf("Duplicate values for option '%s'", name)
}

optDef, found := optDefs[name]
if !found {
err = fmt.Errorf("Unrecognized option '%s'", name)
return false, err
}

if _, ok := opts[name]; ok && optDef.Type() != cmds.Strings {
return false, fmt.Errorf("Duplicate values for option '%s'", name)
}

// mustUse implies that you must use the argument given after the '='
// eg. -r=true means you must take true into consideration
// mustUse == true in the above case
Expand All @@ -137,13 +138,19 @@ func parseOpts(args []string, root *cmds.Command) (
default:
return true, fmt.Errorf("Option '%s' takes true/false arguments, but was passed '%s'", name, argVal)
}
} else if arg != nil && optDef.Type() == cmds.Strings {
if res, ok := opts[name].([]string); ok {
opts[name] = append(res, *arg)
} else {
opts[name] = []string{*arg}
}
} else {
if arg == nil {
return true, fmt.Errorf("Missing argument for option '%s'", name)
}
opts[name] = *arg
return true, nil
}
return true, nil
}

optDefs, err = root.GetOptions(path)
Expand Down
18 changes: 16 additions & 2 deletions commands/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
Uint = reflect.Uint
Float = reflect.Float64
String = reflect.String
Strings = reflect.Array
)

// Option is used to specify a field that will be provided by a consumer
Expand Down Expand Up @@ -53,9 +54,8 @@ func (o *option) Description() string {
if strings.Contains(o.description, "<<default>>") {
return strings.Replace(o.description, "<<default>>",
fmt.Sprintf("Default: %v.", o.defaultVal), -1)
} else {
return fmt.Sprintf("%s Default: %v.", o.description, o.defaultVal)
}
return fmt.Sprintf("%s Default: %v.", o.description, o.defaultVal)
}
return o.description
}
Expand Down Expand Up @@ -107,6 +107,9 @@ func FloatOption(names ...string) Option {
func StringOption(names ...string) Option {
return NewOption(String, names...)
}
func StringsOption(names ...string) Option {
return NewOption(Strings, names...)
}

type OptionValue struct {
value interface{}
Expand Down Expand Up @@ -180,6 +183,17 @@ func (ov OptionValue) String() (value string, found bool, err error) {
return val, ov.found, err
}

func (ov OptionValue) Strings() (value []string, found bool, err error) {
if !ov.found && ov.value == nil {
return nil, false, nil
}
val, ok := ov.value.([]string)
if !ok {
err = util.ErrCast()
}
return val, ov.found, err
}

// Flag names
const (
EncShort = "enc"
Expand Down
2 changes: 1 addition & 1 deletion commands/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ func (r *request) ConvertOptions() error {
}

kind := reflect.TypeOf(v).Kind()
if kind != opt.Type() {
if kind != opt.Type() && opt.Type() != Strings {
if kind == String {
convert := converters[opt.Type()]
str, ok := v.(string)
Expand Down