MCPcopy Create free account
hub / github.com/cli/cli / WriteHelp

Function WriteHelp

pkg/cmd/root/help.go:125–223  ·  view source on GitHub ↗

WriteHelp renders the full help text for command to w. This is the same output produced by `gh --help`, exposed separately so that callers such as error reporting can render help to a stream of their choosing rather than always writing to stdout. A user-defined alias is resolved to the co

(w io.Writer, cs *iostreams.ColorScheme, command *cobra.Command)

Source from the content-addressed store, hash-verified

123// callers see the target's flags and examples rather than the alias stub. This
124// mirrors the usage and help funcs configured for aliases in NewCmdRoot.
125func WriteHelp(w io.Writer, cs *iostreams.ColorScheme, command *cobra.Command) {
126 command = resolveAliasTarget(command)
127
128 type helpEntry struct {
129 Title string
130 Body string
131 }
132
133 longText := command.Long
134 if longText == "" {
135 longText = command.Short
136 }
137 if longText != "" && command.LocalFlags().Lookup("jq") != nil {
138 longText = strings.TrimRight(longText, "\n") +
139 "\n\nFor more information about output formatting flags, see `gh help formatting`."
140 }
141
142 helpEntries := []helpEntry{}
143 if longText != "" {
144 helpEntries = append(helpEntries, helpEntry{"", longText})
145 }
146 helpEntries = append(helpEntries, helpEntry{"USAGE", command.UseLine()})
147
148 if len(command.Aliases) > 0 {
149 helpEntries = append(helpEntries, helpEntry{"ALIASES", strings.Join(BuildAliasList(command, command.Aliases), ", ") + "\n"})
150 }
151
152 // Statically calculated padding for non-extension commands,
153 // longest is `gh accessibility` with 13 characters + 1 space.
154 //
155 // Should consider novel way to calculate this in the future [AF]
156 namePadding := 14
157
158 for _, g := range GroupedCommands(command) {
159 var names []string
160 for _, c := range g.Commands {
161 names = append(names, rpad(c.Name()+":", namePadding)+c.Short)
162 }
163 helpEntries = append(helpEntries, helpEntry{
164 Title: strings.ToUpper(g.Title),
165 Body: strings.Join(names, "\n"),
166 })
167 }
168
169 if isRootCmd(command) {
170 var helpTopics []string
171 if c := findCommand(command, "accessibility"); c != nil {
172 helpTopics = append(helpTopics, rpad(c.Name()+":", namePadding)+c.Short)
173 }
174 if c := findCommand(command, "actions"); c != nil {
175 helpTopics = append(helpTopics, rpad(c.Name()+":", namePadding)+c.Short)
176 }
177 for _, helpTopic := range HelpTopics {
178 helpTopics = append(helpTopics, rpad(helpTopic.name+":", namePadding)+helpTopic.short)
179 }
180 sort.Strings(helpTopics)
181 helpEntries = append(helpEntries, helpEntry{"HELP TOPICS", strings.Join(helpTopics, "\n")})
182 }

Callers 3

printErrorFunction · 0.92
rootHelpFuncFunction · 0.85

Calls 12

FormatSliceFunction · 0.92
IndentFunction · 0.92
resolveAliasTargetFunction · 0.85
BuildAliasListFunction · 0.85
GroupedCommandsFunction · 0.85
rpadFunction · 0.85
isRootCmdFunction · 0.85
findCommandFunction · 0.85
dedentFunction · 0.85
JoinMethod · 0.80
BoldMethod · 0.80
NameMethod · 0.65

Tested by 1