Created
November 26, 2022 12:34
-
-
Save richardbowden/fc1282a604d6727273c883f1a1af001f to your computer and use it in GitHub Desktop.
urfave_cli_choices_ext
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type CLIChoice struct { | |
| Choices []string | |
| Default string | |
| chosen string | |
| } | |
| func (c *CLIChoice) Set(value string) error { | |
| for _, choice := range c.Choices { | |
| if choice == value { | |
| c.chosen = value | |
| return nil | |
| } | |
| } | |
| return fmt.Errorf("%s is not valid. Choices are %s", value, strings.Join(c.Choices, ", ")) | |
| } | |
| func (c CLIChoice) String() string { | |
| if c.chosen == "" { | |
| return c.Default | |
| } | |
| return c.chosen | |
| } | |
| &cli.GenericFlag{Name: "loglevel", Value: &CLIChoice{Choices: []string{"trace", "debug", "info", "warn", "error", "critical"}, Default: "info"}}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment