Skip to content
Myr

Redo HelpFunc() to work things other than the help subcommand

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

Today the HelpFunc() seemed to be tailor built for the `help` subcommand. Which has a rather weird purpose as its `Run` needs to find the actual command we want to get help about. Instead make the HelpFunc() for a command be about that command, rather than having it search for some other command...

authored by Eric Paris · committed by Eric Paris · parent 1f92281 · Browse files · 1 file+24 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+24 added−25 removed

  • other:+24 added−25 removed

1 file+24 added−25 removed

modified:command.go+24 added-25 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
398 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