X Tutup
Skip to content

Commit 2144921

Browse files
committed
Expand help topic functionality
1 parent 3c32507 commit 2144921

File tree

3 files changed

+59
-29
lines changed

3 files changed

+59
-29
lines changed

pkg/cmd/root/help.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,6 @@ func rootHelpFunc(command *cobra.Command, args []string) {
7878
return
7979
}
8080

81-
if helpTopic := command.Annotations["helpTopic"]; helpTopic == "true" {
82-
fmt.Fprint(command.OutOrStdout(), command.Long)
83-
return
84-
}
85-
8681
coreCommands := []string{}
8782
additionalCommands := []string{}
8883
for _, c := range command.Commands() {

pkg/cmd/root/help_topic.go

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,76 @@
11
package root
22

33
import (
4+
"fmt"
5+
46
"github.com/MakeNowJust/heredoc"
57
"github.com/spf13/cobra"
68
)
79

810
func NewHelpTopic(topic string) *cobra.Command {
9-
return &cobra.Command{
10-
Use: "environment",
11-
Long: heredoc.Doc(`
12-
GITHUB_TOKEN: an authentication token for github.com API requests. Setting this avoids
13-
being prompted to authenticate and takes precedence over previously stored credentials.
11+
topicContent := make(map[string]string)
12+
13+
topicContent["environment"] = heredoc.Doc(`
14+
GITHUB_TOKEN: an authentication token for github.com API requests. Setting this avoids
15+
being prompted to authenticate and takes precedence over previously stored credentials.
1416
15-
GITHUB_ENTERPRISE_TOKEN: an authentication token for API requests to GitHub Enterprise.
17+
GITHUB_ENTERPRISE_TOKEN: an authentication token for API requests to GitHub Enterprise.
1618
17-
GH_REPO: specify the GitHub repository in the "[HOST/]OWNER/REPO" format for commands
18-
that otherwise operate on a local repository.
19+
GH_REPO: specify the GitHub repository in the "[HOST/]OWNER/REPO" format for commands
20+
that otherwise operate on a local repository.
1921
20-
GH_HOST: specify the GitHub hostname for commands that would otherwise assume
21-
the "github.com" host when not in a context of an existing repository.
22+
GH_HOST: specify the GitHub hostname for commands that would otherwise assume
23+
the "github.com" host when not in a context of an existing repository.
2224
23-
GH_EDITOR, GIT_EDITOR, VISUAL, EDITOR (in order of precedence): the editor tool to use
24-
for authoring text.
25+
GH_EDITOR, GIT_EDITOR, VISUAL, EDITOR (in order of precedence): the editor tool to use
26+
for authoring text.
2527
26-
BROWSER: the web browser to use for opening links.
28+
BROWSER: the web browser to use for opening links.
2729
28-
DEBUG: set to any value to enable verbose output to standard error. Include values "api"
29-
or "oauth" to print detailed information about HTTP requests or authentication flow.
30+
DEBUG: set to any value to enable verbose output to standard error. Include values "api"
31+
or "oauth" to print detailed information about HTTP requests or authentication flow.
3032
31-
GLAMOUR_STYLE: the style to use for rendering Markdown. See
32-
https://github.com/charmbracelet/glamour#styles
33+
GLAMOUR_STYLE: the style to use for rendering Markdown. See
34+
https://github.com/charmbracelet/glamour#styles
3335
34-
NO_COLOR: avoid printing ANSI escape sequences for color output.
35-
`),
36+
NO_COLOR: avoid printing ANSI escape sequences for color output.
37+
`)
38+
39+
cmd := &cobra.Command{
40+
Use: topic,
41+
Long: topicContent[topic],
3642
Hidden: true,
37-
Annotations: map[string]string{
38-
"helpTopic": "true",
39-
},
43+
Args: cobra.NoArgs,
44+
}
45+
46+
cmd.SetHelpFunc(helpTopicHelpFunc)
47+
cmd.SetUsageFunc(helpTopicUsageFunc)
48+
49+
return cmd
50+
}
51+
52+
func helpTopicHelpFunc(command *cobra.Command, args []string) {
53+
if len(args) >= 2 && args[1] != "--help" && args[1] != "-h" {
54+
command.Printf("unknown command %q for %q\n", args[1], command.CommandPath())
55+
56+
if args[1] == "help" {
57+
command.Print("\nDid you mean this?\n")
58+
command.Printf("\t%s\n\n", "--help")
59+
} else {
60+
command.Printf("\n")
61+
}
62+
63+
helpTopicUsageFunc(command)
64+
command.Printf("\n")
65+
hasFailed = true
66+
return
4067
}
68+
69+
fmt.Fprint(command.OutOrStdout(), command.Long)
70+
}
71+
72+
func helpTopicUsageFunc(command *cobra.Command) error {
73+
command.Printf("Usage: gh help %s", command.Use)
74+
75+
return nil
4176
}

pkg/cmd/root/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ func NewCmdRoot(f *cmdutil.Factory, version, buildDate string) *cobra.Command {
8282

8383
cmdutil.DisableAuthCheck(cmd)
8484

85-
// CHILD COMMANDS
86-
85+
// Child commands
8786
cmd.AddCommand(aliasCmd.NewCmdAlias(f))
8887
cmd.AddCommand(authCmd.NewCmdAuth(f))
8988
cmd.AddCommand(configCmd.NewCmdConfig(f))
9089
cmd.AddCommand(creditsCmd.NewCmdCredits(f, nil))
9190
cmd.AddCommand(gistCmd.NewCmdGist(f))
9291
cmd.AddCommand(NewCmdCompletion(f.IOStreams))
9392

93+
// Help Topics
9494
cmd.AddCommand(NewHelpTopic("environment"))
9595

9696
// the `api` command should not inherit any extra HTTP headers

0 commit comments

Comments
 (0)
X Tutup