Merge pull request #154 from codegangsta/allow-writer-to-be-set

Allow writer to be set
This commit is contained in:
2014-12-12 08:50:46 -05:00
4 changed files with 89 additions and 34 deletions

29
help.go
View File

@@ -1,11 +1,6 @@
package cli
import (
"fmt"
"os"
"text/tabwriter"
"text/template"
)
import "fmt"
// The text template for the Default help topic.
// cli.go uses text/template to render templates. You can
@@ -94,7 +89,9 @@ var helpSubcommand = Command{
}
// Prints help for the App
var HelpPrinter = printHelp
type helpPrinter func(templ string, data interface{})
var HelpPrinter helpPrinter = nil
// Prints version for the App
var VersionPrinter = printVersion
@@ -106,9 +103,9 @@ func ShowAppHelp(c *Context) {
// Prints the list of subcommands as the default app completion method
func DefaultAppComplete(c *Context) {
for _, command := range c.App.Commands {
fmt.Println(command.Name)
fmt.Fprintln(c.App.Writer, command.Name)
if command.ShortName != "" {
fmt.Println(command.ShortName)
fmt.Fprintln(c.App.Writer, command.ShortName)
}
}
}
@@ -125,7 +122,7 @@ func ShowCommandHelp(c *Context, command string) {
if c.App.CommandNotFound != nil {
c.App.CommandNotFound(c, command)
} else {
fmt.Printf("No help topic for '%v'\n", command)
fmt.Fprintf(c.App.Writer, "No help topic for '%v'\n", command)
}
}
@@ -140,7 +137,7 @@ func ShowVersion(c *Context) {
}
func printVersion(c *Context) {
fmt.Printf("%v version %v\n", c.App.Name, c.App.Version)
fmt.Fprintf(c.App.Writer, "%v version %v\n", c.App.Name, c.App.Version)
}
// Prints the lists of commands within a given context
@@ -159,16 +156,6 @@ func ShowCommandCompletions(ctx *Context, command string) {
}
}
func printHelp(templ string, data interface{}) {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 1, '\t', 0)
t := template.Must(template.New("help").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)