Skip to content

Commit 7f61b31

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 7f61b31

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-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: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,42 @@ 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+
cmdB := &Command{
177+
Helptext: HelpText{
178+
ShortDescription: "This is other short",
179+
},
180+
}
181+
cmdA := &Command{
182+
Helptext: HelpText{
183+
ShortDescription: "This is short",
184+
},
185+
Subcommands: map[string]*Command{
186+
"a": cmdB,
187+
},
188+
}
189+
cmdA.ProcessHelp()
190+
if len(cmdA.Helptext.LongDescription) == 0 {
191+
t.Error("LongDescription was not set on basis of ShortDescription")
192+
}
193+
if len(cmdB.Helptext.LongDescription) == 0 {
194+
t.Error("LongDescription was not set on basis of ShortDescription")
195+
}
196+
}

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)