Merge pull request #217 from codegangsta/readd-print-help-function

Readd printHelp function back
This commit is contained in:
Jesse Szwedko 2015-05-03 17:52:12 -07:00
commit 2e64a10881
3 changed files with 34 additions and 36 deletions

23
app.go
View File

@ -5,9 +5,6 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"strings"
"text/tabwriter"
"text/template"
"time" "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}) 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 // append help to commands
if a.Command(helpCommand.Name) == nil && !a.HideHelp { if a.Command(helpCommand.Name) == nil && !a.HideHelp {
a.Commands = append(a.Commands, helpCommand) a.Commands = append(a.Commands, helpCommand)

View File

@ -3,6 +3,7 @@ package cli_test
import ( import (
"flag" "flag"
"fmt" "fmt"
"io"
"os" "os"
"testing" "testing"
@ -537,7 +538,7 @@ func TestAppHelpPrinter(t *testing.T) {
}() }()
var wasCalled = false var wasCalled = false
cli.HelpPrinter = func(template string, data interface{}) { cli.HelpPrinter = func(w io.Writer, template string, data interface{}) {
wasCalled = true wasCalled = true
} }

44
help.go
View File

@ -1,6 +1,12 @@
package cli package cli
import "fmt" import (
"fmt"
"io"
"strings"
"text/tabwriter"
"text/template"
)
// The text template for the Default help topic. // The text template for the Default help topic.
// cli.go uses text/template to render templates. You can // cli.go uses text/template to render templates. You can
@ -87,16 +93,16 @@ var helpSubcommand = Command{
}, },
} }
// Prints help for the App // Prints help for the App or Command
type helpPrinter func(templ string, data interface{}) type helpPrinter func(w io.Writer, templ string, data interface{})
var HelpPrinter helpPrinter = nil var HelpPrinter helpPrinter = printHelp
// Prints version for the App // Prints version for the App
var VersionPrinter = printVersion var VersionPrinter = printVersion
func ShowAppHelp(c *Context) { 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 // 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 // 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 // show the subcommand help for a command with subcommands
if command == "" { if command == "" {
HelpPrinter(SubcommandHelpTemplate, c.App) HelpPrinter(ctx.App.Writer, SubcommandHelpTemplate, ctx.App)
return return
} }
for _, c := range c.App.Commands { for _, c := range ctx.App.Commands {
if c.HasName(command) { if c.HasName(command) {
HelpPrinter(CommandHelpTemplate, c) HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
return return
} }
} }
if c.App.CommandNotFound != nil { if ctx.App.CommandNotFound != nil {
c.App.CommandNotFound(c, command) ctx.App.CommandNotFound(ctx, command)
} else { } 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 { func checkVersion(c *Context) bool {
if c.GlobalBool("version") { if c.GlobalBool("version") {
ShowVersion(c) ShowVersion(c)