Don't expose Usage() and Help() - our interfaces are UsageFunc, HelpFunc and UsageString

This commit is contained in:
Fabiano Franz 2016-07-15 17:17:29 -03:00
parent 20217d8f5e
commit d6bf4ef243

View file

@ -205,7 +205,8 @@ func (c *Command) UsageFunc() (f func(*Command) error) {
return c.parent.UsageFunc() return c.parent.UsageFunc()
} }
return func(c *Command) error { return func(c *Command) error {
err := c.Usage() c.mergePersistentFlags()
err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)
if err != nil { if err != nil {
c.Println(err) c.Println(err)
} }
@ -214,7 +215,7 @@ func (c *Command) UsageFunc() (f func(*Command) error) {
} }
// HelpFunc returns either the function set by SetHelpFunc for this command // HelpFunc returns either the function set by SetHelpFunc for this command
// or a parent, or it returns a function which calls c.Help() // or a parent, or it returns a function with default help behavior
func (c *Command) HelpFunc() func(*Command, []string) { func (c *Command) HelpFunc() func(*Command, []string) {
cmd := c cmd := c
for cmd != nil { for cmd != nil {
@ -224,36 +225,19 @@ func (c *Command) HelpFunc() func(*Command, []string) {
cmd = cmd.parent cmd = cmd.parent
} }
return func(*Command, []string) { return func(*Command, []string) {
err := c.Help() c.mergePersistentFlags()
err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
if err != nil { if err != nil {
c.Println(err) c.Println(err)
} }
} }
} }
// Output the usage for the command
// Used when a user provides invalid input
// Can be defined by user by overriding UsageFunc
func (c *Command) Usage() error {
c.mergePersistentFlags()
err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)
return err
}
// Output the help for the command
// Used when a user calls help [command]
// by the default HelpFunc in the commander
func (c *Command) Help() error {
c.mergePersistentFlags()
err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)
return err
}
func (c *Command) UsageString() string { func (c *Command) UsageString() string {
tmpOutput := c.output tmpOutput := c.output
bb := new(bytes.Buffer) bb := new(bytes.Buffer)
c.SetOutput(bb) c.SetOutput(bb)
c.Usage() c.UsageFunc()(c)
c.output = tmpOutput c.output = tmpOutput
return bb.String() return bb.String()
} }
@ -736,10 +720,9 @@ func (c *Command) initHelpCmd() {
cmd, _, e := c.Root().Find(args) cmd, _, e := c.Root().Find(args)
if cmd == nil || e != nil { if cmd == nil || e != nil {
c.Printf("Unknown help topic %#q.", args) c.Printf("Unknown help topic %#q.", args)
c.Root().Usage() c.Root().UsageFunc()(cmd)
} else { } else {
helpFunc := cmd.HelpFunc() cmd.HelpFunc()(cmd, args)
helpFunc(cmd, args)
} }
}, },
} }