Skip to content

Commit cb3120b

Browse files
committed
refactor: dedup subcmd help code
1 parent 95a2b02 commit cb3120b

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

help.go

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,7 @@ func DefaultShortHelp(c *CommandInfo) string {
181181
}
182182

183183
if len(c.Subcmds) > 0 {
184-
var maxCmdNameLen int
185-
for i := range c.Subcmds {
186-
if n := len(c.Subcmds[i].Name); n > maxCmdNameLen {
187-
maxCmdNameLen = n
188-
}
189-
}
190-
191-
u.WriteString("\ncommands:\n")
192-
for i := range c.Subcmds {
193-
fmt.Fprintf(&u, " %-*s %s\n", maxCmdNameLen, c.Subcmds[i].Name, c.Subcmds[i].HelpBlurb)
194-
}
184+
helpWriteSubcmds(&u, c)
195185
}
196186

197187
return u.String()
@@ -327,17 +317,7 @@ func DefaultFullHelp(c *CommandInfo) string {
327317
}
328318

329319
if len(c.Subcmds) > 0 {
330-
var maxCmdNameLen int
331-
for i := range c.Subcmds {
332-
if n := len(c.Subcmds[i].Name); n > maxCmdNameLen {
333-
maxCmdNameLen = n
334-
}
335-
}
336-
337-
u.WriteString("\ncommands:\n")
338-
for i := range c.Subcmds {
339-
fmt.Fprintf(&u, " %-*s %s\n", maxCmdNameLen, c.Subcmds[i].Name, c.Subcmds[i].HelpBlurb)
340-
}
320+
helpWriteSubcmds(&u, c)
341321
}
342322

343323
return u.String()
@@ -379,6 +359,20 @@ func (o *InputInfo) optUsgArgName() string {
379359
return "<arg>"
380360
}
381361

362+
func helpWriteSubcmds(u *strings.Builder, c *CommandInfo) {
363+
var maxCmdNameLen int
364+
for i := range c.Subcmds {
365+
if n := len(c.Subcmds[i].Name); n > maxCmdNameLen {
366+
maxCmdNameLen = n
367+
}
368+
}
369+
370+
u.WriteString("\ncommands:\n")
371+
for i := range c.Subcmds {
372+
fmt.Fprintf(u, " %-*s %s\n", maxCmdNameLen, c.Subcmds[i].Name, c.Subcmds[i].HelpBlurb)
373+
}
374+
}
375+
382376
func wrapBlurb(v string, indentLen, lineLen int) string {
383377
s := wrapText(v, indentLen, lineLen)
384378
return s[indentLen:]

0 commit comments

Comments
 (0)