Merge pull request #3693 from thaJeztah/cli_usage_aliases

cli: print full command as aliases in usage output
This commit is contained in:
Sebastiaan van Stijn 2022-06-28 17:31:30 +02:00 committed by GitHub
commit 05d288eb06
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 2 deletions

View File

@ -37,6 +37,7 @@ func setupCommonRootCommand(rootCmd *cobra.Command) (*cliflags.ClientOptions, *p
cobra.AddTemplateFunc("hasSwarmSubCommands", hasSwarmSubCommands) cobra.AddTemplateFunc("hasSwarmSubCommands", hasSwarmSubCommands)
cobra.AddTemplateFunc("hasInvalidPlugins", hasInvalidPlugins) cobra.AddTemplateFunc("hasInvalidPlugins", hasInvalidPlugins)
cobra.AddTemplateFunc("topCommands", topCommands) cobra.AddTemplateFunc("topCommands", topCommands)
cobra.AddTemplateFunc("commandAliases", commandAliases)
cobra.AddTemplateFunc("operationSubCommands", operationSubCommands) cobra.AddTemplateFunc("operationSubCommands", operationSubCommands)
cobra.AddTemplateFunc("managementSubCommands", managementSubCommands) cobra.AddTemplateFunc("managementSubCommands", managementSubCommands)
cobra.AddTemplateFunc("orchestratorSubCommands", orchestratorSubCommands) cobra.AddTemplateFunc("orchestratorSubCommands", orchestratorSubCommands)
@ -258,6 +259,21 @@ func hasTopCommands(cmd *cobra.Command) bool {
return len(topCommands(cmd)) > 0 return len(topCommands(cmd)) > 0
} }
// commandAliases is a templating function to return aliases for the command,
// formatted as the full command as they're called (contrary to the default
// Aliases function, which only returns the subcommand).
func commandAliases(cmd *cobra.Command) string {
var parentPath string
if cmd.HasParent() {
parentPath = cmd.Parent().CommandPath() + " "
}
aliases := cmd.CommandPath()
for _, alias := range cmd.Aliases {
aliases += ", " + parentPath + alias
}
return aliases
}
func topCommands(cmd *cobra.Command) []*cobra.Command { func topCommands(cmd *cobra.Command) []*cobra.Command {
cmds := []*cobra.Command{} cmds := []*cobra.Command{}
if cmd.Parent() != nil { if cmd.Parent() != nil {
@ -398,7 +414,7 @@ EXPERIMENTAL:
{{- if gt .Aliases 0}} {{- if gt .Aliases 0}}
Aliases: Aliases:
{{.NameAndAliases}} {{ commandAliases . }}
{{- end}} {{- end}}
{{- if .HasExample}} {{- if .HasExample}}

View File

@ -78,6 +78,14 @@ func TestInvalidPlugin(t *testing.T) {
assert.DeepEqual(t, invalidPlugins(root), []*cobra.Command{sub1}, cmpopts.IgnoreUnexported(cobra.Command{})) assert.DeepEqual(t, invalidPlugins(root), []*cobra.Command{sub1}, cmpopts.IgnoreUnexported(cobra.Command{}))
} }
func TestCommandAliases(t *testing.T) {
root := &cobra.Command{Use: "root"}
sub := &cobra.Command{Use: "subcommand", Aliases: []string{"alias1", "alias2"}}
root.AddCommand(sub)
assert.Equal(t, commandAliases(sub), "root subcommand, root alias1, root alias2")
}
func TestDecoratedName(t *testing.T) { func TestDecoratedName(t *testing.T) {
root := &cobra.Command{Use: "root"} root := &cobra.Command{Use: "root"}
topLevelCommand := &cobra.Command{Use: "pluginTopLevelCommand"} topLevelCommand := &cobra.Command{Use: "pluginTopLevelCommand"}

View File

@ -4,7 +4,7 @@ Usage: docker stack deploy [OPTIONS] STACK
Deploy a new stack or update an existing stack Deploy a new stack or update an existing stack
Aliases: Aliases:
deploy, up docker stack deploy, docker stack up
Options: Options:
-c, --compose-file strings Path to a Compose file, or "-" to read -c, --compose-file strings Path to a Compose file, or "-" to read