Merge pull request #115 from AudriusButkevicius/hidehelp

Make help command optional
main
Jeremy Saenz 10 years ago
commit c7d940a369

@ -22,6 +22,8 @@ type App struct {
Flags []Flag Flags []Flag
// Boolean to enable bash completion commands // Boolean to enable bash completion commands
EnableBashCompletion bool EnableBashCompletion bool
// Boolean to hide built-in help command
HideHelp bool
// An action to execute when the bash-completion flag is set // An action to execute when the bash-completion flag is set
BashComplete func(context *Context) BashComplete func(context *Context)
// An action to execute before any subcommands are run, but after the context is ready // An action to execute before any subcommands are run, but after the context is ready
@ -66,8 +68,9 @@ func NewApp() *App {
// Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination // Entry point to the cli app. Parses the arguments slice and routes to the proper flag/args combination
func (a *App) Run(arguments []string) error { func (a *App) Run(arguments []string) error {
// append help to commands // append help to commands
if a.Command(helpCommand.Name) == nil { if a.Command(helpCommand.Name) == nil && !a.HideHelp {
a.Commands = append(a.Commands, helpCommand) a.Commands = append(a.Commands, helpCommand)
a.appendFlag(HelpFlag)
} }
//append version/help flags //append version/help flags
@ -75,7 +78,6 @@ func (a *App) Run(arguments []string) error {
a.appendFlag(BashCompletionFlag) a.appendFlag(BashCompletionFlag)
} }
a.appendFlag(VersionFlag) a.appendFlag(VersionFlag)
a.appendFlag(HelpFlag)
// parse flags // parse flags
set := flagSet(a.Name, a.Flags) set := flagSet(a.Name, a.Flags)
@ -135,8 +137,9 @@ func (a *App) Run(arguments []string) error {
func (a *App) RunAsSubcommand(ctx *Context) error { func (a *App) RunAsSubcommand(ctx *Context) error {
// append help to commands // append help to commands
if len(a.Commands) > 0 { if len(a.Commands) > 0 {
if a.Command(helpCommand.Name) == nil { if a.Command(helpCommand.Name) == nil && !a.HideHelp {
a.Commands = append(a.Commands, helpCommand) a.Commands = append(a.Commands, helpCommand)
a.appendFlag(HelpFlag)
} }
} }
@ -144,7 +147,6 @@ func (a *App) RunAsSubcommand(ctx *Context) error {
if a.EnableBashCompletion { if a.EnableBashCompletion {
a.appendFlag(BashCompletionFlag) a.appendFlag(BashCompletionFlag)
} }
a.appendFlag(HelpFlag)
// parse flags // parse flags
set := flagSet(a.Name, a.Flags) set := flagSet(a.Name, a.Flags)

@ -84,12 +84,10 @@ func ExampleAppHelp() {
// describeit - use it to see a description // describeit - use it to see a description
// //
// USAGE: // USAGE:
// command describeit [command options] [arguments...] // command describeit [arguments...]
// //
// DESCRIPTION: // DESCRIPTION:
// This is how we describe describeit the function // This is how we describe describeit the function
//
// OPTIONS:
} }
func ExampleAppBashComplete() { func ExampleAppBashComplete() {

@ -29,6 +29,8 @@ type Command struct {
Flags []Flag Flags []Flag
// Treat all flags as normal arguments if true // Treat all flags as normal arguments if true
SkipFlagParsing bool SkipFlagParsing bool
// Boolean to hide built-in help command
HideHelp bool
} }
// Invokes the command given the context, parses ctx.Args() to generate command-specific flags // Invokes the command given the context, parses ctx.Args() to generate command-specific flags
@ -38,11 +40,13 @@ func (c Command) Run(ctx *Context) error {
return c.startApp(ctx) return c.startApp(ctx)
} }
if !c.HideHelp {
// append help to flags // append help to flags
c.Flags = append( c.Flags = append(
c.Flags, c.Flags,
HelpFlag, HelpFlag,
) )
}
if ctx.App.EnableBashCompletion { if ctx.App.EnableBashCompletion {
c.Flags = append(c.Flags, BashCompletionFlag) c.Flags = append(c.Flags, BashCompletionFlag)
@ -117,6 +121,7 @@ func (c Command) startApp(ctx *Context) error {
// set the flags and commands // set the flags and commands
app.Commands = c.Subcommands app.Commands = c.Subcommands
app.Flags = c.Flags app.Flags = c.Flags
app.HideHelp = c.HideHelp
// bash completion // bash completion
app.EnableBashCompletion = ctx.App.EnableBashCompletion app.EnableBashCompletion = ctx.App.EnableBashCompletion

@ -14,17 +14,17 @@ var AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}} {{.Name}} - {{.Usage}}
USAGE: USAGE:
{{.Name}} [global options] command [command options] [arguments...] {{.Name}} {{ if .Flags }}[global options] {{ end }}command{{ if .Flags }} [command options]{{ end }} [arguments...]
VERSION: VERSION:
{{.Version}} {{.Version}}
COMMANDS: COMMANDS:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}} {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
{{end}} {{end}}{{ if .Flags }}
GLOBAL OPTIONS: GLOBAL OPTIONS:
{{range .Flags}}{{.}} {{range .Flags}}{{.}}
{{end}} {{end}}{{ end }}
` `
// The text template for the command help topic. // The text template for the command help topic.
@ -34,14 +34,14 @@ var CommandHelpTemplate = `NAME:
{{.Name}} - {{.Usage}} {{.Name}} - {{.Usage}}
USAGE: USAGE:
command {{.Name}} [command options] [arguments...] command {{.Name}}{{ if .Flags }} [command options]{{ end }} [arguments...]
DESCRIPTION: DESCRIPTION:
{{.Description}} {{.Description}}{{ if .Flags }}
OPTIONS: OPTIONS:
{{range .Flags}}{{.}} {{range .Flags}}{{.}}
{{end}} {{end}}{{ end }}
` `
// The text template for the subcommand help topic. // The text template for the subcommand help topic.
@ -51,14 +51,14 @@ var SubcommandHelpTemplate = `NAME:
{{.Name}} - {{.Usage}} {{.Name}} - {{.Usage}}
USAGE: USAGE:
{{.Name}} [global options] command [command options] [arguments...] {{.Name}} command{{ if .Flags }} [command options]{{ end }} [arguments...]
COMMANDS: COMMANDS:
{{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}} {{range .Commands}}{{.Name}}{{with .ShortName}}, {{.}}{{end}}{{ "\t" }}{{.Usage}}
{{end}} {{end}}{{ if .Flags }}
OPTIONS: OPTIONS:
{{range .Flags}}{{.}} {{range .Flags}}{{.}}
{{end}} {{end}}{{ end }}
` `
var helpCommand = Command{ var helpCommand = Command{

Loading…
Cancel
Save