renderCommandHelp renders styled help for any non-root command, reading structure from cobra's command tree rather than hardcoding per-command.
(cmd *cobra.Command)
| 142 | // renderCommandHelp renders styled help for any non-root command, reading |
| 143 | // structure from cobra's command tree rather than hardcoding per-command. |
| 144 | func renderCommandHelp(cmd *cobra.Command) { |
| 145 | w := cmd.OutOrStdout() |
| 146 | var b strings.Builder |
| 147 | |
| 148 | // Description |
| 149 | desc := cmd.Long |
| 150 | if desc == "" { |
| 151 | desc = cmd.Short |
| 152 | } |
| 153 | if desc != "" { |
| 154 | b.WriteString(desc) |
| 155 | b.WriteString("\n") |
| 156 | } |
| 157 | |
| 158 | // USAGE |
| 159 | b.WriteString("\n") |
| 160 | b.WriteString(bold.format("USAGE") + "\n") |
| 161 | if cmd.HasAvailableSubCommands() && !cmd.Runnable() { |
| 162 | b.WriteString(" " + cmd.CommandPath() + " <command> [flags]\n") |
| 163 | } else { |
| 164 | b.WriteString(" " + cmd.UseLine() + "\n") |
| 165 | } |
| 166 | |
| 167 | // ALIASES |
| 168 | if len(cmd.Aliases) > 0 { |
| 169 | b.WriteString("\n") |
| 170 | b.WriteString(bold.format("ALIASES") + "\n") |
| 171 | b.WriteString(" " + cmd.Name()) |
| 172 | for _, a := range cmd.Aliases { |
| 173 | b.WriteString(", " + a) |
| 174 | } |
| 175 | b.WriteString("\n") |
| 176 | } |
| 177 | |
| 178 | // COMMANDS |
| 179 | if cmd.HasAvailableSubCommands() { |
| 180 | var entries []helpEntry |
| 181 | maxName := 0 |
| 182 | for _, sub := range cmd.Commands() { |
| 183 | if !sub.IsAvailableCommand() { |
| 184 | continue |
| 185 | } |
| 186 | entries = append(entries, helpEntry{name: sub.Name(), desc: sub.Short}) |
| 187 | if len(sub.Name()) > maxName { |
| 188 | maxName = len(sub.Name()) |
| 189 | } |
| 190 | } |
| 191 | b.WriteString("\n") |
| 192 | b.WriteString(bold.format("COMMANDS") + "\n") |
| 193 | for _, e := range entries { |
| 194 | fmt.Fprintf(&b, " %-*s %s\n", maxName, e.name, e.desc) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // FLAGS — local flags plus parent-scoped persistent flags |
| 199 | merged := pflag.NewFlagSet("flags", pflag.ContinueOnError) |
| 200 | cmd.LocalFlags().VisitAll(func(f *pflag.Flag) { merged.AddFlag(f) }) |
| 201 | parentScopedFlags(cmd).VisitAll(func(f *pflag.Flag) { |
no test coverage detected