From ca3a30c5f18b04d9818bf8b87accf460ec946de2 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Sun, 2 Feb 2020 20:21:55 +1100 Subject: [PATCH] Make HelpOptions into an Option. --- help.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/help.go b/help.go index 3b94a8f..0f4dfe0 100644 --- a/help.go +++ b/help.go @@ -47,6 +47,12 @@ type HelpOptions struct { 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. type HelpProvider interface { // 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. -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 switch node.Type { default: @@ -353,16 +359,16 @@ func (w *HelpOptions) commandTree(node *Node, prefix string) (rows [][2]string) nodeName += prefix + "<" + node.Name + ">" } rows = append(rows, [2]string{nodeName, node.Help}) - if w.Indenter == nil { + if h.Indenter == nil { prefix = SpaceIndenter(prefix) } else { - prefix = w.Indenter(prefix) + prefix = h.Indenter(prefix) } for _, arg := range node.Positional { rows = append(rows, [2]string{prefix + arg.Summary(), arg.Help}) } for _, subCmd := range node.Children { - rows = append(rows, w.commandTree(subCmd, prefix)...) + rows = append(rows, h.commandTree(subCmd, prefix)...) } return }