From 53a97c3414a4e78c66ddbc0db25afdd1f873781b Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Fri, 25 Sep 2020 16:11:11 +0200 Subject: [PATCH] Extract rootFlagErrrorFunc into root help --- pkg/cmd/root/help.go | 9 +++++++++ pkg/cmd/root/root.go | 15 ++++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/pkg/cmd/root/help.go b/pkg/cmd/root/help.go index 400be3834..ae57c1328 100644 --- a/pkg/cmd/root/help.go +++ b/pkg/cmd/root/help.go @@ -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 diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 8549ac3bc..749c3bc05 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -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 }