@@ -9,6 +9,11 @@ import (
99// SetupRootCommand sets default usage, help, and error handling for the
1010// root command.
1111func SetupRootCommand (rootCmd * cobra.Command ) {
12+ cobra .AddTemplateFunc ("hasSubCommands" , hasSubCommands )
13+ cobra .AddTemplateFunc ("hasManagementSubCommands" , hasManagementSubCommands )
14+ cobra .AddTemplateFunc ("operationSubCommands" , operationSubCommands )
15+ cobra .AddTemplateFunc ("managementSubCommands" , managementSubCommands )
16+
1217 rootCmd .SetUsageTemplate (usageTemplate )
1318 rootCmd .SetHelpTemplate (helpTemplate )
1419 rootCmd .SetFlagErrorFunc (FlagErrorFunc )
@@ -34,23 +39,81 @@ func FlagErrorFunc(cmd *cobra.Command, err error) error {
3439 }
3540}
3641
37- var usageTemplate = `Usage: {{if not .HasSubCommands}}{{.UseLine}}{{end}}{{if .HasSubCommands}}{{ .CommandPath}} COMMAND{{end}}
42+ func hasSubCommands (cmd * cobra.Command ) bool {
43+ return len (operationSubCommands (cmd )) > 0
44+ }
45+
46+ func hasManagementSubCommands (cmd * cobra.Command ) bool {
47+ return len (managementSubCommands (cmd )) > 0
48+ }
49+
50+ func operationSubCommands (cmd * cobra.Command ) []* cobra.Command {
51+ cmds := []* cobra.Command {}
52+ for _ , sub := range cmd .Commands () {
53+ if sub .IsAvailableCommand () && ! sub .HasSubCommands () {
54+ cmds = append (cmds , sub )
55+ }
56+ }
57+ return cmds
58+ }
59+
60+ func managementSubCommands (cmd * cobra.Command ) []* cobra.Command {
61+ cmds := []* cobra.Command {}
62+ for _ , sub := range cmd .Commands () {
63+ if sub .IsAvailableCommand () && sub .HasSubCommands () {
64+ cmds = append (cmds , sub )
65+ }
66+ }
67+ return cmds
68+ }
69+
70+ var usageTemplate = `Usage:
3871
39- {{ .Short | trim }}{{if gt .Aliases 0}}
72+ {{- if not .HasSubCommands}} {{.UseLine}}{{end}}
73+ {{- if .HasSubCommands}} {{ .CommandPath}} COMMAND{{end}}
74+
75+ {{ .Short | trim }}
76+
77+ {{- if gt .Aliases 0}}
4078
4179Aliases:
42- {{.NameAndAliases}}{{end}}{{if .HasExample}}
80+ {{.NameAndAliases}}
81+
82+ {{- end}}
83+ {{- if .HasExample}}
4384
4485Examples:
45- {{ .Example }}{{end}}{{if .HasFlags}}
86+ {{ .Example }}
87+
88+ {{- end}}
89+ {{- if .HasFlags}}
4690
4791Options:
48- {{.Flags.FlagUsages | trimRightSpace}}{{end}}{{ if .HasAvailableSubCommands}}
92+ {{.Flags.FlagUsages | trimRightSpace}}
93+
94+ {{- end}}
95+ {{- if hasManagementSubCommands . }}
96+
97+ Management Commands:
98+
99+ {{- range managementSubCommands . }}
100+ {{rpad .Name .NamePadding }} {{.Short}}
101+ {{- end}}
102+
103+ {{- end}}
104+ {{- if hasSubCommands .}}
105+
106+ Commands:
107+
108+ {{- range operationSubCommands . }}
109+ {{rpad .Name .NamePadding }} {{.Short}}
110+ {{- end}}
111+ {{- end}}
49112
50- Commands:{{range .Commands}}{{if .IsAvailableCommand}}
51- {{rpad .Name .NamePadding }} {{.Short}}{{end}}{{end}}{{end}}{{ if .HasSubCommands }}
113+ {{- if .HasSubCommands }}
52114
53- Run '{{.CommandPath}} COMMAND --help' for more information on a command.{{end}}
115+ Run '{{.CommandPath}} COMMAND --help' for more information on a command.
116+ {{- end}}
54117`
55118
56119var helpTemplate = `
0 commit comments