Made bash completion command optional; still need to update documentation

main
Summer Mousa 11 years ago
parent 0b29bee364
commit 3a10545f91

@ -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"})

@ -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)

@ -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.

@ -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
}

Loading…
Cancel
Save