Cli: Enum Support

Created on 26 Feb 2017  路  11Comments  路  Source: urfave/cli

Option values often only support a limited set of valid values. It would be helpful to have CLI enforce valid options and auto generate help docs listing valid options.

arev2 kinfeature statuclaimed statustale

Most helpful comment

The following might be helpful:

type EnumValue struct {
    Enum     []string
    Default  string
    selected string
}

func (e *EnumValue) Set(value string) error {
    for _, enum := range e.Enum {
        if enum == value {
            e.selected = value
            return nil
        }
    }

    return fmt.Errorf("allowed values are %s", strings.Join(e.Enum, ", "))
}

func (e EnumValue) String() string {
    if e.selected == "" {
        return e.Default
    }
    return e.selected
}

Usage:

app.Flags = []cli.Flag{
    cli.GenericFlag{
        Name: "format, f",
        Value: &EnumValue{
            Enum:    []string{"json", "plist", "xml"},
            Default: "json",
        },
        Usage: "output in json, plist or xml format",
    },
}

All 11 comments

:+1: this is possible via the GenericFlag, but I'll mark this as a possible enhancement.

Thanks!

+1

+1

The following might be helpful:

type EnumValue struct {
    Enum     []string
    Default  string
    selected string
}

func (e *EnumValue) Set(value string) error {
    for _, enum := range e.Enum {
        if enum == value {
            e.selected = value
            return nil
        }
    }

    return fmt.Errorf("allowed values are %s", strings.Join(e.Enum, ", "))
}

func (e EnumValue) String() string {
    if e.selected == "" {
        return e.Default
    }
    return e.selected
}

Usage:

app.Flags = []cli.Flag{
    cli.GenericFlag{
        Name: "format, f",
        Value: &EnumValue{
            Enum:    []string{"json", "plist", "xml"},
            Default: "json",
        },
        Usage: "output in json, plist or xml format",
    },
}

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

This issue or PR has been bumped and is no longer marked as stale! Feel free to bump it again in the future, if it's still relevant.

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

I'd still like to see this dear bot.

This issue or PR has been bumped and is no longer marked as stale! Feel free to bump it again in the future, if it's still relevant.

This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.

Closing this as it has become stale.

Was this page helpful?
0 / 5 - 0 ratings