From 27bf24802c870327dc7d1b74d9e66df2801dcb6c Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Tue, 1 Sep 2015 13:58:55 -0400 Subject: [PATCH 1/2] Redo HelpFunc() to work things other than the help subcommand Today the HelpFunc() seemed to be tailor built for the `help` subcommand. Which has a rather weird purpose as its `Run` needs to find the actual command we want to get help about. Instead make the HelpFunc() for a command be about that command, rather than having it search for some other command... --- command.go | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/command.go b/command.go index 5274082d..5bb30582 100644 --- a/command.go +++ b/command.go @@ -188,32 +188,21 @@ func (c *Command) UsageFunc() (f func(*Command) error) { } } } + +// HelpFunc returns either the function set by SetHelpFunc for this command +// or a parent, or it returns a function which calls c.Help() func (c *Command) HelpFunc() func(*Command, []string) { - if c.helpFunc != nil { - return c.helpFunc + cmd := c + for cmd != nil { + if cmd.helpFunc != nil { + return cmd.helpFunc + } + cmd = cmd.parent } - - if c.HasParent() { - return c.parent.HelpFunc() - } else { - return func(c *Command, args []string) { - if len(args) == 0 { - // Help called without any topic, calling on root - c.Root().Help() - return - } - - cmd, _, e := c.Root().Find(args) - if cmd == nil || e != nil { - c.Printf("Unknown help topic %#q.", args) - - c.Root().Usage() - } else { - err := cmd.Help() - if err != nil { - c.Println(err) - } - } + return func(*Command, []string) { + err := c.Help() + if err != nil { + c.Println(err) } } } @@ -620,9 +609,19 @@ func (c *Command) initHelpCmd() { Short: "Help about any command", Long: `Help provides help for any command in the application. Simply type ` + c.Name() + ` help [path to command] for full details.`, - Run: c.HelpFunc(), PersistentPreRun: func(cmd *Command, args []string) {}, PersistentPostRun: func(cmd *Command, args []string) {}, + + Run: func(c *Command, args []string) { + cmd, _, e := c.Root().Find(args) + if cmd == nil || e != nil { + c.Printf("Unknown help topic %#q.", args) + c.Root().Usage() + } else { + helpFunc := cmd.HelpFunc() + helpFunc(cmd, args) + } + }, } } c.AddCommand(c.helpCommand) From c2d19e9c4338e36a2f2176537a4f67b3ee59f4e7 Mon Sep 17 00:00:00 2001 From: Eric Paris Date: Tue, 1 Sep 2015 14:10:55 -0400 Subject: [PATCH 2/2] Use the HelpFunc for the --help flag We were just calling Help() when a user set the --help flag. You could overwrite how the help subcommand worked with SetHelpFunc, but not now the --help flag worked. --- command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/command.go b/command.go index 5bb30582..4ff14b2d 100644 --- a/command.go +++ b/command.go @@ -582,7 +582,7 @@ func (c *Command) Execute() (err error) { err = cmd.execute(flags) if err != nil { if err == flag.ErrHelp { - cmd.Help() + cmd.HelpFunc()(cmd, args) return nil } c.Println(cmd.UsageString())