@ -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 ( c tx * Context , command string ) {
// show the subcommand help for a command with subcommands
if command == "" {
HelpPrinter ( SubcommandHelpTemplate, c . App )
HelpPrinter ( ctx. App . Writer , SubcommandHelpTemplate, c tx . App )
return
}
for _ , c := range c . App . Commands {
for _ , c := range c tx . 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 c tx . App . CommandNotFound != nil {
c tx . App . CommandNotFound ( c tx , command )
} else {
fmt . Fprintf ( c . App . Writer , "No help topic for '%v'\n" , command )
fmt . Fprintf ( c tx . 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 )