Skip to content

Commit 4dead11

Browse files
committed
cli: print full command as aliases in usage output
The default output for Cobra aliases only shows the subcommand as alias, which is not very intuitive. This patch changes the output to print the full command as it would be called by the user. 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 ps --help` will not show `docker container ps` as alias, and vice-versa. This will require additional changes, and can possibly be resolved using custom metadata/annotations. Before this patch: docker container ls --help Usage: docker container ls [OPTIONS] List containers Aliases: ls, ps, list After this patch: docker container ls --help Usage: docker container ls [OPTIONS] List containers Aliases: docker container ls, docker container ps, docker container list Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent b496125 commit 4dead11

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

cli/cobra.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *p
3737
cobra.AddTemplateFunc("hasSwarmSubCommands", hasSwarmSubCommands)
3838
cobra.AddTemplateFunc("hasInvalidPlugins", hasInvalidPlugins)
3939
cobra.AddTemplateFunc("topCommands", topCommands)
40+
cobra.AddTemplateFunc("commandAliases", commandAliases)
4041
cobra.AddTemplateFunc("operationSubCommands", operationSubCommands)
4142
cobra.AddTemplateFunc("managementSubCommands", managementSubCommands)
4243
cobra.AddTemplateFunc("orchestratorSubCommands", orchestratorSubCommands)
@@ -258,6 +259,21 @@ func hasTopCommands(cmd *cobra.Command) bool {
258259
return len(topCommands(cmd)) > 0
259260
}
260261

262+
// commandAliases is a templating function to return aliases for the command,
263+
// formatted as the full command as they're called (contrary to the default
264+
// Aliases function, which only returns the subcommand).
265+
func commandAliases(cmd *cobra.Command) string {
266+
var parentPath string
267+
if cmd.HasParent() {
268+
parentPath = cmd.Parent().CommandPath() + " "
269+
}
270+
aliases := cmd.CommandPath()
271+
for _, alias := range cmd.Aliases {
272+
aliases += ", " + parentPath + alias
273+
}
274+
return aliases
275+
}
276+
261277
func topCommands(cmd *cobra.Command) []*cobra.Command {
262278
cmds := []*cobra.Command{}
263279
if cmd.Parent() != nil {
@@ -398,7 +414,7 @@ EXPERIMENTAL:
398414
{{- if gt .Aliases 0}}
399415
400416
Aliases:
401-
{{.NameAndAliases}}
417+
{{ commandAliases . }}
402418
403419
{{- end}}
404420
{{- if .HasExample}}

cli/cobra_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ func TestInvalidPlugin(t *testing.T) {
7878
assert.DeepEqual(t, invalidPlugins(root), []*cobra.Command{sub1}, cmpopts.IgnoreUnexported(cobra.Command{}))
7979
}
8080

81+
func TestCommandAliases(t *testing.T) {
82+
root := &cobra.Command{Use: "root"}
83+
sub := &cobra.Command{Use: "subcommand", Aliases: []string{"alias1", "alias2"}}
84+
root.AddCommand(sub)
85+
86+
assert.Equal(t, commandAliases(sub), "root subcommand, root alias1, root alias2")
87+
}
88+
8189
func TestDecoratedName(t *testing.T) {
8290
root := &cobra.Command{Use: "root"}
8391
topLevelCommand := &cobra.Command{Use: "pluginTopLevelCommand"}

0 commit comments

Comments
 (0)