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)
| 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. |
| 125 | func 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 | } |