Skip to content

Instantly share code, notes, and snippets.

@richardbowden
Created November 26, 2022 12:34
Show Gist options
  • Select an option

  • Save richardbowden/fc1282a604d6727273c883f1a1af001f to your computer and use it in GitHub Desktop.

Select an option

Save richardbowden/fc1282a604d6727273c883f1a1af001f to your computer and use it in GitHub Desktop.
urfave_cli_choices_ext
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