forked from mirror/cobra
Merge pull request #308 from fabianofranz/restore_backwards_compatibility
Restore Help() and Usage() for backwards compatibility
This commit is contained in:
commit
dbb7c2d02e
21
command.go
21
command.go
|
@ -214,6 +214,13 @@ func (c *Command) UsageFunc() (f func(*Command) error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
return c.UsageFunc()(c)
|
||||||
|
}
|
||||||
|
|
||||||
// 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 with default help behavior
|
// 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) {
|
||||||
|
@ -233,11 +240,19 @@ func (c *Command) HelpFunc() func(*Command, []string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Output the help for the command
|
||||||
|
// Used when a user calls help [command]
|
||||||
|
// Can be defined by user by overriding HelpFunc
|
||||||
|
func (c *Command) Help() error {
|
||||||
|
c.HelpFunc()(c, []string{})
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
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.UsageFunc()(c)
|
c.Usage()
|
||||||
c.output = tmpOutput
|
c.output = tmpOutput
|
||||||
return bb.String()
|
return bb.String()
|
||||||
}
|
}
|
||||||
|
@ -720,9 +735,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().UsageFunc()(cmd)
|
c.Root().Usage()
|
||||||
} else {
|
} else {
|
||||||
cmd.HelpFunc()(cmd, args)
|
cmd.Help()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue