diff --git a/pkg/cmd/templates/templater.go b/pkg/cmd/templates/templater.go index 499d0741b4f2..adb5e05de152 100644 --- a/pkg/cmd/templates/templater.go +++ b/pkg/cmd/templates/templater.go @@ -76,6 +76,7 @@ type FlagExposer interface { ExposeFlags(cmd *cobra.Command, flags ...string) FlagExposer } +// ActsAsRootCommand displays command usage using a root command usage template func ActsAsRootCommand(cmd *cobra.Command, filters []string, groups ...CommandGroup) FlagExposer { if cmd == nil { panic("nil root command") @@ -192,14 +193,25 @@ func (t *templater) optionsCmdFor(c *cobra.Command) string { if !c.Runnable() { return "" } - rootCmdStructure := t.parents(c) - for i := len(rootCmdStructure) - 1; i >= 0; i-- { - cmd := rootCmdStructure[i] - if _, _, err := cmd.Find([]string{"options"}); err == nil { - return cmd.CommandPath() + " options" + + parentCmdHasOptsArg := false + currentCmdHasOptsArg := false + + if t.RootCmd.HasParent() { + if _, _, err := t.RootCmd.Parent().Find([]string{"options"}); err == nil { + parentCmdHasOptsArg = true } } - return "" + + if _, _, err := t.RootCmd.Find([]string{"options"}); err == nil { + currentCmdHasOptsArg = true + } + + if (parentCmdHasOptsArg && currentCmdHasOptsArg) || !t.RootCmd.HasParent() { + return t.RootCmd.CommandPath() + " options" + } + + return t.RootCmd.Parent().CommandPath() + " options" } func (t *templater) usageLine(c *cobra.Command) string { diff --git a/pkg/cmd/templates/templates.go b/pkg/cmd/templates/templates.go index bc9cbf57258a..a9a3b2a434a7 100644 --- a/pkg/cmd/templates/templates.go +++ b/pkg/cmd/templates/templates.go @@ -49,7 +49,7 @@ Examples: {{end}}{{end}}{{ if or $visibleFlags.HasFlags $explicitlyExposedFlags.HasFlags}} Options: {{ if $visibleFlags.HasFlags}}{{flagsUsages $visibleFlags}}{{end}}{{ if $explicitlyExposedFlags.HasFlags}}{{flagsUsages $explicitlyExposedFlags}}{{end}}{{end}}{{ if .HasSubCommands }} -Use "{{$rootCmd}} help " for more information about a given command.{{end}}{{ if $optionsCmdFor}} +Use "{{$rootCmd}} --help" for more information about a given command.{{end}}{{ if $optionsCmdFor}} Use "{{$optionsCmdFor}}" for a list of global command-line options (applies to all commands).{{end}}` optionsHelpTemplate = ``