|
| 1 | +package flags |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + |
| 7 | + "github.com/liujed/goutil/optionals" |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/spf13/pflag" |
| 10 | +) |
| 11 | + |
| 12 | +// Defines a command-line Flag. |
| 13 | +type Flag[T any] struct { |
| 14 | + // Whether this flag is inherited by subcommands. |
| 15 | + Persistent bool |
| 16 | + |
| 17 | + Name string |
| 18 | + ShortName optionals.Optional[rune] |
| 19 | + DefaultValue T |
| 20 | + UsageMsg string |
| 21 | + |
| 22 | + Required bool |
| 23 | + Hidden bool |
| 24 | + DeprecationMsg optionals.Optional[string] |
| 25 | + |
| 26 | + // If given, then command-line completions will be restricted to filenames |
| 27 | + // having any of the given extensions. |
| 28 | + FilenameExts optionals.Optional[[]string] |
| 29 | + |
| 30 | + // Whether command-line completions should be restricted to directory names. |
| 31 | + DirNames bool |
| 32 | +} |
| 33 | + |
| 34 | +// Adds the given boolean-valued flag to the given command. |
| 35 | +func AddBoolFlag( |
| 36 | + cmd *cobra.Command, |
| 37 | + f Flag[bool], |
| 38 | +) *bool { |
| 39 | + flags := f.getFlagSet(cmd) |
| 40 | + return addFlag(flags, flags.BoolP, f) |
| 41 | +} |
| 42 | + |
| 43 | +// Adds the given string-valued flag to the given command. |
| 44 | +func AddStringFlag( |
| 45 | + cmd *cobra.Command, |
| 46 | + f Flag[string], |
| 47 | +) *string { |
| 48 | + flags := f.getFlagSet(cmd) |
| 49 | + return addFlag(flags, flags.StringP, f) |
| 50 | +} |
| 51 | + |
| 52 | +// Adds the given string-slice-valued flag to the given command. |
| 53 | +func AddStringSliceFlag( |
| 54 | + cmd *cobra.Command, |
| 55 | + f Flag[[]string], |
| 56 | +) *[]string { |
| 57 | + flags := f.getFlagSet(cmd) |
| 58 | + return addFlag(flags, flags.StringSliceP, f) |
| 59 | +} |
| 60 | + |
| 61 | +// Returns the FlagSet corresponding to this flag. |
| 62 | +func (f Flag[T]) getFlagSet(cmd *cobra.Command) *pflag.FlagSet { |
| 63 | + if f.Persistent { |
| 64 | + return cmd.PersistentFlags() |
| 65 | + } |
| 66 | + return cmd.Flags() |
| 67 | +} |
| 68 | + |
| 69 | +// Adds the given flag to the given command. |
| 70 | +func addFlag[T any]( |
| 71 | + flagSet *pflag.FlagSet, |
| 72 | + defineFlag func(name string, shorthand string, value T, usage string) *T, |
| 73 | + f Flag[T], |
| 74 | +) *T { |
| 75 | + if reflect.TypeFor[T]().Kind() == reflect.Slice { |
| 76 | + f.UsageMsg = fmt.Sprintf("%s. Can be specified multiple times", f.UsageMsg) |
| 77 | + } |
| 78 | + |
| 79 | + shortName := "" |
| 80 | + if r, exists := f.ShortName.Get(); exists { |
| 81 | + shortName = string(r) |
| 82 | + } |
| 83 | + result := defineFlag(f.Name, shortName, f.DefaultValue, f.UsageMsg) |
| 84 | + |
| 85 | + if f.Required { |
| 86 | + cobra.MarkFlagRequired(flagSet, f.Name) |
| 87 | + } |
| 88 | + if f.Hidden { |
| 89 | + flagSet.MarkHidden(f.Name) |
| 90 | + } |
| 91 | + if msg, deprecated := f.DeprecationMsg.Get(); deprecated { |
| 92 | + flagSet.MarkDeprecated(f.Name, msg) |
| 93 | + } |
| 94 | + |
| 95 | + if exts, exists := f.FilenameExts.Get(); exists { |
| 96 | + cobra.MarkFlagFilename(flagSet, f.Name, exts...) |
| 97 | + } |
| 98 | + if f.DirNames { |
| 99 | + cobra.MarkFlagDirname(flagSet, f.Name) |
| 100 | + } |
| 101 | + |
| 102 | + return result |
| 103 | +} |
0 commit comments