Skip to content

Commit dbb7c2d

Browse files
authored
Merge pull request #308 from fabianofranz/restore_backwards_compatibility
Restore Help() and Usage() for backwards compatibility
2 parents 9d9ce12 + ded646f commit dbb7c2d

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

command.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,13 @@ func (c *Command) UsageFunc() (f func(*Command) error) {
214214
}
215215
}
216216

217+
// Output the usage for the command
218+
// Used when a user provides invalid input
219+
// Can be defined by user by overriding UsageFunc
220+
func (c *Command) Usage() error {
221+
return c.UsageFunc()(c)
222+
}
223+
217224
// HelpFunc returns either the function set by SetHelpFunc for this command
218225
// or a parent, or it returns a function with default help behavior
219226
func (c *Command) HelpFunc() func(*Command, []string) {
@@ -233,11 +240,19 @@ func (c *Command) HelpFunc() func(*Command, []string) {
233240
}
234241
}
235242

243+
// Output the help for the command
244+
// Used when a user calls help [command]
245+
// Can be defined by user by overriding HelpFunc
246+
func (c *Command) Help() error {
247+
c.HelpFunc()(c, []string{})
248+
return nil
249+
}
250+
236251
func (c *Command) UsageString() string {
237252
tmpOutput := c.output
238253
bb := new(bytes.Buffer)
239254
c.SetOutput(bb)
240-
c.UsageFunc()(c)
255+
c.Usage()
241256
c.output = tmpOutput
242257
return bb.String()
243258
}
@@ -720,9 +735,9 @@ func (c *Command) initHelpCmd() {
720735
cmd, _, e := c.Root().Find(args)
721736
if cmd == nil || e != nil {
722737
c.Printf("Unknown help topic %#q.", args)
723-
c.Root().UsageFunc()(cmd)
738+
c.Root().Usage()
724739
} else {
725-
cmd.HelpFunc()(cmd, args)
740+
cmd.Help()
726741
}
727742
},
728743
}

0 commit comments

Comments
 (0)