Skip to content

Commit

Permalink
Merge pull request cli#5221 from cli/string-enum-flag
Browse files Browse the repository at this point in the history
Use StringEnumFlag helper in more places
  • Loading branch information
mislav authored Feb 21, 2022
2 parents 1eefda0 + 19563c4 commit 2c981b7
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 19 deletions.
3 changes: 1 addition & 2 deletions pkg/cmd/completion/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ func NewCmdCompletion(io *iostreams.IOStreams) *cobra.Command {
}

cmdutil.DisableAuthCheck(cmd)

cmd.Flags().StringVarP(&shellType, "shell", "s", "", "Shell type: {bash|zsh|fish|powershell}")
cmdutil.StringEnumFlag(cmd, &shellType, "shell", "s", "", []string{"bash", "zsh", "fish", "powershell"}, "Shell type")

return cmd
}
2 changes: 1 addition & 1 deletion pkg/cmd/completion/completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestNewCmdCompletion(t *testing.T) {
{
name: "unsupported shell",
args: "completion -s csh",
wantErr: "unsupported shell type \"csh\"",
wantErr: "invalid argument \"csh\" for \"-s, --shell\" flag: valid values are {bash|zsh|fish|powershell}",
},
}
for _, tt := range tests {
Expand Down
5 changes: 1 addition & 4 deletions pkg/cmd/issue/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman
cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the browser to list the issue(s)")
cmd.Flags().StringVarP(&opts.Assignee, "assignee", "a", "", "Filter by assignee")
cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by labels")
cmd.Flags().StringVarP(&opts.State, "state", "s", "open", "Filter by state: {open|closed|all}")
_ = cmd.RegisterFlagCompletionFunc("state", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"open", "closed", "all"}, cobra.ShellCompDirectiveNoFileComp
})
cmdutil.StringEnumFlag(cmd, &opts.State, "state", "s", "open", []string{"open", "closed", "all"}, "Filter by state")
cmd.Flags().IntVarP(&opts.LimitResults, "limit", "L", 30, "Maximum number of issues to fetch")
cmd.Flags().StringVarP(&opts.Author, "author", "A", "", "Filter by author")
cmd.Flags().StringVar(&opts.Mention, "mention", "", "Filter by mention")
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/pr/diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewCmdDiff(f *cmdutil.Factory, runF func(*DiffOptions) error) *cobra.Comman
case "never":
opts.UseColor = false
default:
return cmdutil.FlagErrorf("the value for `--color` must be one of \"auto\", \"always\", or \"never\"")
return fmt.Errorf("unsupported color %q", colorFlag)
}

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

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

return cmd
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/pr/diff/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func Test_NewCmdDiff(t *testing.T) {
name: "invalid --color argument",
args: "--color doublerainbow",
isTTY: true,
wantErr: "the value for `--color` must be one of \"auto\", \"always\", or \"never\"",
wantErr: "invalid argument \"doublerainbow\" for \"--color\" flag: valid values are {always|never|auto}",
},
}
for _, tt := range tests {
Expand Down
5 changes: 1 addition & 4 deletions pkg/cmd/pr/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ func NewCmdList(f *cmdutil.Factory, runF func(*ListOptions) error) *cobra.Comman

cmd.Flags().BoolVarP(&opts.WebMode, "web", "w", false, "Open the browser to list the pull requests")
cmd.Flags().IntVarP(&opts.LimitResults, "limit", "L", 30, "Maximum number of items to fetch")
cmd.Flags().StringVarP(&opts.State, "state", "s", "open", "Filter by state: {open|closed|merged|all}")
_ = cmd.RegisterFlagCompletionFunc("state", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{"open", "closed", "merged", "all"}, cobra.ShellCompDirectiveNoFileComp
})
cmdutil.StringEnumFlag(cmd, &opts.State, "state", "s", "open", []string{"open", "closed", "merged", "all"}, "Filter by state")
cmd.Flags().StringVarP(&opts.BaseBranch, "base", "B", "", "Filter by base branch")
cmd.Flags().StringVarP(&opts.HeadBranch, "head", "H", "", "Filter by head branch")
cmd.Flags().StringSliceVarP(&opts.Labels, "label", "l", nil, "Filter by labels")
Expand Down
6 changes: 1 addition & 5 deletions pkg/cmd/secret/set/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
return cmdutil.FlagErrorf("`--visibility` is only supported with `--org`")
}

if opts.Visibility != shared.All && opts.Visibility != shared.Private && opts.Visibility != shared.Selected {
return cmdutil.FlagErrorf("`--visibility` must be one of \"all\", \"private\", or \"selected\"")
}

if opts.Visibility != shared.Selected && len(opts.RepositoryNames) > 0 {
return cmdutil.FlagErrorf("`--repos` is only supported with `--visibility=selected`")
}
Expand All @@ -149,7 +145,7 @@ func NewCmdSet(f *cmdutil.Factory, runF func(*SetOptions) error) *cobra.Command
cmd.Flags().StringVarP(&opts.OrgName, "org", "o", "", "Set `organization` secret")
cmd.Flags().StringVarP(&opts.EnvName, "env", "e", "", "Set deployment `environment` secret")
cmd.Flags().BoolVarP(&opts.UserSecrets, "user", "u", false, "Set a secret for your user")
cmd.Flags().StringVarP(&opts.Visibility, "visibility", "v", "private", "Set visibility for an organization secret: `{all|private|selected}`")
cmdutil.StringEnumFlag(cmd, &opts.Visibility, "visibility", "v", shared.Private, []string{shared.All, shared.Private, shared.Selected}, "Set visibility for an organization secret")
cmd.Flags().StringSliceVarP(&opts.RepositoryNames, "repos", "r", []string{}, "List of `repositories` that can access an organization or user secret")
cmd.Flags().StringVarP(&opts.Body, "body", "b", "", "The value for the secret (reads from standard input if not specified)")
cmd.Flags().BoolVar(&opts.DoNotStore, "no-store", false, "Print the encrypted, base64-encoded value instead of storing it on Github")
Expand Down
1 change: 1 addition & 0 deletions pkg/cmdutil/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func NilBoolFlag(cmd *cobra.Command, p **bool, name string, shorthand string, us

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

0 comments on commit 2c981b7

Please sign in to comment.