Make initHelpCmd public (#436)

Useful for solving #424
This commit is contained in:
Albert Nigmatzianov 2017-05-20 19:23:33 +02:00 committed by GitHub
parent ca5710c94e
commit 52ae6a1d02

View File

@ -700,7 +700,7 @@ func (c *Command) ExecuteC() (cmd *Command, err error) {
// initialize help as the last point possible to allow for user // initialize help as the last point possible to allow for user
// overriding // overriding
c.initHelpCmd() c.InitDefaultHelpCmd()
var args []string var args []string
@ -764,31 +764,32 @@ func (c *Command) InitDefaultHelpFlag() {
} }
} }
func (c *Command) initHelpCmd() { // InitDefaultHelpCmd adds default help command to c.
if c.helpCommand == nil { // It is called automatically by executing the c or by calling help and usage.
if !c.HasSubCommands() { // If c already has help command or c has no subcommands, it will do nothing.
return func (c *Command) InitDefaultHelpCmd() {
} if c.helpCommand != nil || !c.HasSubCommands() {
return
}
c.helpCommand = &Command{ c.helpCommand = &Command{
Use: "help [command]", Use: "help [command]",
Short: "Help about any command", Short: "Help about any command",
Long: `Help provides help for any command in the application. Long: `Help provides help for any command in the application.
Simply type ` + c.Name() + ` help [path to command] for full details.`, Simply type ` + c.Name() + ` help [path to command] for full details.`,
PersistentPreRun: func(cmd *Command, args []string) {}, PersistentPreRun: func(cmd *Command, args []string) {},
PersistentPostRun: func(cmd *Command, args []string) {}, PersistentPostRun: func(cmd *Command, args []string) {},
Run: func(c *Command, args []string) { Run: func(c *Command, args []string) {
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\n", args) c.Printf("Unknown help topic %#q\n", args)
c.Root().Usage() c.Root().Usage()
} else { } else {
cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown cmd.InitDefaultHelpFlag() // make possible 'help' flag to be shown
cmd.Help() cmd.Help()
} }
}, },
}
} }
c.RemoveCommand(c.helpCommand) c.RemoveCommand(c.helpCommand)
c.AddCommand(c.helpCommand) c.AddCommand(c.helpCommand)