Made bash completion command optional; still need to update documentation

This commit is contained in:
Summer Mousa 2014-04-12 08:32:53 -05:00
parent 0b29bee364
commit 3a10545f91
4 changed files with 38 additions and 16 deletions

23
app.go
View File

@ -20,6 +20,10 @@ type App struct {
Commands []Command Commands []Command
// List of flags to parse // List of flags to parse
Flags []Flag 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 // 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 // If a non-nil error is returned, no subcommands are run
Before func(context *Context) error 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. // Creates a new cli Application with some reasonable defaults for Name, Usage, Version and Action.
func NewApp() *App { func NewApp() *App {
return &App{ return &App{
Name: os.Args[0], Name: os.Args[0],
Usage: "A new cli application", Usage: "A new cli application",
Version: "0.0.0", Version: "0.0.0",
Action: helpCommand.Action, BashComplete: DefaultAppComplete,
Compiled: compileTime(), Action: helpCommand.Action,
Author: "Author", Compiled: compileTime(),
Email: "unknown@email", Author: "Author",
Email: "unknown@email",
} }
} }
@ -64,7 +69,9 @@ func (a *App) Run(arguments []string) error {
} }
//append version/help flags //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{"version, v", "print the version"})
a.appendFlag(BoolFlag{"help, h", "show help"}) a.appendFlag(BoolFlag{"help, h", "show help"})

View File

@ -29,10 +29,13 @@ func (c Command) Run(ctx *Context) error {
// append help to flags // append help to flags
c.Flags = append( c.Flags = append(
c.Flags, c.Flags,
BoolFlag{"generate-bash-completion", ""},
BoolFlag{"help, h", "show help"}, BoolFlag{"help, h", "show help"},
) )
if ctx.App.EnableBashCompletion {
c.Flags = append(c.Flags, BashCompletionFlag)
}
set := flagSet(c.Name, c.Flags) set := flagSet(c.Name, c.Flags)
set.SetOutput(ioutil.Discard) set.SetOutput(ioutil.Discard)

View File

@ -7,6 +7,9 @@ import (
"strings" "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. // Flag is a common interface related to parsing flags in cli.
// For more advanced flag parsing techniques, it is recomended that // For more advanced flag parsing techniques, it is recomended that
// this interface be implemented. // this interface be implemented.

23
help.go
View File

@ -60,10 +60,21 @@ var helpCommand = Command{
// Prints help for the App // Prints help for the App
var HelpPrinter = printHelp var HelpPrinter = printHelp
func ShowAppHelp(c *Context) { func ShowAppHelp(c *Context) {
HelpPrinter(AppHelpTemplate, c.App) 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 // Prints help for the given command
func ShowCommandHelp(c *Context, command string) { func ShowCommandHelp(c *Context, command string) {
for _, c := range c.App.Commands { for _, c := range c.App.Commands {
@ -83,11 +94,9 @@ func ShowVersion(c *Context) {
// Prints the lists of commands within a given context // Prints the lists of commands within a given context
func ShowCompletions(c *Context) { func ShowCompletions(c *Context) {
for _, command := range c.App.Commands { a := c.App
fmt.Println(command.Name) if a != nil && a.BashComplete != nil {
if command.ShortName != "" { a.BashComplete(c)
fmt.Println(command.ShortName)
}
} }
} }
@ -137,7 +146,7 @@ func checkCommandHelp(c *Context, name string) bool {
} }
func checkCompletions(c *Context) bool { func checkCompletions(c *Context) bool {
if c.GlobalBool("generate-bash-completion") { if c.GlobalBool(BashCompletionFlag.Name) && c.App.EnableBashCompletion {
ShowCompletions(c) ShowCompletions(c)
return true return true
} }
@ -146,7 +155,7 @@ func checkCompletions(c *Context) bool {
} }
func checkCommandCompletions(c *Context, name string) 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) ShowCommandCompletions(c, name)
return true return true
} }