Skip to content
Myr

Merge pull request #138 from eparis/cleanup-helpfunc

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

Allow SetHelpFunc to overwrite the --help flag instead of just the help subcommand

authored by Eric Paris · committed by Eric Paris · parents 1ba88c4, c2d19e9 · Browse files · 1 file+25 added−26 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+25 added−26 removed

  • other:+25 added−26 removed

1 file+25 added−26 removed

modified:command.go+25 added-26 removed
186 unmodified lines
187 return err188 }189 }190}191192// HelpFunc returns either the function set by SetHelpFunc for this command193// or a parent, or it returns a function which calls c.Help()194func (c *Command) HelpFunc() func(*Command, []string) {195 cmd := c196 for cmd != nil {192 if c.helpFunc != nil {193 return c.helpFunc197 if cmd.helpFunc != nil {198 return cmd.helpFunc199 }200 cmd = cmd.parent201 }195196 if c.HasParent() {197 return c.parent.HelpFunc()198 } else {199 return func(c *Command, args []string) {200 if len(args) == 0 {201 // Help called without any topic, calling on root202 c.Root().Help()203 return204 }205206 cmd, _, e := c.Root().Find(args)207 if cmd == nil || e != nil {208 c.Printf("Unknown help topic %#q.", args)209210 c.Root().Usage()211 } else {212 err := cmd.Help()213 if err != nil {214 c.Println(err)202 return func(*Command, []string) {203 err := c.Help()204 if err != nil {205 c.Println(err)215 }216 }206 }207 }208}209
371 unmodified lines
581582 err = cmd.execute(flags)583 if err != nil {584 if err == flag.ErrHelp {596 cmd.Help()585 cmd.HelpFunc()(cmd, args)586 return nil587 }588 c.Println(cmd.UsageString())589 c.Println("Error:", err.Error())
18 unmodified lines
608 Use: "help [command]",609 Short: "Help about any command",610 Long: `Help provides help for any command in the application.611 Simply type ` + c.Name() + ` help [path to command] for full details.`,623 Run: c.HelpFunc(),612 PersistentPreRun: func(cmd *Command, args []string) {},613 PersistentPostRun: func(cmd *Command, args []string) {},614615 Run: func(c *Command, args []string) {616 cmd, _, e := c.Root().Find(args)617 if cmd == nil || e != nil {618 c.Printf("Unknown help topic %#q.", args)619 c.Root().Usage()620 } else {621 helpFunc := cmd.HelpFunc()622 helpFunc(cmd, args)623 }624 },625 }626 }627 c.AddCommand(c.helpCommand)628}
450 unmodified lines