Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list the various alias permutations for the command and subcommands, via '--help' and 'gh reference' #8824

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/docs/docs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func init() {

jsonCmd.Flags().StringSlice("json", nil, "help message for flag json")

aliasCmd.Flags().StringSlice("yang", nil, "help message for flag yang")

echoCmd.AddCommand(timesCmd, echoSubCmd, deprecatedCmd)
rootCmd.AddCommand(printCmd, echoCmd, dummyCmd)
}
Expand Down Expand Up @@ -75,6 +77,13 @@ var printCmd = &cobra.Command{
Long: `an absolutely utterly useless command for testing.`,
}

var aliasCmd = &cobra.Command{
Use: "ying [yang]",
Short: "The ying and yang of it all",
Long: "an absolutely utterly useless command for testing aliases!.",
Aliases: []string{"yoo", "foo"},
}

var jsonCmd = &cobra.Command{
Use: "blah --json <fields>",
Short: "View details in JSON",
Expand Down
9 changes: 9 additions & 0 deletions internal/docs/man.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,14 @@ func manPrintOptions(buf *bytes.Buffer, command *cobra.Command) {
}
}

func manPrintAliases(buf *bytes.Buffer, command *cobra.Command) {
if len(command.Aliases) > 0 {
buf.WriteString("# ALIASES\n")
buf.WriteString(strings.Join(root.BuildAliasList(command, command.Aliases), ", "))
buf.WriteString("\n")
}
}

func manPrintJSONFields(buf *bytes.Buffer, command *cobra.Command) {
raw, ok := command.Annotations["help:json-fields"]
if !ok {
Expand Down Expand Up @@ -207,6 +215,7 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
}
}
manPrintOptions(buf, cmd)
manPrintAliases(buf, cmd)
manPrintJSONFields(buf, cmd)
if len(cmd.Example) > 0 {
buf.WriteString("# EXAMPLE\n")
Expand Down
15 changes: 15 additions & 0 deletions internal/docs/man_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,21 @@ func TestGenManSeeAlso(t *testing.T) {
}
}

func TestGenManAliases(t *testing.T) {
buf := new(bytes.Buffer)
header := &GenManHeader{}
if err := renderMan(aliasCmd, header, buf); err != nil {
t.Fatal(err)
}

output := buf.String()

checkStringContains(t, output, translate(aliasCmd.Name()))
checkStringContains(t, output, "ALIASES")
checkStringContains(t, output, "foo")
checkStringContains(t, output, "yoo")
}

func TestGenManJSONFields(t *testing.T) {
buf := new(bytes.Buffer)
header := &GenManHeader{}
Expand Down
10 changes: 10 additions & 0 deletions internal/docs/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ func printJSONFields(w io.Writer, cmd *cobra.Command) {
fmt.Fprint(w, "\n\n")
}

func printAliases(w io.Writer, cmd *cobra.Command) {
if len(cmd.Aliases) > 0 {
fmt.Fprintf(w, "### ALIASES\n\n")
fmt.Fprint(w, text.FormatSlice(strings.Split(strings.Join(root.BuildAliasList(cmd, cmd.Aliases), ", "), ","), 0, 0, "", "", true))
fmt.Fprint(w, "\n\n")
}

}

func printOptions(w io.Writer, cmd *cobra.Command) error {
flags := cmd.NonInheritedFlags()
flags.SetOutput(w)
Expand Down Expand Up @@ -147,6 +156,7 @@ func genMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string)
if err := printOptions(w, cmd); err != nil {
return err
}
printAliases(w, cmd)
printJSONFields(w, cmd)
fmt.Fprint(w, "{% endraw %}\n")

Expand Down
14 changes: 14 additions & 0 deletions internal/docs/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ func TestGenMdNoHiddenParents(t *testing.T) {
checkStringOmits(t, output, "Options inherited from parent commands")
}

func TestGenMdAliases(t *testing.T) {
buf := new(bytes.Buffer)
if err := genMarkdownCustom(aliasCmd, buf, nil); err != nil {
t.Fatal(err)
}
output := buf.String()

checkStringContains(t, output, aliasCmd.Long)
checkStringContains(t, output, jsonCmd.Example)
checkStringContains(t, output, "ALIASES")
checkStringContains(t, output, "yoo")
checkStringContains(t, output, "foo")
}

func TestGenMdJSONFields(t *testing.T) {
buf := new(bytes.Buffer)
if err := genMarkdownCustom(jsonCmd, buf, nil); err != nil {
Expand Down
23 changes: 22 additions & 1 deletion pkg/cmd/root/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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", strings.Join(BuildAliasList(command, command.Aliases), ", ") + "\n"})
}

for _, g := range GroupedCommands(command) {
Expand Down Expand Up @@ -302,3 +302,24 @@ func dedent(s string) string {
}
return strings.TrimSuffix(buf.String(), "\n")
}

func BuildAliasList(cmd *cobra.Command, aliases []string) []string {
if !cmd.HasParent() {
return aliases
}

parentAliases := append(cmd.Parent().Aliases, cmd.Parent().Name())
sort.Strings(parentAliases)

var aliasesWithParentAliases []string
// e.g aliases = [ls]
for _, alias := range aliases {
// e.g parentAliases = [codespaces, cs]
for _, parentAlias := range parentAliases {
// e.g. aliasesWithParentAliases = [codespaces list, codespaces ls, cs list, cs ls]
aliasesWithParentAliases = append(aliasesWithParentAliases, fmt.Sprintf("%s %s", parentAlias, alias))
}
}

return BuildAliasList(cmd.Parent(), aliasesWithParentAliases)
}
6 changes: 6 additions & 0 deletions pkg/cmd/root/help_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ func cmdRef(w io.Writer, cmd *cobra.Command, depth int) {
fmt.Fprintf(w, "```\n%s````\n\n", dedent(flagUsages))
}

// Aliases
if len(cmd.Aliases) > 0 {
fmt.Fprintf(w, "%s\n\n", "Aliases")
fmt.Fprintf(w, "\n%s\n\n", dedent(strings.Join(BuildAliasList(cmd, cmd.Aliases), ", ")))
}

// Subcommands
for _, c := range cmd.Commands() {
if c.Hidden {
Expand Down
Loading