-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
help.go
162 lines (141 loc) · 4.45 KB
/
help.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package command
import (
"fmt"
"regexp"
"strings"
"github.com/cli/cli/utils"
"github.com/spf13/cobra"
)
func rootUsageFunc(command *cobra.Command) error {
command.Printf("Usage: %s", command.UseLine())
subcommands := command.Commands()
if len(subcommands) > 0 {
command.Print("\n\nAvailable commands:\n")
for _, c := range subcommands {
if c.Hidden {
continue
}
command.Printf(" %s\n", c.Name())
}
return nil
}
flagUsages := command.LocalFlags().FlagUsages()
if flagUsages != "" {
command.Printf("\n\nFlags:\n%s", flagUsages)
}
return nil
}
var hasFailed bool
// HasFailed signals that the main process should exit with non-zero status
func HasFailed() bool {
return hasFailed
}
// 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.
func nestedSuggestFunc(command *cobra.Command, arg string) {
command.Printf("unknown command %q for %q\n", arg, command.CommandPath())
var candidates []string
if arg == "help" {
candidates = []string{"--help"}
} else {
if command.SuggestionsMinimumDistance <= 0 {
command.SuggestionsMinimumDistance = 2
}
candidates = command.SuggestionsFor(arg)
}
if len(candidates) > 0 {
command.Print("\nDid you mean this?\n")
for _, c := range candidates {
command.Printf("\t%s\n", c)
}
}
command.Print("\n")
_ = rootUsageFunc(command)
}
func rootHelpFunc(command *cobra.Command, args []string) {
if command.Parent() == RootCmd && len(args) >= 2 && args[1] != "--help" && args[1] != "-h" {
nestedSuggestFunc(command, args[1])
hasFailed = true
return
}
coreCommands := []string{}
additionalCommands := []string{}
for _, c := range command.Commands() {
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)
} 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{}
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")})
}
if len(additionalCommands) > 0 {
helpEntries = append(helpEntries, helpEntry{"ADDITIONAL COMMANDS", strings.Join(additionalCommands, "\n")})
}
flagUsages := command.LocalFlags().FlagUsages()
if flagUsages != "" {
dedent := regexp.MustCompile(`(?m)^ `)
helpEntries = append(helpEntries, helpEntry{"FLAGS", dedent.ReplaceAllString(flagUsages, "")})
}
inheritedFlagUsages := command.InheritedFlags().FlagUsages()
if inheritedFlagUsages != "" {
dedent := regexp.MustCompile(`(?m)^ `)
helpEntries = append(helpEntries, helpEntry{"INHERITED FLAGS", dedent.ReplaceAllString(inheritedFlagUsages, "")})
}
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})
}
helpEntries = append(helpEntries, helpEntry{"LEARN MORE", `
Use "gh <command> <subcommand> --help" for more information about a command.
Read the manual at https://cli.github.com/manual`})
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(strings.Trim(e.Body, "\n\r"), "\n") {
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)
}