Extract rootFlagErrrorFunc into root help

This commit is contained in:
Sam Coe 2020-09-25 16:11:11 +02:00
parent f00a0f2854
commit 53a97c3414
No known key found for this signature in database
GPG key ID: 8E322C20F811D086
2 changed files with 13 additions and 11 deletions

View file

@ -5,9 +5,11 @@ import (
"fmt"
"strings"
"github.com/cli/cli/pkg/cmdutil"
"github.com/cli/cli/pkg/text"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func rootUsageFunc(command *cobra.Command) error {
@ -33,6 +35,13 @@ func rootUsageFunc(command *cobra.Command) error {
return nil
}
func rootFlagErrrorFunc(cmd *cobra.Command, err error) error {
if err == pflag.ErrHelp {
return err
}
return &cmdutil.FlagError{Err: err}
}
var hasFailed bool
// HasFailed signals that the main process should exit with non-zero status

View file

@ -24,7 +24,6 @@ import (
creditsCmd "github.com/cli/cli/pkg/cmd/repo/credits"
"github.com/cli/cli/pkg/cmdutil"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
@ -73,13 +72,7 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
cmd.PersistentFlags().Bool("help", false, "Show help for command")
cmd.SetHelpFunc(rootHelpFunc)
cmd.SetUsageFunc(rootUsageFunc)
cmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error {
if err == pflag.ErrHelp {
return err
}
return &cmdutil.FlagError{Err: err}
})
cmd.SetFlagErrorFunc(rootFlagErrrorFunc)
cmdutil.DisableAuthCheck(cmd)
@ -91,9 +84,6 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
cmd.AddCommand(gistCmd.NewCmdGist(f))
cmd.AddCommand(completionCmd.NewCmdCompletion(f))
// Help topics
cmd.AddCommand(NewHelpTopic("environment"))
// the `api` command should not inherit any extra HTTP headers
bareHTTPCmdFactory := *f
bareHTTPCmdFactory.HttpClient = bareHTTPClient(f, version)
@ -109,6 +99,9 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
cmd.AddCommand(releaseCmd.NewCmdRelease(&repoResolvingCmdFactory))
cmd.AddCommand(repoCmd.NewCmdRepo(&repoResolvingCmdFactory))
// Help topics
cmd.AddCommand(NewHelpTopic("environment"))
return cmd
}