diff --git a/internal/core/cobra_builder.go b/internal/core/cobra_builder.go index 2f3264efb9..30f0cc69b7 100644 --- a/internal/core/cobra_builder.go +++ b/internal/core/cobra_builder.go @@ -99,13 +99,13 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command) { cobraCmd.SetUsageTemplate(usageTemplate) - if cobraCmd.Annotations == nil { - cobraCmd.Annotations = make(map[string]string) - } - // Use a custom function to print usage // This function will build usage to avoid building it for each commands cobraCmd.SetUsageFunc(usageFuncBuilder(cobraCmd, func() { + if cobraCmd.Annotations == nil { + cobraCmd.Annotations = make(map[string]string) + } + if cmd.ArgsType != nil { cobraCmd.Annotations["UsageArgs"] = buildUsageArgs(b.ctx, cmd, false) } @@ -121,6 +121,8 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command) { if cmd.SeeAlsos != nil { cobraCmd.Annotations["SeeAlsos"] = cmd.seeAlsosAsStr() } + + cobraCmd.Annotations["CommandUsage"] = cmd.GetUsage(ExtractBinaryName(b.ctx), b.commands) })) if cmd.Run != nil { @@ -140,8 +142,6 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command) { if cmd.WaitFunc != nil { cobraCmd.PersistentFlags().BoolP("wait", "w", false, "wait until the "+cmd.Resource+" is ready") } - - cobraCmd.Annotations["CommandUsage"] = cmd.GetUsage(ExtractBinaryName(b.ctx), b.commands) } const usageTemplate = `USAGE: