From aeb6620dbeae0e376e716aa9d94b2a7985d10530 Mon Sep 17 00:00:00 2001 From: Alec Thomas Date: Thu, 23 Jan 2020 21:19:23 +1100 Subject: [PATCH] Don't select default command when displaying help. See #41. --- context.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index 86e6f5b..7114acf 100644 --- a/context.go +++ b/context.go @@ -397,8 +397,17 @@ func (c *Context) trace(node *Node) (err error) { // nolint: gocyclo return fmt.Errorf("unexpected token %s", token) } } + return c.maybeSelectDefault(flags, node) +} - // End of the line, check for a default command. +// End of the line, check for a default command, but only if we're not displaying help, +// otherwise we'd only ever display the help for the default command. +func (c *Context) maybeSelectDefault(flags []*Flag, node *Node) error { + for _, flag := range flags { + if flag.Name == "help" && flag.Set { + return nil + } + } var defaultNode *Path for _, child := range node.Children { if child.Type == CommandNode && child.Tag.Default != "" {