StringEnumFlag defines a new string flag that only allows values listed in options.
(cmd *cobra.Command, p *string, name, shorthand, defaultValue string, options []string, usage string)
| 26 | |
| 27 | // StringEnumFlag defines a new string flag that only allows values listed in options. |
| 28 | func StringEnumFlag(cmd *cobra.Command, p *string, name, shorthand, defaultValue string, options []string, usage string) *pflag.Flag { |
| 29 | *p = defaultValue |
| 30 | val := &enumValue{string: p, options: options} |
| 31 | f := cmd.Flags().VarPF(val, name, shorthand, fmt.Sprintf("%s: %s", usage, formatValuesForUsageDocs(options))) |
| 32 | _ = cmd.RegisterFlagCompletionFunc(name, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { |
| 33 | return options, cobra.ShellCompDirectiveNoFileComp |
| 34 | }) |
| 35 | return f |
| 36 | } |
| 37 | |
| 38 | func StringSliceEnumFlag(cmd *cobra.Command, p *[]string, name, shorthand string, defaultValues, options []string, usage string) *pflag.Flag { |
| 39 | *p = defaultValues |
no test coverage detected