diff --git a/app.go b/app.go index cd29005..891416d 100644 --- a/app.go +++ b/app.go @@ -5,9 +5,6 @@ import ( "io" "io/ioutil" "os" - "strings" - "text/tabwriter" - "text/template" "time" ) @@ -83,26 +80,6 @@ func (a *App) Run(arguments []string) (err error) { a.Authors = append(a.Authors, Author{Name: a.Author, Email: a.Email}) } - if HelpPrinter == nil { - defer func() { - HelpPrinter = nil - }() - - HelpPrinter = func(templ string, data interface{}) { - funcMap := template.FuncMap{ - "join": strings.Join, - } - - w := tabwriter.NewWriter(a.Writer, 0, 8, 1, '\t', 0) - t := template.Must(template.New("help").Funcs(funcMap).Parse(templ)) - err := t.Execute(w, data) - if err != nil { - panic(err) - } - w.Flush() - } - } - // append help to commands if a.Command(helpCommand.Name) == nil && !a.HideHelp { a.Commands = append(a.Commands, helpCommand) diff --git a/app_test.go b/app_test.go index 4a40b89..3bede53 100644 --- a/app_test.go +++ b/app_test.go @@ -3,6 +3,7 @@ package cli_test import ( "flag" "fmt" + "io" "os" "testing" @@ -537,7 +538,7 @@ func TestAppHelpPrinter(t *testing.T) { }() var wasCalled = false - cli.HelpPrinter = func(template string, data interface{}) { + cli.HelpPrinter = func(w io.Writer, template string, data interface{}) { wasCalled = true } diff --git a/help.go b/help.go index 7c4f81b..a45e642 100644 --- a/help.go +++ b/help.go @@ -1,6 +1,12 @@ package cli -import "fmt" +import ( + "fmt" + "io" + "strings" + "text/tabwriter" + "text/template" +) // The text template for the Default help topic. // cli.go uses text/template to render templates. You can @@ -87,16 +93,16 @@ var helpSubcommand = Command{ }, } -// Prints help for the App -type helpPrinter func(templ string, data interface{}) +// Prints help for the App or Command +type helpPrinter func(w io.Writer, templ string, data interface{}) -var HelpPrinter helpPrinter = nil +var HelpPrinter helpPrinter = printHelp // Prints version for the App var VersionPrinter = printVersion func ShowAppHelp(c *Context) { - HelpPrinter(AppHelpTemplate, c.App) + HelpPrinter(c.App.Writer, AppHelpTemplate, c.App) } // Prints the list of subcommands as the default app completion method @@ -109,24 +115,24 @@ func DefaultAppComplete(c *Context) { } // Prints help for the given command -func ShowCommandHelp(c *Context, command string) { +func ShowCommandHelp(ctx *Context, command string) { // show the subcommand help for a command with subcommands if command == "" { - HelpPrinter(SubcommandHelpTemplate, c.App) + HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App) return } - for _, c := range c.App.Commands { + for _, c := range ctx.App.Commands { if c.HasName(command) { - HelpPrinter(CommandHelpTemplate, c) + HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c) return } } - if c.App.CommandNotFound != nil { - c.App.CommandNotFound(c, command) + if ctx.App.CommandNotFound != nil { + ctx.App.CommandNotFound(ctx, command) } else { - fmt.Fprintf(c.App.Writer, "No help topic for '%v'\n", command) + fmt.Fprintf(ctx.App.Writer, "No help topic for '%v'\n", command) } } @@ -160,6 +166,20 @@ func ShowCommandCompletions(ctx *Context, command string) { } } +func printHelp(out io.Writer, templ string, data interface{}) { + funcMap := template.FuncMap{ + "join": strings.Join, + } + + w := tabwriter.NewWriter(out, 0, 8, 1, '\t', 0) + t := template.Must(template.New("help").Funcs(funcMap).Parse(templ)) + err := t.Execute(w, data) + if err != nil { + panic(err) + } + w.Flush() +} + func checkVersion(c *Context) bool { if c.GlobalBool("version") { ShowVersion(c)