Merge pull request #41 from github/silence-usage

Silence Cobra usage on errors
This commit is contained in:
Mislav Marohnić 2019-10-31 22:56:02 +01:00 committed by GitHub
commit f0ca9aa023
2 changed files with 19 additions and 6 deletions

View file

@ -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

View file

@ -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)
}
}