Add support for custom help templates.

main
Harshavardhana 8 years ago
parent d70f47eeca
commit f7d6a07f2d

@ -85,6 +85,10 @@ type App struct {
ErrWriter io.Writer
// Other custom info
Metadata map[string]interface{}
// CustomAppHelpTemplate the text template for app help topic.
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
CustomAppHelpTemplate string
didSetup bool
}

@ -59,6 +59,11 @@ type Command struct {
// Full name of command for help, defaults to full command name, including parent commands.
HelpName string
commandNamePath []string
// CustomHelpTemplate the text template for the command help topic.
// cli.go uses text/template to render templates. You can
// render custom help text by setting this variable.
CustomHelpTemplate string
}
type CommandsByName []Command
@ -250,6 +255,7 @@ func (c Command) startApp(ctx *Context) error {
// set CommandNotFound
app.CommandNotFound = ctx.App.CommandNotFound
app.CustomAppHelpTemplate = c.CustomHelpTemplate
// set the flags and commands
app.Commands = c.Subcommands

@ -186,9 +186,23 @@ func (a Args) First() string {
return a.Get(0)
}
// Last - Return the last argument, or else a blank string
func (a Args) Last() string {
return a.Get(len(a) - 1)
}
// Head - Return the rest of the arguments (not the last one)
// or else an empty string slice
func (a Args) Head() Args {
if len(a) == 1 {
return a
}
return []string(a)[:len(a)-1]
}
// Tail returns the rest of the arguments (not the first one)
// or else an empty string slice
func (a Args) Tail() []string {
func (a Args) Tail() Args {
if len(a) >= 2 {
return []string(a)[1:]
}

@ -120,9 +120,19 @@ var HelpPrinter helpPrinter = printHelp
// VersionPrinter prints the version for the App
var VersionPrinter = printVersion
// ShowAppHelpAndExit - Prints the list of subcommands for the app and exits with exit code.
func ShowAppHelpAndExit(c *Context, exitCode int) {
ShowAppHelp(c)
os.Exit(exitCode)
}
// ShowAppHelp is an action that displays the help.
func ShowAppHelp(c *Context) error {
HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
if c.App.CustomAppHelpTemplate != "" {
HelpPrinter(c.App.Writer, c.App.CustomAppHelpTemplate, c.App)
} else {
HelpPrinter(c.App.Writer, AppHelpTemplate, c.App)
}
return nil
}
@ -138,6 +148,12 @@ func DefaultAppComplete(c *Context) {
}
}
// ShowCommandHelpAndExit - exits with code after showing help
func ShowCommandHelpAndExit(c *Context, command string, code int) {
ShowCommandHelp(c, command)
os.Exit(code)
}
// ShowCommandHelp prints help for the given command
func ShowCommandHelp(ctx *Context, command string) error {
// show the subcommand help for a command with subcommands
@ -148,7 +164,11 @@ func ShowCommandHelp(ctx *Context, command string) error {
for _, c := range ctx.App.Commands {
if c.HasName(command) {
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
if c.CustomHelpTemplate != "" {
HelpPrinter(ctx.App.Writer, c.CustomHelpTemplate, c)
} else {
HelpPrinter(ctx.App.Writer, CommandHelpTemplate, c)
}
return nil
}
}

Loading…
Cancel
Save