Skip to content

Commit 23f22d3

Browse files
committed
use full command as aliases
The default output for Cobra aliases only shows the subcommand as alias, which is not very intuitive. This patch changes the output to use the full command as it would be called by the user. Before this patch: aliases: build, b After this patch: aliases: docker buildx build, docker buildx b Note that there's still some improvements to be made; due to how aliases must be set-up in Cobra, aliases at different "levels" are still not shown. So for example, `docker build --help` will not show `docker buildx build` as alias, and vice-versa. This will require additional changes, and can possibly be resolved using custom metadata/annotations. Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent b4dab44 commit 23f22d3

5 files changed

Lines changed: 22 additions & 8 deletions

File tree

clidocstool.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,19 @@ func copyFile(src string, dst string) error {
9797
_, err = io.Copy(df, sf)
9898
return err
9999
}
100+
101+
func getAliases(cmd *cobra.Command) []string {
102+
if len(cmd.Aliases) == 0 {
103+
return cmd.Aliases
104+
}
105+
106+
var parentPath string
107+
if cmd.HasParent() {
108+
parentPath = cmd.Parent().CommandPath() + " "
109+
}
110+
aliases := []string{cmd.CommandPath()}
111+
for _, a := range cmd.Aliases {
112+
aliases = append(aliases, parentPath+a)
113+
}
114+
return aliases
115+
}

clidocstool_md.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,9 @@ func mdCmdOutput(cmd *cobra.Command, old string) (string, error) {
155155
fmt.Fprintf(b, "%s\n\n", desc)
156156
}
157157

158-
if len(cmd.Aliases) != 0 {
159-
fmt.Fprintf(b, "### Aliases\n\n`%s`", cmd.Name())
160-
for _, a := range cmd.Aliases {
161-
fmt.Fprintf(b, ", `%s`", a)
162-
}
158+
if aliases := getAliases(cmd); len(aliases) != 0 {
159+
fmt.Fprint(b, "### Aliases\n\n")
160+
fmt.Fprint(b, "`"+strings.Join(aliases, "`, `")+"`")
163161
fmt.Fprint(b, "\n\n")
164162
}
165163

clidocstool_yaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (c *Client) genYamlCustom(cmd *cobra.Command, w io.Writer) error {
150150

151151
cliDoc := cmdDoc{
152152
Name: cmd.CommandPath(),
153-
Aliases: strings.Join(cmd.Aliases, ", "),
153+
Aliases: strings.Join(getAliases(cmd), ", "),
154154
Short: forceMultiLine(cmd.Short, shortMaxWidth),
155155
Long: forceMultiLine(cmd.Long, longMaxWidth),
156156
Example: cmd.Example,

fixtures/buildx_build.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Start a build
55

66
### Aliases
77

8-
`build`, `b`
8+
`docker buildx build`, `docker buildx b`
99

1010
### Options
1111

fixtures/docker_buildx_build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
command: docker buildx build
2-
aliases: b
2+
aliases: docker buildx build, docker buildx b
33
short: Start a build
44
long: Start a build
55
usage: docker buildx build [OPTIONS] PATH | URL | -

0 commit comments

Comments
 (0)