Skip to content

Commit b507b7c

Browse files
committed
Add logic for copying ShortDes to LongDesc if it is not present
This is the best place for inserting it that I found. Test in #2648 should be modified to run `Root.ProcessHelp()`. License: MIT Signed-off-by: Jakub Sztandera <[email protected]>
1 parent d183e34 commit b507b7c

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

commands/command.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,25 @@ func (c *Command) Subcommand(id string) *Command {
258258
return c.Subcommands[id]
259259
}
260260

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+
261280
// checkArgValue returns an error if a given arg value is not valid for the
262281
// given Argument
263282
func checkArgValue(v string, found bool, def Argument) error {

commands/command_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,43 @@ func TestResolving(t *testing.T) {
155155
t.Error("Returned command path is different than expected", cmds)
156156
}
157157
}
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+
}

core/commands/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ var rootROSubcommands = map[string]*cmds.Command{
153153
}
154154

155155
func init() {
156+
Root.ProcessHelp()
156157
*RootRO = *Root
157158

158159
// sanitize readonly refs command

0 commit comments

Comments
 (0)