Skip to content

Commit b838ac4

Browse files
committed
Improve no args error handler and extend it to other commands
1 parent d583f8b commit b838ac4

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

command/issue.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/cli/cli/api"
1515
"github.com/cli/cli/git"
1616
"github.com/cli/cli/internal/ghrepo"
17+
"github.com/cli/cli/pkg/cmdutil"
1718
"github.com/cli/cli/pkg/githubtemplate"
1819
"github.com/cli/cli/utils"
1920
"github.com/spf13/cobra"
@@ -67,21 +68,17 @@ var issueCmd = &cobra.Command{
6768
var issueCreateCmd = &cobra.Command{
6869
Use: "create",
6970
Short: "Create a new issue",
71+
Args: cmdutil.NoArgsQuoteReminder,
7072
RunE: issueCreate,
7173
}
7274
var issueListCmd = &cobra.Command{
7375
Use: "list",
7476
Short: "List and filter issues in this repository",
75-
Args: func(cmd *cobra.Command, args []string) error {
76-
if len(args) > 0 {
77-
return fmt.Errorf("unknown argument %q for %q: Please quote all flag values that contain spaces.", args[0], cmd.CommandPath())
78-
}
79-
return nil
80-
},
81-
Example: `
77+
Example: heredoc.Doc(`
8278
$ gh issue list -l "help wanted"
83-
$ gh issue list -A "some author"
84-
`,
79+
$ gh issue list -A monalisa
80+
`),
81+
Args: cmdutil.NoArgsQuoteReminder,
8582
RunE: issueList,
8683
}
8784
var issueStatusCmd = &cobra.Command{

command/pr.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/cli/cli/context"
1616
"github.com/cli/cli/git"
1717
"github.com/cli/cli/internal/ghrepo"
18+
"github.com/cli/cli/pkg/cmdutil"
1819
"github.com/cli/cli/pkg/text"
1920
"github.com/cli/cli/utils"
2021
"github.com/spf13/cobra"
@@ -65,6 +66,7 @@ var prCmd = &cobra.Command{
6566
var prListCmd = &cobra.Command{
6667
Use: "list",
6768
Short: "List and filter pull requests in this repository",
69+
Args: cmdutil.NoArgsQuoteReminder,
6870
Example: heredoc.Doc(`
6971
$ gh pr list --limit 999
7072
$ gh pr list --state closed

command/pr_create.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/cli/cli/context"
1212
"github.com/cli/cli/git"
1313
"github.com/cli/cli/internal/ghrepo"
14+
"github.com/cli/cli/pkg/cmdutil"
1415
"github.com/cli/cli/pkg/githubtemplate"
1516
"github.com/cli/cli/utils"
1617
"github.com/spf13/cobra"
@@ -452,6 +453,7 @@ func generateCompareURL(r ghrepo.Interface, base, head, title, body string, assi
452453
var prCreateCmd = &cobra.Command{
453454
Use: "create",
454455
Short: "Create a pull request",
456+
Args: cmdutil.NoArgsQuoteReminder,
455457
RunE: prCreate,
456458
}
457459

pkg/cmdutil/args.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package cmdutil
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
7+
"github.com/spf13/cobra"
8+
"github.com/spf13/pflag"
9+
)
10+
11+
func NoArgsQuoteReminder(cmd *cobra.Command, args []string) error {
12+
if len(args) < 1 {
13+
return nil
14+
}
15+
16+
errMsg := fmt.Sprintf("unknown argument %q", args[0])
17+
if len(args) > 1 {
18+
errMsg = fmt.Sprintf("unknown arguments %q", args)
19+
}
20+
21+
hasValueFlag := false
22+
cmd.Flags().Visit(func(f *pflag.Flag) {
23+
if f.Value.Type() != "bool" {
24+
hasValueFlag = true
25+
}
26+
})
27+
28+
if hasValueFlag {
29+
errMsg += "; please quote all values that have spaces"
30+
}
31+
32+
return &FlagError{Err: errors.New(errMsg)}
33+
}

0 commit comments

Comments
 (0)