list the various alias permutations for the command and subcommands

this occurs from either utlizing the help function for a specific command/subcommand
combination or when 'gh reference' lists the command tree
This commit is contained in:
gabemontero 2024-03-16 11:30:33 -04:00
parent 3b9f9a8f59
commit dd7ea5adff
2 changed files with 37 additions and 1 deletions

View file

@ -132,7 +132,7 @@ func rootHelpFunc(f *cmdutil.Factory, command *cobra.Command, args []string) {
helpEntries = append(helpEntries, helpEntry{"USAGE", command.UseLine()})
if len(command.Aliases) > 0 {
helpEntries = append(helpEntries, helpEntry{"ALIASES", strings.Join(command.Aliases, "\n")})
helpEntries = append(helpEntries, helpEntry{"ALIASES", buildAliases(command, command.Aliases) + "\n"})
}
for _, g := range GroupedCommands(command) {
@ -302,3 +302,35 @@ func dedent(s string) string {
}
return strings.TrimSuffix(buf.String(), "\n")
}
func buildAliases(cmd *cobra.Command, aliasList []string) string {
if len(aliasList) == 0 {
return "No Aliases"
}
if !cmd.HasParent() {
return strings.Join(aliasList, " ")
}
if cmd.Parent().HasParent() {
// prepend cmd.Name so unaliased is first
aliasList = append([]string{cmd.Name()}, aliasList...)
}
list := append(cmd.Parent().Aliases, cmd.Parent().Name())
sort.Strings(list)
sep := ","
newAliasList := []string{}
if !cmd.Parent().HasParent() {
// trim last comma
idx := len(aliasList) - 1
last := aliasList[idx]
last = strings.TrimSuffix(last, ",")
aliasList[idx] = last
sep = ""
}
for _, c := range list {
for _, a := range aliasList {
newAliasList = append(newAliasList, fmt.Sprintf("%s %s%s", c, a, sep))
}
}
return buildAliases(cmd.Parent(), newAliasList)
}

View file

@ -62,6 +62,10 @@ func cmdRef(w io.Writer, cmd *cobra.Command, depth int) {
fmt.Fprintf(w, "```\n%s````\n\n", dedent(flagUsages))
}
// Aliases
fmt.Fprintf(w, "%s\n\n", "Aliases")
fmt.Fprintf(w, "\n%s\n\n", dedent(buildAliases(cmd, cmd.Aliases)))
// Subcommands
for _, c := range cmd.Commands() {
if c.Hidden {