Factor out command help into functions.

This commit is contained in:
Alec Thomas
2019-01-24 17:11:54 +11:00
parent 2a90ca2ad3
commit 42ea64b1b4
2 changed files with 58 additions and 29 deletions
+52 -23
View File
@@ -126,10 +126,40 @@ func printNodeDetail(w *helpWriter, node *Node, hide bool) {
cmds := node.Leaves(hide) cmds := node.Leaves(hide)
if len(cmds) > 0 { if len(cmds) > 0 {
w.Print("") w.Print("")
w.Print("Commands:")
if w.Tree { if w.Tree {
w.Print("Command Tree:") writeCommandTree(w, node)
} else {
iw := w.Indent() iw := w.Indent()
var rows [][2]string if w.Compact {
writeCompactCommandList(cmds, iw)
} else {
writeCommandList(cmds, iw)
}
}
}
}
func writeCommandList(cmds []*Node, iw *helpWriter) {
for i, cmd := range cmds {
printCommandSummary(iw, cmd)
if i != len(cmds)-1 {
iw.Print("")
}
}
}
func writeCompactCommandList(cmds []*Node, iw *helpWriter) {
rows := [][2]string{}
for _, cmd := range cmds {
rows = append(rows, [2]string{cmd.Path(), cmd.Help})
}
writeTwoColumns(iw, defaultColumnPadding, rows)
}
func writeCommandTree(w *helpWriter, node *Node) {
iw := w.Indent()
rows := make([][2]string, 0, len(node.Children)*2)
for i, cmd := range node.Children { for i, cmd := range node.Children {
rows = append(rows, w.commandTree(cmd, "")...) rows = append(rows, w.commandTree(cmd, "")...)
if i != len(node.Children)-1 { if i != len(node.Children)-1 {
@@ -137,25 +167,26 @@ func printNodeDetail(w *helpWriter, node *Node, hide bool) {
} }
} }
writeTwoColumns(iw, defaultColumnPadding, rows) writeTwoColumns(iw, defaultColumnPadding, rows)
} else { }
w.Print("Commands:")
iw := w.Indent() type helpCommandGroup struct {
if w.Compact { Name string
rows := [][2]string{} Commands []*Node
for _, cmd := range cmds { }
rows = append(rows, [2]string{cmd.Path(), cmd.Help})
} func collectCommandGroups(nodes []*Node) []helpCommandGroup { // nolint: deadcode
writeTwoColumns(iw, defaultColumnPadding, rows) groups := map[string][]*Node{}
} else { for _, node := range nodes {
for i, cmd := range cmds { groups[node.Group] = append(groups[node.Group], node)
printCommandSummary(iw, cmd)
if i != len(cmds)-1 {
iw.Print("")
}
}
} }
out := []helpCommandGroup{}
for name, nodes := range groups {
if name == "" {
name = "Commands"
} }
out = append(out, helpCommandGroup{Name: name, Commands: nodes})
} }
return out
} }
func printCommandSummary(w *helpWriter, cmd *Command) { func printCommandSummary(w *helpWriter, cmd *Command) {
@@ -330,20 +361,18 @@ func SpaceIndenter(prefix string) string {
return prefix + strings.Repeat(" ", defaultIndent) return prefix + strings.Repeat(" ", defaultIndent)
} }
// SpaceIndenter adds line points to every new indent. // LineIndenter adds line points to every new indent.
func LineIndenter(prefix string) string { func LineIndenter(prefix string) string {
if prefix == "" { if prefix == "" {
return "- " return "- "
} else {
return strings.Repeat(" ", defaultIndent) + prefix
} }
return strings.Repeat(" ", defaultIndent) + prefix
} }
// TreeIndenter adds line points to every new indent and vertical lines to every layer. // TreeIndenter adds line points to every new indent and vertical lines to every layer.
func TreeIndenter(prefix string) string { func TreeIndenter(prefix string) string {
if prefix == "" { if prefix == "" {
return "|- " return "|- "
} else {
return "|" + strings.Repeat(" ", defaultIndent) + prefix
} }
return "|" + strings.Repeat(" ", defaultIndent) + prefix
} }
+2 -2
View File
@@ -174,7 +174,7 @@ A test app.
Flags: Flags:
--help Show context-sensitive help. --help Show context-sensitive help.
Command Tree: Commands:
one subcommand one one subcommand one
- thing subcommand thing - thing subcommand thing
- <arg> argument - <arg> argument
@@ -207,7 +207,7 @@ subcommand one
Flags: Flags:
--help Show context-sensitive help. --help Show context-sensitive help.
Command Tree: Commands:
thing subcommand thing thing subcommand thing
- <arg> argument - <arg> argument