Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Fabiano Franz · committed by Fabiano Franz · parent 20217d8 · Browse files · 1 file+8 added−25 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+8 added−25 removed

  • other:+8 added−25 removed

1 file+8 added−25 removed

modified:command.go+8 added-25 removed
203 unmodified lines
204 if c.HasParent() {205 return c.parent.UsageFunc()206 }207 return func(c *Command) error {208 err := c.Usage()208 c.mergePersistentFlags()209 err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)210 if err != nil {211 c.Println(err)212 }213 return err214 }215}216217// HelpFunc returns either the function set by SetHelpFunc for this command217// or a parent, or it returns a function which calls c.Help()218// or a parent, or it returns a function with default help behavior219func (c *Command) HelpFunc() func(*Command, []string) {220 cmd := c221 for cmd != nil {222 if cmd.helpFunc != nil {
1 unmodified line
224 }225 cmd = cmd.parent226 }227 return func(*Command, []string) {227 err := c.Help()228 c.mergePersistentFlags()229 err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)230 if err != nil {231 c.Println(err)232 }233 }234}235234// Output the usage for the command235// Used when a user provides invalid input236// Can be defined by user by overriding UsageFunc237func (c *Command) Usage() error {238 c.mergePersistentFlags()239 err := tmpl(c.OutOrStderr(), c.UsageTemplate(), c)240 return err241}242243// Output the help for the command244// Used when a user calls help [command]245// by the default HelpFunc in the commander246func (c *Command) Help() error {247 c.mergePersistentFlags()248 err := tmpl(c.OutOrStdout(), c.HelpTemplate(), c)249 return err250}251236func (c *Command) UsageString() string {237 tmpOutput := c.output238 bb := new(bytes.Buffer)239 c.SetOutput(bb)256 c.Usage()240 c.UsageFunc()(c)241 c.output = tmpOutput242 return bb.String()243}244
474 unmodified lines
719 Run: func(c *Command, args []string) {720 cmd, _, e := c.Root().Find(args)721 if cmd == nil || e != nil {722 c.Printf("Unknown help topic %#q.", args)739 c.Root().Usage()723 c.Root().UsageFunc()(cmd)724 } else {741 helpFunc := cmd.HelpFunc()742 helpFunc(cmd, args)725 cmd.HelpFunc()(cmd, args)726 }727 },728 }729 }
512 unmodified lines