File tree 3 files changed +60
-0
lines changed
3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change @@ -258,6 +258,25 @@ func (c *Command) Subcommand(id string) *Command {
258
258
return c .Subcommands [id ]
259
259
}
260
260
261
+ type CommandVisitor func (* Command )
262
+
263
+ // Walks tree of all subcommands (including this one)
264
+ func (c * Command ) Walk (visitor CommandVisitor ) {
265
+ visitor (c )
266
+ for _ , cm := range c .Subcommands {
267
+ cm .Walk (visitor )
268
+ }
269
+ }
270
+
271
+ func (c * Command ) ProcessHelp () {
272
+ c .Walk (func (cm * Command ) {
273
+ ht := & cm .Helptext
274
+ if len (ht .LongDescription ) == 0 {
275
+ ht .LongDescription = ht .ShortDescription
276
+ }
277
+ })
278
+ }
279
+
261
280
// checkArgValue returns an error if a given arg value is not valid for the
262
281
// given Argument
263
282
func checkArgValue (v string , found bool , def Argument ) error {
Original file line number Diff line number Diff line change @@ -155,3 +155,43 @@ func TestResolving(t *testing.T) {
155
155
t .Error ("Returned command path is different than expected" , cmds )
156
156
}
157
157
}
158
+
159
+ func TestWalking (t * testing.T ) {
160
+ cmdA := & Command {
161
+ Subcommands : map [string ]* Command {
162
+ "b" : & Command {},
163
+ "B" : & Command {},
164
+ },
165
+ }
166
+ i := 0
167
+ cmdA .Walk (func (c * Command ) {
168
+ i = i + 1
169
+ })
170
+ if i != 3 {
171
+ t .Error ("Command tree walk didn't work, expected 3 got:" , i )
172
+ }
173
+ }
174
+
175
+ func TestHelpProcessing (t * testing.T ) {
176
+
177
+ cmdB := & Command {
178
+ Helptext : HelpText {
179
+ ShortDescription : "This is other short" ,
180
+ },
181
+ }
182
+ cmdA := & Command {
183
+ Helptext : HelpText {
184
+ ShortDescription : "This is short" ,
185
+ },
186
+ Subcommands : map [string ]* Command {
187
+ "a" : cmdB ,
188
+ },
189
+ }
190
+ cmdA .ProcessHelp ()
191
+ if len (cmdA .Helptext .LongDescription ) == 0 {
192
+ t .Error ("LongDescription was not set on basis of ShortDescription" )
193
+ }
194
+ if len (cmdB .Helptext .LongDescription ) == 0 {
195
+ t .Error ("LongDescription was not set on basis of ShortDescription" )
196
+ }
197
+ }
Original file line number Diff line number Diff line change @@ -153,6 +153,7 @@ var rootROSubcommands = map[string]*cmds.Command{
153
153
}
154
154
155
155
func init () {
156
+ Root .ProcessHelp ()
156
157
* RootRO = * Root
157
158
158
159
// sanitize readonly refs command
You can’t perform that action at this time.
0 commit comments