Skip to content

Commit

Permalink
Add support for custom help templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
harshavardhana committed May 8, 2017
1 parent d70f47e commit f7d6a07
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 6 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
}
Expand Down
24 changes: 22 additions & 2 deletions help.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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
}
}
Expand Down

0 comments on commit f7d6a07

Please sign in to comment.