From 3a10545f91107ff5454ddfbc64a9aa655c67962b Mon Sep 17 00:00:00 2001 From: Summer Mousa Date: Sat, 12 Apr 2014 08:32:53 -0500 Subject: [PATCH] Made bash completion command optional; still need to update documentation --- app.go | 23 +++++++++++++++-------- command.go | 5 ++++- flag.go | 3 +++ help.go | 23 ++++++++++++++++------- 4 files changed, 38 insertions(+), 16 deletions(-) diff --git a/app.go b/app.go index 298e7f1..a7c7122 100644 --- a/app.go +++ b/app.go @@ -20,6 +20,10 @@ type App struct { Commands []Command // List of flags to parse Flags []Flag + // Boolean to enable bash completion commands + EnableBashCompletion bool + // An action to execute when the bash-completion flag is set + BashComplete func(context *Context) // An action to execute before any subcommands are run, but after the context is ready // If a non-nil error is returned, no subcommands are run Before func(context *Context) error @@ -46,13 +50,14 @@ func compileTime() time.Time { // Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action. func NewApp() *App { return &App{ - Name: os.Args[0], - Usage: "A new cli application", - Version: "0.0.0", - Action: helpCommand.Action, - Compiled: compileTime(), - Author: "Author", - Email: "unknown@email", + Name: os.Args[0], + Usage: "A new cli application", + Version: "0.0.0", + BashComplete: DefaultAppComplete, + Action: helpCommand.Action, + Compiled: compileTime(), + Author: "Author", + Email: "unknown@email", } } @@ -64,7 +69,9 @@ func (a *App) Run(arguments []string) error { } //append version/help flags - a.appendFlag(BoolFlag{"generate-bash-completion", ""}) + if a.EnableBashCompletion { + a.appendFlag(BashCompletionFlag) + } a.appendFlag(BoolFlag{"version, v", "print the version"}) a.appendFlag(BoolFlag{"help, h", "show help"}) diff --git a/command.go b/command.go index 55d5221..60d29aa 100644 --- a/command.go +++ b/command.go @@ -29,10 +29,13 @@ func (c Command) Run(ctx *Context) error { // append help to flags c.Flags = append( c.Flags, - BoolFlag{"generate-bash-completion", ""}, BoolFlag{"help, h", "show help"}, ) + if ctx.App.EnableBashCompletion { + c.Flags = append(c.Flags, BashCompletionFlag) + } + set := flagSet(c.Name, c.Flags) set.SetOutput(ioutil.Discard) diff --git a/flag.go b/flag.go index d0af858..d11c80f 100644 --- a/flag.go +++ b/flag.go @@ -7,6 +7,9 @@ import ( "strings" ) +// This flag enables bash-completion for all commands and subcommands +var BashCompletionFlag = BoolFlag{"generate-bash-completion", ""} + // Flag is a common interface related to parsing flags in cli. // For more advanced flag parsing techniques, it is recomended that // this interface be implemented. diff --git a/help.go b/help.go index 6ac03be..64b4bab 100644 --- a/help.go +++ b/help.go @@ -60,10 +60,21 @@ var helpCommand = Command{ // Prints help for the App var HelpPrinter = printHelp + func ShowAppHelp(c *Context) { HelpPrinter(AppHelpTemplate, c.App) } +// Prints the list of subcommands as the default app completion method +func DefaultAppComplete(c *Context) { + for _, command := range c.App.Commands { + fmt.Println(command.Name) + if command.ShortName != "" { + fmt.Println(command.ShortName) + } + } +} + // Prints help for the given command func ShowCommandHelp(c *Context, command string) { for _, c := range c.App.Commands { @@ -83,11 +94,9 @@ func ShowVersion(c *Context) { // Prints the lists of commands within a given context func ShowCompletions(c *Context) { - for _, command := range c.App.Commands { - fmt.Println(command.Name) - if command.ShortName != "" { - fmt.Println(command.ShortName) - } + a := c.App + if a != nil && a.BashComplete != nil { + a.BashComplete(c) } } @@ -137,7 +146,7 @@ func checkCommandHelp(c *Context, name string) bool { } func checkCompletions(c *Context) bool { - if c.GlobalBool("generate-bash-completion") { + if c.GlobalBool(BashCompletionFlag.Name) && c.App.EnableBashCompletion { ShowCompletions(c) return true } @@ -146,7 +155,7 @@ func checkCompletions(c *Context) bool { } func checkCommandCompletions(c *Context, name string) bool { - if c.Bool("generate-bash-completion") { + if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion { ShowCommandCompletions(c, name) return true }