Normalize pr command arguments

This commit is contained in:
Sam Coe 2021-02-23 09:17:35 -08:00
parent c5af4ddfdc
commit 9d062ed8fc
No known key found for this signature in database
GPG key ID: 8E322C20F811D086
6 changed files with 27 additions and 9 deletions

View file

@ -35,9 +35,9 @@ func NewCmdClose(f *cmdutil.Factory, runF func(*CloseOptions) error) *cobra.Comm
}
cmd := &cobra.Command{
Use: "close {<number> | <url> | <branch>}",
Use: "close [<number> | <url> | <branch>]",
Short: "Close a pull request",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo

View file

@ -30,6 +30,7 @@ func NewCmdComment(f *cmdutil.Factory, runF func(*shared.CommentableOptions) err
Example: heredoc.Doc(`
$ gh pr comment 22 --body "This looks great, lets get it deployed."
`),
Args: cobra.MaximumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if repoOverride, _ := cmd.Flags().GetString("repo"); repoOverride != "" && len(args) == 0 {
return &cmdutil.FlagError{Err: errors.New("argument required when using the --repo flag")}

View file

@ -32,6 +32,12 @@ func TestNewCmdComment(t *testing.T) {
},
wantsErr: false,
},
{
name: "two arguments",
input: "1 2",
output: shared.CommentableOptions{},
wantsErr: true,
},
{
name: "pr number",
input: "1",

View file

@ -46,7 +46,7 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
}
cmd := &cobra.Command{
Use: "edit {<number> | <url>}",
Use: "edit [<number> | <url> | <branch>]",
Short: "Edit a pull request",
Example: heredoc.Doc(`
$ gh pr edit 23 --title "I found a bug" --body "Nothing works"
@ -56,12 +56,14 @@ func NewCmdEdit(f *cmdutil.Factory, runF func(*EditOptions) error) *cobra.Comman
$ gh pr edit 23 --add-project "Roadmap" --remove-project v1,v2
$ gh pr edit 23 --milestone "Version 1"
`),
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo
opts.SelectorArg = args[0]
if len(args) > 0 {
opts.SelectorArg = args[0]
}
flags := cmd.Flags()
if flags.Changed("title") {

View file

@ -23,8 +23,17 @@ func TestNewCmdEdit(t *testing.T) {
wantsErr bool
}{
{
name: "no argument",
input: "",
name: "no argument",
input: "",
output: EditOptions{
SelectorArg: "",
Interactive: true,
},
wantsErr: false,
},
{
name: "two arguments",
input: "1 2",
output: EditOptions{},
wantsErr: true,
},

View file

@ -30,9 +30,9 @@ func NewCmdReopen(f *cmdutil.Factory, runF func(*ReopenOptions) error) *cobra.Co
}
cmd := &cobra.Command{
Use: "reopen {<number> | <url> | <branch>}",
Use: "reopen [<number> | <url> | <branch>]",
Short: "Reopen a pull request",
Args: cobra.ExactArgs(1),
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// support `-R, --repo` override
opts.BaseRepo = f.BaseRepo