Make HelpOptions into an Option.

This commit is contained in:
Alec Thomas
2020-02-02 20:21:55 +11:00
parent b4ae5fb86e
commit ca3a30c5f1
+10 -4
View File
@@ -47,6 +47,12 @@ type HelpOptions struct {
Indenter HelpIndenter Indenter HelpIndenter
} }
// Apply options to Kong as a configuration option.
func (h HelpOptions) Apply(k *Kong) error {
k.helpOptions = h
return nil
}
// HelpProvider can be implemented by commands/args to provide detailed help. // HelpProvider can be implemented by commands/args to provide detailed help.
type HelpProvider interface { type HelpProvider interface {
// This string is formatted by go/doc and thus has the same formatting rules. // This string is formatted by go/doc and thus has the same formatting rules.
@@ -344,7 +350,7 @@ func formatFlag(haveShort bool, flag *Flag) string {
} }
// commandTree creates a tree with the given node name as root and its children's arguments and sub commands as leaves. // commandTree creates a tree with the given node name as root and its children's arguments and sub commands as leaves.
func (w *HelpOptions) commandTree(node *Node, prefix string) (rows [][2]string) { func (h *HelpOptions) commandTree(node *Node, prefix string) (rows [][2]string) {
var nodeName string var nodeName string
switch node.Type { switch node.Type {
default: default:
@@ -353,16 +359,16 @@ func (w *HelpOptions) commandTree(node *Node, prefix string) (rows [][2]string)
nodeName += prefix + "<" + node.Name + ">" nodeName += prefix + "<" + node.Name + ">"
} }
rows = append(rows, [2]string{nodeName, node.Help}) rows = append(rows, [2]string{nodeName, node.Help})
if w.Indenter == nil { if h.Indenter == nil {
prefix = SpaceIndenter(prefix) prefix = SpaceIndenter(prefix)
} else { } else {
prefix = w.Indenter(prefix) prefix = h.Indenter(prefix)
} }
for _, arg := range node.Positional { for _, arg := range node.Positional {
rows = append(rows, [2]string{prefix + arg.Summary(), arg.Help}) rows = append(rows, [2]string{prefix + arg.Summary(), arg.Help})
} }
for _, subCmd := range node.Children { for _, subCmd := range node.Children {
rows = append(rows, w.commandTree(subCmd, prefix)...) rows = append(rows, h.commandTree(subCmd, prefix)...)
} }
return return
} }