Add support for custom help templates.
This commit is contained in:
parent
d70f47eeca
commit
f7d6a07f2d
4
app.go
4
app.go
@ -85,6 +85,10 @@ type App struct {
|
|||||||
ErrWriter io.Writer
|
ErrWriter io.Writer
|
||||||
// Other custom info
|
// Other custom info
|
||||||
Metadata map[string]interface{}
|
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
|
didSetup bool
|
||||||
}
|
}
|
||||||
|
@ -59,6 +59,11 @@ type Command struct {
|
|||||||
// Full name of command for help, defaults to full command name, including parent commands.
|
// Full name of command for help, defaults to full command name, including parent commands.
|
||||||
HelpName string
|
HelpName string
|
||||||
commandNamePath []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
|
type CommandsByName []Command
|
||||||
@ -250,6 +255,7 @@ func (c Command) startApp(ctx *Context) error {
|
|||||||
|
|
||||||
// set CommandNotFound
|
// set CommandNotFound
|
||||||
app.CommandNotFound = ctx.App.CommandNotFound
|
app.CommandNotFound = ctx.App.CommandNotFound
|
||||||
|
app.CustomAppHelpTemplate = c.CustomHelpTemplate
|
||||||
|
|
||||||
// set the flags and commands
|
// set the flags and commands
|
||||||
app.Commands = c.Subcommands
|
app.Commands = c.Subcommands
|
||||||
|
16
context.go
16
context.go
@ -186,9 +186,23 @@ func (a Args) First() string {
|
|||||||
return a.Get(0)
|
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)
|
// Tail returns the rest of the arguments (not the first one)
|
||||||
// or else an empty string slice
|
// or else an empty string slice
|
||||||
func (a Args) Tail() []string {
|
func (a Args) Tail() Args {
|
||||||
if len(a) >= 2 {
|
if len(a) >= 2 {
|
||||||
return []string(a)[1:]
|
return []string(a)[1:]
|
||||||
}
|
}
|
||||||
|
24
help.go
24
help.go
@ -120,9 +120,19 @@ var HelpPrinter helpPrinter = printHelp
|
|||||||
// VersionPrinter prints the version for the App
|
// VersionPrinter prints the version for the App
|
||||||
var VersionPrinter = printVersion
|
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.
|
// ShowAppHelp is an action that displays the help.
|
||||||
func ShowAppHelp(c *Context) error {
|
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
|
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
|
// ShowCommandHelp prints help for the given command
|
||||||
func ShowCommandHelp(ctx *Context, command string) error {
|
func ShowCommandHelp(ctx *Context, command string) error {
|
||||||
// show the subcommand help for a command with subcommands
|
// 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 {
|
for _, c := range ctx.App.Commands {
|
||||||
if c.HasName(command) {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user