Skip to content

Commit 19563c4

Browse files
committed
Use StringEnumFlag helper in more places
1 parent 1eefda0 commit 19563c4

File tree

8 files changed

+9
-19
lines changed

8 files changed

+9
-19
lines changed

pkg/cmd/completion/completion.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ func NewCmdCompletion(io *iostreams.IOStreams) *cobra.Command {
9292
}
9393

9494
cmdutil.DisableAuthCheck(cmd)
95-
96-
cmd.Flags().StringVarP(&shellType, "shell", "s", "", "Shell type: {bash|zsh|fish|powershell}")
95+
cmdutil.StringEnumFlag(cmd, &shellType, "shell", "s", "", []string{"bash", "zsh", "fish", "powershell"}, "Shell type")
9796

9897
return cmd
9998
}

pkg/cmd/completion/completion_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestNewCmdCompletion(t *testing.T) {
3939
{
4040
name: "unsupported shell",
4141
args: "completion -s csh",
42-
wantErr: "unsupported shell type \"csh\"",
42+
wantErr: "invalid argument \"csh\" for \"-s, --shell\" flag: valid values are {bash|zsh|fish|powershell}",
4343
},
4444
}
4545
for _, tt := range tests {

pkg/cmd/issue/list/list.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
8686
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the browser to list the issue(s)")
8787
cmd.Flags().StringVarP(&opts.Assignee, "assignee", "a", "", "Filter by assignee")
8888
cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by labels")
89-
cmd.Flags().StringVarP(&opts.State, "state", "s", "open", "Filter by state: {open|closed|all}")
90-
_ = cmd.RegisterFlagCompletionFunc("state", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
91-
return []string{"open", "closed", "all"}, cobra.ShellCompDirectiveNoFileComp
92-
})
89+
cmdutil.StringEnumFlag(cmd, &opts.State, "state", "s", "open", []string{"open", "closed", "all"}, "Filter by state")
9390
cmd.Flags().IntVarP(&opts.LimitResults, "limit", "L", 30, "Maximum number of issues to fetch")
9491
cmd.Flags().StringVarP(&opts.Author, "author", "A", "", "Filter by author")
9592
cmd.Flags().StringVar(&opts.Mention, "mention", "", "Filter by mention")

pkg/cmd/pr/diff/diff.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Comman
6666
case "never":
6767
opts.UseColor = false
6868
default:
69-
return cmdutil.FlagErrorf("the value for `--color` must be one of \"auto\", \"always\", or \"never\"")
69+
return fmt.Errorf("unsupported color %q", colorFlag)
7070
}
7171

7272
if runF != nil {
@@ -76,7 +76,7 @@ func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Comman
7676
},
7777
}
7878

79-
cmd.Flags().StringVar(&colorFlag, "color", "auto", "Use color in diff output: {always|never|auto}")
79+
cmdutil.StringEnumFlag(cmd, &colorFlag, "color", "", "auto", []string{"always", "never", "auto"}, "Use color in diff output")
8080
cmd.Flags().BoolVar(&opts.Patch, "patch", false, "Display diff in patch format")
8181

8282
return cmd

pkg/cmd/pr/diff/diff_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func Test_NewCmdDiff(t *testing.T) {
8383
name: "invalid --color argument",
8484
args: "--color doublerainbow",
8585
isTTY: true,
86-
wantErr: "the value for `--color` must be one of \"auto\", \"always\", or \"never\"",
86+
wantErr: "invalid argument \"doublerainbow\" for \"--color\" flag: valid values are {always|never|auto}",
8787
},
8888
}
8989
for _, tt := range tests {

pkg/cmd/pr/list/list.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
101101

102102
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the browser to list the pull requests")
103103
cmd.Flags().IntVarP(&opts.LimitResults, "limit", "L", 30, "Maximum number of items to fetch")
104-
cmd.Flags().StringVarP(&opts.State, "state", "s", "open", "Filter by state: {open|closed|merged|all}")
105-
_ = cmd.RegisterFlagCompletionFunc("state", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
106-
return []string{"open", "closed", "merged", "all"}, cobra.ShellCompDirectiveNoFileComp
107-
})
104+
cmdutil.StringEnumFlag(cmd, &opts.State, "state", "s", "open", []string{"open", "closed", "merged", "all"}, "Filter by state")
108105
cmd.Flags().StringVarP(&opts.BaseBranch, "base", "B", "", "Filter by base branch")
109106
cmd.Flags().StringVarP(&opts.HeadBranch, "head", "H", "", "Filter by head branch")
110107
cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by labels")

pkg/cmd/secret/set/set.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,6 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
121121
return cmdutil.FlagErrorf("`--visibility` is only supported with `--org`")
122122
}
123123

124-
if opts.Visibility != shared.All && opts.Visibility != shared.Private && opts.Visibility != shared.Selected {
125-
return cmdutil.FlagErrorf("`--visibility` must be one of \"all\", \"private\", or \"selected\"")
126-
}
127-
128124
if opts.Visibility != shared.Selected && len(opts.RepositoryNames) > 0 {
129125
return cmdutil.FlagErrorf("`--repos` is only supported with `--visibility=selected`")
130126
}
@@ -149,7 +145,7 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
149145
cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "Set `organization` secret")
150146
cmd.Flags().StringVarP(&opts.EnvName, "env", "e", "", "Set deployment `environment` secret")
151147
cmd.Flags().BoolVarP(&opts.UserSecrets, "user", "u", false, "Set a secret for your user")
152-
cmd.Flags().StringVarP(&opts.Visibility, "visibility", "v", "private", "Set visibility for an organization secret: `{all|private|selected}`")
148+
cmdutil.StringEnumFlag(cmd, &opts.Visibility, "visibility", "v", shared.Private, []string{shared.All, shared.Private, shared.Selected}, "Set visibility for an organization secret")
153149
cmd.Flags().StringSliceVarP(&opts.RepositoryNames, "repos", "r", []string{}, "List of `repositories` that can access an organization or user secret")
154150
cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "The value for the secret (reads from standard input if not specified)")
155151
cmd.Flags().BoolVar(&opts.DoNotStore, "no-store", false, "Print the encrypted, base64-encoded value instead of storing it on Github")

pkg/cmdutil/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func NilBoolFlag(cmd *cobra.Command, p **bool, name string, shorthand string, us
2525

2626
// StringEnumFlag defines a new string flag that only allows values listed in options.
2727
func StringEnumFlag(cmd *cobra.Command, p *string, name, shorthand, defaultValue string, options []string, usage string) *pflag.Flag {
28+
*p = defaultValue
2829
val := &enumValue{string: p, options: options}
2930
f := cmd.Flags().VarPF(val, name, shorthand, fmt.Sprintf("%s: %s", usage, formatValuesForUsageDocs(options)))
3031
_ = cmd.RegisterFlagCompletionFunc(name, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

0 commit comments

Comments
 (0)