Factor out command help into functions.
This commit is contained in:
@@ -126,38 +126,69 @@ 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)
|
||||||
iw := w.Indent()
|
|
||||||
var rows [][2]string
|
|
||||||
for i, cmd := range node.Children {
|
|
||||||
rows = append(rows, w.commandTree(cmd, "")...)
|
|
||||||
if i != len(node.Children)-1 {
|
|
||||||
rows = append(rows, [2]string{"", ""})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
writeTwoColumns(iw, defaultColumnPadding, rows)
|
|
||||||
} else {
|
} else {
|
||||||
w.Print("Commands:")
|
|
||||||
iw := w.Indent()
|
iw := w.Indent()
|
||||||
if w.Compact {
|
if w.Compact {
|
||||||
rows := [][2]string{}
|
writeCompactCommandList(cmds, iw)
|
||||||
for _, cmd := range cmds {
|
|
||||||
rows = append(rows, [2]string{cmd.Path(), cmd.Help})
|
|
||||||
}
|
|
||||||
writeTwoColumns(iw, defaultColumnPadding, rows)
|
|
||||||
} else {
|
} else {
|
||||||
for i, cmd := range cmds {
|
writeCommandList(cmds, iw)
|
||||||
printCommandSummary(iw, cmd)
|
|
||||||
if i != len(cmds)-1 {
|
|
||||||
iw.Print("")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
rows = append(rows, w.commandTree(cmd, "")...)
|
||||||
|
if i != len(node.Children)-1 {
|
||||||
|
rows = append(rows, [2]string{"", ""})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
writeTwoColumns(iw, defaultColumnPadding, rows)
|
||||||
|
}
|
||||||
|
|
||||||
|
type helpCommandGroup struct {
|
||||||
|
Name string
|
||||||
|
Commands []*Node
|
||||||
|
}
|
||||||
|
|
||||||
|
func collectCommandGroups(nodes []*Node) []helpCommandGroup { // nolint: deadcode
|
||||||
|
groups := map[string][]*Node{}
|
||||||
|
for _, node := range nodes {
|
||||||
|
groups[node.Group] = append(groups[node.Group], node)
|
||||||
|
}
|
||||||
|
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) {
|
||||||
w.Print(cmd.Summary())
|
w.Print(cmd.Summary())
|
||||||
if cmd.Help != "" {
|
if cmd.Help != "" {
|
||||||
@@ -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
@@ -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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user