Skip to content

Commit 2939924

Browse files
authored
Merge branch 'master' into version-flag
2 parents c3d70bd + f0ca9aa commit 2939924

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

command/root.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,25 @@ func init() {
2222
RootCmd.PersistentFlags().StringP("current-branch", "B", "", "current git branch")
2323
// TODO:
2424
// RootCmd.PersistentFlags().BoolP("verbose", "V", false, "enable verbose output")
25+
26+
RootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
27+
return FlagError{err}
28+
})
29+
}
30+
31+
// FlagError is the kind of error raised in flag processing
32+
type FlagError struct {
33+
error
2534
}
2635

2736
// RootCmd is the entry point of command-line execution
2837
var RootCmd = &cobra.Command{
2938
Use: "gh",
3039
Short: "GitHub CLI",
3140
Long: `Do things with GitHub from your terminal`,
41+
42+
SilenceErrors: true,
43+
SilenceUsage: true,
3244
}
3345

3446
// overriden in tests

main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ package main
33
import (
44
"fmt"
55
"os"
6+
"strings"
67

78
"github.com/github/gh-cli/command"
89
)
910

1011
func main() {
11-
if err := command.RootCmd.Execute(); err != nil {
12-
fmt.Println(err)
12+
if cmd, err := command.RootCmd.ExecuteC(); err != nil {
13+
fmt.Fprintln(os.Stderr, err)
14+
_, isFlagError := err.(command.FlagError)
15+
if isFlagError || strings.HasPrefix(err.Error(), "unknown command ") {
16+
fmt.Fprintln(os.Stderr, cmd.UsageString())
17+
}
1318
os.Exit(1)
1419
}
1520
}

0 commit comments

Comments
 (0)