@@ -100,21 +100,25 @@ func (b *cobraBuilder) hydrateCobra(cobraCmd *cobra.Command, cmd *Command) {
100100 cobraCmd .Annotations = make (map [string ]string )
101101 }
102102
103- if cmd .ArgsType != nil {
104- cobraCmd .Annotations ["UsageArgs" ] = buildUsageArgs (b .ctx , cmd , false )
105- }
103+ // Use a custom function to print usage
104+ // This function will build usage to avoid building it for each commands
105+ cobraCmd .SetUsageFunc (usageFuncBuilder (cobraCmd , func () {
106+ if cmd .ArgsType != nil {
107+ cobraCmd .Annotations ["UsageArgs" ] = buildUsageArgs (b .ctx , cmd , false )
108+ }
106109
107- if cmd .ArgSpecs != nil {
108- cobraCmd .Annotations ["UsageDeprecatedArgs" ] = buildUsageArgs (b .ctx , cmd , true )
109- }
110+ if cmd .ArgSpecs != nil {
111+ cobraCmd .Annotations ["UsageDeprecatedArgs" ] = buildUsageArgs (b .ctx , cmd , true )
112+ }
110113
111- if cmd .Examples != nil {
112- cobraCmd .Annotations ["Examples" ] = buildExamples (b .meta .BinaryName , cmd )
113- }
114+ if cmd .Examples != nil {
115+ cobraCmd .Annotations ["Examples" ] = buildExamples (b .meta .BinaryName , cmd )
116+ }
114117
115- if cmd .SeeAlsos != nil {
116- cobraCmd .Annotations ["SeeAlsos" ] = cmd .seeAlsosAsStr ()
117- }
118+ if cmd .SeeAlsos != nil {
119+ cobraCmd .Annotations ["SeeAlsos" ] = cmd .seeAlsosAsStr ()
120+ }
121+ }))
118122
119123 if cmd .Run != nil {
120124 cobraCmd .RunE = cobraRun (b .ctx , cmd )
0 commit comments