From 4d182c431426014030c82674d07fda2134986010 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Thu, 4 Jun 2020 12:51:26 -0700 Subject: [PATCH 01/10] Better help command --- command/help.go | 117 ++++++++++++++++++++++++++++++++++++++++++++++++ command/pr.go | 7 +-- command/root.go | 108 +++++--------------------------------------- 3 files changed, 133 insertions(+), 99 deletions(-) create mode 100644 command/help.go diff --git a/command/help.go b/command/help.go new file mode 100644 index 000000000..d57763133 --- /dev/null +++ b/command/help.go @@ -0,0 +1,117 @@ +package command + +import ( + "fmt" + "strings" + + "github.com/cli/cli/utils" + "github.com/spf13/cobra" +) + +func rootDisplayCommandTypoHelp(command *cobra.Command, args []string) { + if command != RootCmd { + // Display helpful error message in case subcommand name was mistyped. + // This matches Cobra's behavior for root command, which Cobra + // confusingly doesn't apply to nested commands. + if command.Parent() == RootCmd && len(args) >= 2 { + if command.SuggestionsMinimumDistance <= 0 { + command.SuggestionsMinimumDistance = 2 + } + candidates := command.SuggestionsFor(args[1]) + + errOut := command.OutOrStderr() + fmt.Fprintf(errOut, "unknown command %q for %q\n", args[1], "gh "+args[0]) + + if len(candidates) > 0 { + fmt.Fprint(errOut, "\nDid you mean this?\n") + for _, c := range candidates { + fmt.Fprintf(errOut, "\t%s\n", c) + } + fmt.Fprint(errOut, "\n") + } + + oldOut := command.OutOrStdout() + command.SetOut(errOut) + defer command.SetOut(oldOut) + } + } +} + +func rootHelpFunc(command *cobra.Command, args []string) { + rootDisplayCommandTypoHelp(command, args) + + coreCommands := []string{} + additionalCommands := []string{} + for _, c := range command.Commands() { + if c.Short == "" { + continue + } + s := rpad(c.Name()+":", c.NamePadding()) + c.Short + if _, ok := c.Annotations["IsCore"]; ok { + coreCommands = append(coreCommands, s) + } else { + additionalCommands = append(additionalCommands, s) + } + } + + // If there are no core commands, assume everything is a core command + if len(coreCommands) == 0 { + coreCommands = additionalCommands + additionalCommands = []string{} + } + + type helpEntry struct { + Title string + Body string + } + + helpEntries := []helpEntry{ + {"", command.Long}, + {"USAGE", command.Use}, + } + + if len(coreCommands) > 0 { + helpEntries = append(helpEntries, helpEntry{"CORE COMMANDS", strings.Join(coreCommands, "\n")}) + } + if len(additionalCommands) > 0 { + helpEntries = append(helpEntries, helpEntry{"ADDITIONAL COMMANDS", strings.Join(additionalCommands, "\n")}) + } + if command.HasLocalFlags() { + helpEntries = append(helpEntries, helpEntry{"FLAGS", strings.TrimRight(command.LocalFlags().FlagUsages(), "\n")}) + } + if _, ok := command.Annotations["help:examples"]; ok { + helpEntries = append(helpEntries, helpEntry{"EXAMPLES", command.Annotations["help:examples"]}) + } + if _, ok := command.Annotations["help:learnmore"]; ok { + helpEntries = append(helpEntries, helpEntry{"LEARN MORE", command.Annotations["help:learnmore"]}) + } + if _, ok := command.Annotations["help:feedback"]; ok { + helpEntries = append(helpEntries, helpEntry{"FEEDBACK", command.Annotations["help:feedback"]}) + } + + out := colorableOut(command) + for _, e := range helpEntries { + if e.Title != "" { + // If there is a title, add indentation to each line in the body + fmt.Fprintln(out, utils.Bold(e.Title)) + + for _, l := range strings.Split(e.Body, "\n") { + l = strings.Trim(l, " \n\r") + if l == "" { + continue + } + fmt.Fprintln(out, " "+l) + } + } else { + // If there is no title print the body as is + fmt.Fprintln(out, e.Body) + } + fmt.Fprintln(out) + } +} + +// rpad adds padding to the right of a string. +func rpad(s string, padding int) string { + template := fmt.Sprintf("%%-%ds ", padding) + return fmt.Sprintf(template, s) +} diff --git a/command/pr.go b/command/pr.go index e92fa7cf0..20cacbf42 100644 --- a/command/pr.go +++ b/command/pr.go @@ -56,9 +56,10 @@ A pull request can be supplied as argument in any of the following formats: - by the name of its head branch, e.g. "patch-1" or "OWNER:patch-1".`, } var prListCmd = &cobra.Command{ - Use: "list", - Short: "List and filter pull requests in this repository", - RunE: prList, + Use: "list", + Short: "List and filter pull requests in this repository", + RunE: prList, + Annotations: map[string]string{"IsCore": "true"}, } var prStatusCmd = &cobra.Command{ Use: "status", diff --git a/command/root.go b/command/root.go index db76509ed..ee1a94143 100644 --- a/command/root.go +++ b/command/root.go @@ -34,7 +34,6 @@ var Version = "DEV" var BuildDate = "" // YYYY-MM-DD var versionOutput = "" -var cobraDefaultHelpFunc func(*cobra.Command, []string) func init() { if Version == "DEV" { @@ -58,7 +57,6 @@ func init() { // TODO: // RootCmd.PersistentFlags().BoolP("verbose", "V", false, "enable verbose output") - cobraDefaultHelpFunc = RootCmd.HelpFunc() RootCmd.SetHelpFunc(rootHelpFunc) RootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { @@ -95,6 +93,18 @@ var RootCmd = &cobra.Command{ SilenceErrors: true, SilenceUsage: true, + + Annotations: map[string]string{ + "help:examples": ` +$ gh issue create +$ gh repo clone +$ gh pr checkout 321`, + "help:learnmore": ` +Use "gh --help" for more information about a command. +Read the manual at `, + "help:feedback": ` +Fill out our feedback form +Open an issue using “gh issue create -R cli/cli”`}, } var versionCmd = &cobra.Command{ @@ -320,100 +330,6 @@ func determineBaseRepo(apiClient *api.Client, cmd *cobra.Command, ctx context.Co return baseRepo, nil } -func rootHelpFunc(command *cobra.Command, args []string) { - if command != RootCmd { - // Display helpful error message in case subcommand name was mistyped. - // This matches Cobra's behavior for root command, which Cobra - // confusingly doesn't apply to nested commands. - if command.Parent() == RootCmd && len(args) >= 2 { - if command.SuggestionsMinimumDistance <= 0 { - command.SuggestionsMinimumDistance = 2 - } - candidates := command.SuggestionsFor(args[1]) - - errOut := command.OutOrStderr() - fmt.Fprintf(errOut, "unknown command %q for %q\n", args[1], "gh "+args[0]) - - if len(candidates) > 0 { - fmt.Fprint(errOut, "\nDid you mean this?\n") - for _, c := range candidates { - fmt.Fprintf(errOut, "\t%s\n", c) - } - fmt.Fprint(errOut, "\n") - } - - oldOut := command.OutOrStdout() - command.SetOut(errOut) - defer command.SetOut(oldOut) - } - cobraDefaultHelpFunc(command, args) - return - } - - type helpEntry struct { - Title string - Body string - } - - coreCommandNames := []string{"issue", "pr", "repo"} - var coreCommands []string - var additionalCommands []string - for _, c := range command.Commands() { - if c.Short == "" { - continue - } - s := " " + rpad(c.Name()+":", c.NamePadding()) + c.Short - if includes(coreCommandNames, c.Name()) { - coreCommands = append(coreCommands, s) - } else if c != creditsCmd { - additionalCommands = append(additionalCommands, s) - } - } - - helpEntries := []helpEntry{ - { - "", - command.Long}, - {"USAGE", command.Use}, - {"CORE COMMANDS", strings.Join(coreCommands, "\n")}, - {"ADDITIONAL COMMANDS", strings.Join(additionalCommands, "\n")}, - {"FLAGS", strings.TrimRight(command.LocalFlags().FlagUsages(), "\n")}, - {"EXAMPLES", ` - $ gh issue create - $ gh repo clone - $ gh pr checkout 321`}, - {"LEARN MORE", ` - Use "gh --help" for more information about a command. - Read the manual at `}, - {"FEEDBACK", ` - Fill out our feedback form - Open an issue using “gh issue create -R cli/cli”`}, - } - - out := colorableOut(command) - for _, e := range helpEntries { - if e.Title != "" { - fmt.Fprintln(out, utils.Bold(e.Title)) - } - fmt.Fprintln(out, strings.TrimLeft(e.Body, "\n")+"\n") - } -} - -// rpad adds padding to the right of a string. -func rpad(s string, padding int) string { - template := fmt.Sprintf("%%-%ds ", padding) - return fmt.Sprintf(template, s) -} - -func includes(a []string, s string) bool { - for _, x := range a { - if x == s { - return true - } - } - return false -} - func formatRemoteURL(cmd *cobra.Command, fullRepoName string) string { ctx := contextForCommand(cmd) From 51353c79d6f669cf0c7f951eafca621e8658f4aa Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 9 Jun 2020 08:43:22 -0700 Subject: [PATCH 02/10] Use cobra `Example` field --- command/help.go | 4 ++-- command/root.go | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/command/help.go b/command/help.go index d57763133..b44444b2b 100644 --- a/command/help.go +++ b/command/help.go @@ -79,8 +79,8 @@ func rootHelpFunc(command *cobra.Command, args []string) { if command.HasLocalFlags() { helpEntries = append(helpEntries, helpEntry{"FLAGS", strings.TrimRight(command.LocalFlags().FlagUsages(), "\n")}) } - if _, ok := command.Annotations["help:examples"]; ok { - helpEntries = append(helpEntries, helpEntry{"EXAMPLES", command.Annotations["help:examples"]}) + if command.Example != "" { + helpEntries = append(helpEntries, helpEntry{"EXAMPLES", command.Example}) } if _, ok := command.Annotations["help:learnmore"]; ok { helpEntries = append(helpEntries, helpEntry{"LEARN MORE", command.Annotations["help:learnmore"]}) diff --git a/command/root.go b/command/root.go index ee1a94143..2bda468d0 100644 --- a/command/root.go +++ b/command/root.go @@ -93,12 +93,10 @@ var RootCmd = &cobra.Command{ SilenceErrors: true, SilenceUsage: true, - - Annotations: map[string]string{ - "help:examples": ` -$ gh issue create + Example: `$ gh issue create $ gh repo clone $ gh pr checkout 321`, + Annotations: map[string]string{ "help:learnmore": ` Use "gh --help" for more information about a command. Read the manual at `, From 0af61bee900924db3cedd2fc62d7d12215ba8bf9 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 9 Jun 2020 08:49:22 -0700 Subject: [PATCH 03/10] Set core root commands --- command/issue.go | 1 + command/pr.go | 8 ++++---- command/repo.go | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/command/issue.go b/command/issue.go index d467986a1..4650d4b43 100644 --- a/command/issue.go +++ b/command/issue.go @@ -56,6 +56,7 @@ var issueCmd = &cobra.Command{ An issue can be supplied as argument in any of the following formats: - by number, e.g. "123"; or - by URL, e.g. "https://github.com/OWNER/REPO/issues/123".`, + Annotations: map[string]string{"IsCore": "true"}, } var issueCreateCmd = &cobra.Command{ Use: "create", diff --git a/command/pr.go b/command/pr.go index 20cacbf42..8891ca2ac 100644 --- a/command/pr.go +++ b/command/pr.go @@ -54,12 +54,12 @@ A pull request can be supplied as argument in any of the following formats: - by number, e.g. "123"; - by URL, e.g. "https://github.com/OWNER/REPO/pull/123"; or - by the name of its head branch, e.g. "patch-1" or "OWNER:patch-1".`, + Annotations: map[string]string{"IsCore": "true"}, } var prListCmd = &cobra.Command{ - Use: "list", - Short: "List and filter pull requests in this repository", - RunE: prList, - Annotations: map[string]string{"IsCore": "true"}, + Use: "list", + Short: "List and filter pull requests in this repository", + RunE: prList, } var prStatusCmd = &cobra.Command{ Use: "status", diff --git a/command/repo.go b/command/repo.go index ad208a277..16308deec 100644 --- a/command/repo.go +++ b/command/repo.go @@ -48,6 +48,7 @@ var repoCmd = &cobra.Command{ A repository can be supplied as an argument in any of the following formats: - "OWNER/REPO" - by URL, e.g. "https://github.com/OWNER/REPO"`, + Annotations: map[string]string{"IsCore": "true"}, } var repoCloneCmd = &cobra.Command{ From a6948423f425bd220510b05b12d1c5396f8f338b Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 9 Jun 2020 09:33:22 -0700 Subject: [PATCH 04/10] Update issue and pr help --- command/help.go | 9 ++++++--- command/issue.go | 15 +++++++++------ command/pr.go | 16 ++++++++++------ command/root.go | 3 --- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/command/help.go b/command/help.go index b44444b2b..fe539d929 100644 --- a/command/help.go +++ b/command/help.go @@ -79,12 +79,15 @@ func rootHelpFunc(command *cobra.Command, args []string) { if command.HasLocalFlags() { helpEntries = append(helpEntries, helpEntry{"FLAGS", strings.TrimRight(command.LocalFlags().FlagUsages(), "\n")}) } + if _, ok := command.Annotations["help:arguments"]; ok { + helpEntries = append(helpEntries, helpEntry{"ARGUMENTS", command.Annotations["help:arguments"]}) + } if command.Example != "" { helpEntries = append(helpEntries, helpEntry{"EXAMPLES", command.Example}) } - if _, ok := command.Annotations["help:learnmore"]; ok { - helpEntries = append(helpEntries, helpEntry{"LEARN MORE", command.Annotations["help:learnmore"]}) - } + helpEntries = append(helpEntries, helpEntry{"LEARN MORE", ` +Use "gh --help" for more information about a command. +Read the manual at `}) if _, ok := command.Annotations["help:feedback"]; ok { helpEntries = append(helpEntries, helpEntry{"FEEDBACK", command.Annotations["help:feedback"]}) } diff --git a/command/issue.go b/command/issue.go index 4650d4b43..c9a677191 100644 --- a/command/issue.go +++ b/command/issue.go @@ -49,14 +49,17 @@ func init() { } var issueCmd = &cobra.Command{ - Use: "issue", + Use: "issue [flags]", Short: "Create and view issues", - Long: `Work with GitHub issues. - -An issue can be supplied as argument in any of the following formats: + Long: `Work with GitHub issues.`, + Example: `$ gh issue list +$ gh issue create --fill +$ gh issue view --web`, + Annotations: map[string]string{ + "IsCore": "true", + "help:arguments": `An issue can be supplied as argument in any of the following formats: - by number, e.g. "123"; or -- by URL, e.g. "https://github.com/OWNER/REPO/issues/123".`, - Annotations: map[string]string{"IsCore": "true"}, +- by URL, e.g. "https://github.com/OWNER/REPO/issues/123".`}, } var issueCreateCmd = &cobra.Command{ Use: "create", diff --git a/command/pr.go b/command/pr.go index 8891ca2ac..15b0cfb06 100644 --- a/command/pr.go +++ b/command/pr.go @@ -46,15 +46,19 @@ func init() { } var prCmd = &cobra.Command{ - Use: "pr", + Use: "pr [flags]", Short: "Create, view, and checkout pull requests", - Long: `Work with GitHub pull requests. - -A pull request can be supplied as argument in any of the following formats: + Long: `Work with GitHub pull requests.`, + Example: `$ gh pr checkout 353 +$ gh pr checkout bug-fix-branch +$ gh pr create --fill +$ gh pr view --web`, + Annotations: map[string]string{ + "IsCore": "true", + "help:arguments": `A pull request can be supplied as argument in any of the following formats: - by number, e.g. "123"; - by URL, e.g. "https://github.com/OWNER/REPO/pull/123"; or -- by the name of its head branch, e.g. "patch-1" or "OWNER:patch-1".`, - Annotations: map[string]string{"IsCore": "true"}, +- by the name of its head branch, e.g. "patch-1" or "OWNER:patch-1".`}, } var prListCmd = &cobra.Command{ Use: "list", diff --git a/command/root.go b/command/root.go index 2bda468d0..7a6e3e8a9 100644 --- a/command/root.go +++ b/command/root.go @@ -97,9 +97,6 @@ var RootCmd = &cobra.Command{ $ gh repo clone $ gh pr checkout 321`, Annotations: map[string]string{ - "help:learnmore": ` -Use "gh --help" for more information about a command. -Read the manual at `, "help:feedback": ` Fill out our feedback form Open an issue using “gh issue create -R cli/cli”`}, From 8979c0ce41fba728cc835cfe0db50dd8b72fcd6f Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 9 Jun 2020 09:51:18 -0700 Subject: [PATCH 05/10] Update repo --- command/issue.go | 2 +- command/pr.go | 2 +- command/repo.go | 15 ++++++++++----- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/command/issue.go b/command/issue.go index c9a677191..95df9f086 100644 --- a/command/issue.go +++ b/command/issue.go @@ -51,7 +51,7 @@ func init() { var issueCmd = &cobra.Command{ Use: "issue [flags]", Short: "Create and view issues", - Long: `Work with GitHub issues.`, + Long: `Work with GitHub issues`, Example: `$ gh issue list $ gh issue create --fill $ gh issue view --web`, diff --git a/command/pr.go b/command/pr.go index 15b0cfb06..622ef7f1c 100644 --- a/command/pr.go +++ b/command/pr.go @@ -48,7 +48,7 @@ func init() { var prCmd = &cobra.Command{ Use: "pr [flags]", Short: "Create, view, and checkout pull requests", - Long: `Work with GitHub pull requests.`, + Long: `Work with GitHub pull requests`, Example: `$ gh pr checkout 353 $ gh pr checkout bug-fix-branch $ gh pr create --fill diff --git a/command/repo.go b/command/repo.go index 16308deec..9601a3df3 100644 --- a/command/repo.go +++ b/command/repo.go @@ -41,14 +41,19 @@ func init() { } var repoCmd = &cobra.Command{ - Use: "repo", + Use: "repo [flags]", Short: "Create, clone, fork, and view repositories", - Long: `Work with GitHub repositories. - + Long: `Work with GitHub repositories`, + Example: `$ gh repo create +$ gh repo clone cli/cli +$ gh repo view --web +`, + Annotations: map[string]string{ + "IsCore": "true", + "help:arguments": ` A repository can be supplied as an argument in any of the following formats: - "OWNER/REPO" -- by URL, e.g. "https://github.com/OWNER/REPO"`, - Annotations: map[string]string{"IsCore": "true"}, +- by URL, e.g. "https://github.com/OWNER/REPO"`}, } var repoCloneCmd = &cobra.Command{ From 30d125709685a2798a661e3f239e82dcdf6b2192 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 9 Jun 2020 13:19:00 -0700 Subject: [PATCH 06/10] Update help output --- command/help.go | 17 ++++++++--------- command/issue.go | 2 +- command/pr.go | 7 +++++-- command/repo.go | 15 ++++++++++++--- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/command/help.go b/command/help.go index fe539d929..e7c9a1e02 100644 --- a/command/help.go +++ b/command/help.go @@ -32,7 +32,7 @@ func rootDisplayCommandTypoHelp(command *cobra.Command, args []string) { oldOut := command.OutOrStdout() command.SetOut(errOut) - defer command.SetOut(oldOut) + command.SetOut(oldOut) } } } @@ -65,11 +65,13 @@ func rootHelpFunc(command *cobra.Command, args []string) { Body string } - helpEntries := []helpEntry{ - {"", command.Long}, - {"USAGE", command.Use}, + helpEntries := []helpEntry{} + if command.Long != "" { + helpEntries = append(helpEntries, helpEntry{"", command.Long}) + } else if command.Short != "" { + helpEntries = append(helpEntries, helpEntry{"", command.Short}) } - + helpEntries = append(helpEntries, helpEntry{"USAGE", command.UseLine()}) if len(coreCommands) > 0 { helpEntries = append(helpEntries, helpEntry{"CORE COMMANDS", strings.Join(coreCommands, "\n")}) } @@ -98,11 +100,8 @@ Read the manual at `}) // If there is a title, add indentation to each line in the body fmt.Fprintln(out, utils.Bold(e.Title)) - for _, l := range strings.Split(e.Body, "\n") { + for _, l := range strings.Split(strings.Trim(e.Body, "\n\r"), "\n") { l = strings.Trim(l, " \n\r") - if l == "" { - continue - } fmt.Fprintln(out, " "+l) } } else { diff --git a/command/issue.go b/command/issue.go index 95df9f086..0f947bfc2 100644 --- a/command/issue.go +++ b/command/issue.go @@ -49,7 +49,7 @@ func init() { } var issueCmd = &cobra.Command{ - Use: "issue [flags]", + Use: "issue ", Short: "Create and view issues", Long: `Work with GitHub issues`, Example: `$ gh issue list diff --git a/command/pr.go b/command/pr.go index 622ef7f1c..dce006bf0 100644 --- a/command/pr.go +++ b/command/pr.go @@ -46,7 +46,7 @@ func init() { } var prCmd = &cobra.Command{ - Use: "pr [flags]", + Use: "pr ", Short: "Create, view, and checkout pull requests", Long: `Work with GitHub pull requests`, Example: `$ gh pr checkout 353 @@ -63,7 +63,10 @@ $ gh pr view --web`, var prListCmd = &cobra.Command{ Use: "list", Short: "List and filter pull requests in this repository", - RunE: prList, + Example: `$ gh pr list --limit all +$ gh pr list --state closed +$ gh pr list --label “priority 1” “bug”`, + RunE: prList, } var prStatusCmd = &cobra.Command{ Use: "status", diff --git a/command/repo.go b/command/repo.go index 9601a3df3..5378cac2a 100644 --- a/command/repo.go +++ b/command/repo.go @@ -41,7 +41,7 @@ func init() { } var repoCmd = &cobra.Command{ - Use: "repo [flags]", + Use: "repo ", Short: "Create, clone, fork, and view repositories", Long: `Work with GitHub repositories`, Example: `$ gh repo create @@ -69,9 +69,18 @@ To pass 'git clone' flags, separate them with '--'.`, var repoCreateCmd = &cobra.Command{ Use: "create []", Short: "Create a new repository", - Long: `Create a new GitHub repository. + Long: `Create a new GitHub repository`, + Example: utils.Bold("$ gh repo create") + ` +Will create a repository on your account using the name of your current directory -Use the "ORG/NAME" syntax to create a repository within your organization.`, +` + utils.Bold("$ gh repo create my-project") + ` +Will create a repository on your account using the name 'my-project' + +` + utils.Bold("$ gh repo create cli/my-project") + ` +Will create a repository in the organization 'cli' using the name 'my-project'`, + Annotations: map[string]string{"help:arguments": `A repository can be supplied as an argument in any of the following formats: +- +- by URL, e.g. "https://github.com/OWNER/REPO"`}, RunE: repoCreate, } From d140ae6f5ba3e44ca08425a0d6894e99e4716ad1 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 10 Jun 2020 09:29:34 -0700 Subject: [PATCH 07/10] Don't show hidden commands --- command/help.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/command/help.go b/command/help.go index e7c9a1e02..afc0e04bf 100644 --- a/command/help.go +++ b/command/help.go @@ -46,6 +46,10 @@ func rootHelpFunc(command *cobra.Command, args []string) { if c.Short == "" { continue } + if c.Hidden { + continue + } + s := rpad(c.Name()+":", c.NamePadding()) + c.Short if _, ok := c.Annotations["IsCore"]; ok { coreCommands = append(coreCommands, s) From 4931892cc3f6d5560a4205a90628cd96ff065f3a Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 10 Jun 2020 09:30:35 -0700 Subject: [PATCH 08/10] Apply changes that were lost in merge conflict --- command/help.go | 2 +- command/root.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/command/help.go b/command/help.go index afc0e04bf..e0599aa78 100644 --- a/command/help.go +++ b/command/help.go @@ -93,7 +93,7 @@ func rootHelpFunc(command *cobra.Command, args []string) { } helpEntries = append(helpEntries, helpEntry{"LEARN MORE", ` Use "gh --help" for more information about a command. -Read the manual at `}) +Read the manual at http://cli.github.com/manual`}) if _, ok := command.Annotations["help:feedback"]; ok { helpEntries = append(helpEntries, helpEntry{"FEEDBACK", command.Annotations["help:feedback"]}) } diff --git a/command/root.go b/command/root.go index 2d567754d..1b92ef86a 100644 --- a/command/root.go +++ b/command/root.go @@ -98,7 +98,7 @@ $ gh repo clone $ gh pr checkout 321`, Annotations: map[string]string{ "help:feedback": ` -Fill out our feedback form +Fill out our feedback form https://forms.gle/umxd3h31c7aMQFKG7 Open an issue using “gh issue create -R cli/cli”`}, } From 7d7a010739bed4d1105998a4d9f9a58ef13f91ea Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 10 Jun 2020 09:43:24 -0700 Subject: [PATCH 09/10] Clear out usage func --- command/root.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/command/root.go b/command/root.go index 1b92ef86a..12979ad5f 100644 --- a/command/root.go +++ b/command/root.go @@ -59,6 +59,9 @@ func init() { RootCmd.SetHelpFunc(rootHelpFunc) + // This will silence the usage func on error + RootCmd.SetUsageFunc(func(_ *cobra.Command) error { return nil }) + RootCmd.SetFlagErrorFunc(func(cmd *cobra.Command, err error) error { if err == pflag.ErrHelp { return err From 2fb48e93eda3a8185ee92d8509559f2039c526ec Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 10 Jun 2020 09:59:40 -0700 Subject: [PATCH 10/10] Inline method call --- command/help.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/command/help.go b/command/help.go index e0599aa78..ad3561af2 100644 --- a/command/help.go +++ b/command/help.go @@ -8,11 +8,11 @@ import ( "github.com/spf13/cobra" ) -func rootDisplayCommandTypoHelp(command *cobra.Command, args []string) { +func rootHelpFunc(command *cobra.Command, args []string) { + // Display helpful error message in case subcommand name was mistyped. + // This matches Cobra's behavior for root command, which Cobra + // confusingly doesn't apply to nested commands. if command != RootCmd { - // Display helpful error message in case subcommand name was mistyped. - // This matches Cobra's behavior for root command, which Cobra - // confusingly doesn't apply to nested commands. if command.Parent() == RootCmd && len(args) >= 2 { if command.SuggestionsMinimumDistance <= 0 { command.SuggestionsMinimumDistance = 2 @@ -32,13 +32,9 @@ func rootDisplayCommandTypoHelp(command *cobra.Command, args []string) { oldOut := command.OutOrStdout() command.SetOut(errOut) - command.SetOut(oldOut) + defer command.SetOut(oldOut) } } -} - -func rootHelpFunc(command *cobra.Command, args []string) { - rootDisplayCommandTypoHelp(command, args) coreCommands := []string{} additionalCommands := []string{}