From dd7ea5adff6a7cbb51c0f5d2a07d4be4f94cba16 Mon Sep 17 00:00:00 2001 From: gabemontero Date: Sat, 16 Mar 2024 11:30:33 -0400 Subject: [PATCH] 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 --- pkg/cmd/root/help.go | 34 +++++++++++++++++++++++++++++++++- pkg/cmd/root/help_reference.go | 4 ++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/root/help.go b/pkg/cmd/root/help.go index 7180a3152..e2cf4f9e0 100644 --- a/pkg/cmd/root/help.go +++ b/pkg/cmd/root/help.go @@ -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) +} diff --git a/pkg/cmd/root/help_reference.go b/pkg/cmd/root/help_reference.go index 76e9db24e..6bc29066b 100644 --- a/pkg/cmd/root/help_reference.go +++ b/pkg/cmd/root/help_reference.go @@ -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 {