mirror of
https://github.com/spf13/cobra
synced 2024-11-24 14:47:12 +00:00
Merge pull request #138 from eparis/cleanup-helpfunc
Allow SetHelpFunc to overwrite the --help flag instead of just the help subcommand
This commit is contained in:
commit
16b4170663
1 changed files with 25 additions and 26 deletions
51
command.go
51
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) {
|
func (c *Command) HelpFunc() func(*Command, []string) {
|
||||||
if c.helpFunc != nil {
|
cmd := c
|
||||||
return c.helpFunc
|
for cmd != nil {
|
||||||
|
if cmd.helpFunc != nil {
|
||||||
|
return cmd.helpFunc
|
||||||
|
}
|
||||||
|
cmd = cmd.parent
|
||||||
}
|
}
|
||||||
|
return func(*Command, []string) {
|
||||||
if c.HasParent() {
|
err := c.Help()
|
||||||
return c.parent.HelpFunc()
|
if err != nil {
|
||||||
} else {
|
c.Println(err)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -593,7 +582,7 @@ func (c *Command) Execute() (err error) {
|
||||||
err = cmd.execute(flags)
|
err = cmd.execute(flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if err == flag.ErrHelp {
|
if err == flag.ErrHelp {
|
||||||
cmd.Help()
|
cmd.HelpFunc()(cmd, args)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
c.Println(cmd.UsageString())
|
c.Println(cmd.UsageString())
|
||||||
|
@ -620,9 +609,19 @@ func (c *Command) initHelpCmd() {
|
||||||
Short: "Help about any command",
|
Short: "Help about any command",
|
||||||
Long: `Help provides help for any command in the application.
|
Long: `Help provides help for any command in the application.
|
||||||
Simply type ` + c.Name() + ` help [path to command] for full details.`,
|
Simply type ` + c.Name() + ` help [path to command] for full details.`,
|
||||||
Run: c.HelpFunc(),
|
|
||||||
PersistentPreRun: func(cmd *Command, args []string) {},
|
PersistentPreRun: func(cmd *Command, args []string) {},
|
||||||
PersistentPostRun: 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)
|
c.AddCommand(c.helpCommand)
|
||||||
|
|
Loading…
Reference in a new issue