diff --git a/command/root.go b/command/root.go index c0b676c86..a309960e3 100644 --- a/command/root.go +++ b/command/root.go @@ -16,6 +16,15 @@ func init() { RootCmd.PersistentFlags().StringP("current-branch", "B", "", "current git branch") // TODO: // RootCmd.PersistentFlags().BoolP("verbose", "V", false, "enable verbose output") + + RootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { + return FlagError{err} + }) +} + +// FlagError is the kind of error raised in flag processing +type FlagError struct { + error } // RootCmd is the entry point of command-line execution @@ -23,10 +32,9 @@ var RootCmd = &cobra.Command{ Use: "gh", Short: "GitHub CLI", Long: `Do things with GitHub from your terminal`, - Args: cobra.MinimumNArgs(1), - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("root") - }, + + SilenceErrors: true, + SilenceUsage: true, } // overriden in tests diff --git a/main.go b/main.go index f954ae933..e7f94a5d0 100644 --- a/main.go +++ b/main.go @@ -3,13 +3,18 @@ package main import ( "fmt" "os" + "strings" "github.com/github/gh-cli/command" ) func main() { - if err := command.RootCmd.Execute(); err != nil { - fmt.Println(err) + if cmd, err := command.RootCmd.ExecuteC(); err != nil { + fmt.Fprintln(os.Stderr, err) + _, isFlagError := err.(command.FlagError) + if isFlagError || strings.HasPrefix(err.Error(), "unknown command ") { + fmt.Fprintln(os.Stderr, cmd.UsageString()) + } os.Exit(1) } }