Refactoring names from bash to shell

The refactoring is required since completion is also supported for zsh
shell.

Refactoring details:
- flag generate-bash-completion -> generate-completion
- var  EnableBashCompletion     -> EnableShellCompletion
- var  BashComplete             -> ShellComplete
- var  BashCompletionFlag       -> GenerateCompletionFlag
- type BashCompleteFunc         -> ShellCompleteFunc
This commit is contained in:
Antoine Eiche 2016-07-22 10:19:29 +02:00
parent 34a8b004d2
commit 94bc26fd1c
7 changed files with 60 additions and 57 deletions

View File

@ -35,7 +35,7 @@ applications in an expressive way.
* [Subcommands](#subcommands) * [Subcommands](#subcommands)
* [Subcommands categories](#subcommands-categories) * [Subcommands categories](#subcommands-categories)
* [Exit code](#exit-code) * [Exit code](#exit-code)
* [Bash Completion](#bash-completion) * [Shell Completion](#shell-completion)
+ [Enabling](#enabling) + [Enabling](#enabling)
+ [Distribution](#distribution) + [Distribution](#distribution)
+ [Customization](#customization) + [Customization](#customization)
@ -740,15 +740,15 @@ func main() {
} }
``` ```
### Bash Completion ### Shell Completion
You can enable completion commands by setting the `EnableBashCompletion` You can enable completion commands by setting the `EnableShellCompletion`
flag on the `App` object. By default, this setting will only auto-complete to flag on the `App` object. By default, this setting will only auto-complete to
show an app's subcommands, but you can write your own completion methods for show an app's subcommands, but you can write your own completion methods for
the App or its subcommands. the App or its subcommands.
<!-- { <!-- {
"args": ["complete", "&#45;&#45;generate&#45;bash&#45;completion"], "args": ["complete", "&#45;&#45;generate&#45;completion"],
"output": "laundry" "output": "laundry"
} --> } -->
``` go ``` go
@ -765,7 +765,7 @@ func main() {
tasks := []string{"cook", "clean", "laundry", "eat", "sleep", "code"} tasks := []string{"cook", "clean", "laundry", "eat", "sleep", "code"}
app := &cli.App{ app := &cli.App{
EnableBashCompletion: true, EnableShellCompletion: true,
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
Name: "complete", Name: "complete",
@ -775,7 +775,7 @@ func main() {
fmt.Println("completed task: ", c.Args().First()) fmt.Println("completed task: ", c.Args().First())
return nil return nil
}, },
BashComplete: func(c *cli.Context) { ShellComplete: func(c *cli.Context) {
// This will complete if no args are passed // This will complete if no args are passed
if c.NArg() > 0 { if c.NArg() > 0 {
return return
@ -794,7 +794,7 @@ func main() {
#### Enabling #### Enabling
You can generate bash or zsh completion code by using the flag `--init-completion bash` or `--init-completion bash`. You can generate bash or zsh completion code by using the flag `--init-completion bash` or `--init-completion zsh`.
To setup for bash: To setup for bash:
@ -825,8 +825,8 @@ to the name of their program (as above).
#### Customization #### Customization
The default bash completion flag (`--generate-bash-completion`) is defined as The default shell completion flag (`--generate-completion`) is defined as
`cli.BashCompletionFlag`, and may be redefined if desired, e.g.: `cli.GenerateCompletionFlag`, and may be redefined if desired, e.g.:
<!-- { <!-- {
"args": ["&#45;&#45;compgen"], "args": ["&#45;&#45;compgen"],
@ -842,13 +842,13 @@ import (
) )
func main() { func main() {
cli.BashCompletionFlag = &cli.BoolFlag{ cli.GenerateCompletionFlag = &cli.BoolFlag{
Name: "compgen", Name: "compgen",
Hidden: true, Hidden: true,
} }
app := &cli.App{ app := &cli.App{
EnableBashCompletion: true, EnableShellCompletion: true,
Commands: []*cli.Command{ Commands: []*cli.Command{
{ {
Name: "wat", Name: "wat",
@ -1058,7 +1058,7 @@ func init() {
cli.SubcommandHelpTemplate += "\nor something\n" cli.SubcommandHelpTemplate += "\nor something\n"
cli.HelpFlag = &cli.BoolFlag{Name: "halp"} cli.HelpFlag = &cli.BoolFlag{Name: "halp"}
cli.BashCompletionFlag = &cli.BoolFlag{Name: "compgen", Hidden: true} cli.GenerateCompletionFlag = &cli.BoolFlag{Name: "compgen", Hidden: true}
cli.VersionFlag = &cli.BoolFlag{Name: "print-version", Aliases: []string{"V"}} cli.VersionFlag = &cli.BoolFlag{Name: "print-version", Aliases: []string{"V"}}
cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) { cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
@ -1138,7 +1138,7 @@ func main() {
HideHelp: false, HideHelp: false,
Hidden: false, Hidden: false,
HelpName: "doo!", HelpName: "doo!",
BashComplete: func(c *cli.Context) { ShellComplete: func(c *cli.Context) {
fmt.Fprintf(c.App.Writer, "--better\n") fmt.Fprintf(c.App.Writer, "--better\n")
}, },
Before: func(c *cli.Context) error { Before: func(c *cli.Context) error {
@ -1181,10 +1181,10 @@ func main() {
&cli.UintFlag{Name: "age"}, &cli.UintFlag{Name: "age"},
&cli.Uint64Flag{Name: "bigage"}, &cli.Uint64Flag{Name: "bigage"},
}, },
EnableBashCompletion: true, EnableShellCompletion: true,
HideHelp: false, HideHelp: false,
HideVersion: false, HideVersion: false,
BashComplete: func(c *cli.Context) { ShellComplete: func(c *cli.Context) {
fmt.Fprintf(c.App.Writer, "lipstick\nkiss\nme\nlipstick\nringo\n") fmt.Fprintf(c.App.Writer, "lipstick\nkiss\nme\nlipstick\nringo\n")
}, },
Before: func(c *cli.Context) error { Before: func(c *cli.Context) error {

29
app.go
View File

@ -29,16 +29,16 @@ 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 // Boolean to enable shell completion commands
EnableBashCompletion bool EnableShellCompletion bool
// Boolean to hide built-in help command // Boolean to hide built-in help command
HideHelp bool HideHelp bool
// Boolean to hide built-in version flag and the VERSION section of help // Boolean to hide built-in version flag and the VERSION section of help
HideVersion bool HideVersion bool
// Categories contains the categorized commands and is populated on app startup // Categories contains the categorized commands and is populated on app startup
Categories CommandCategories Categories CommandCategories
// An action to execute when the bash-completion flag is set // An action to execute when the shell completion flag is set
BashComplete BashCompleteFunc ShellComplete ShellCompleteFunc
// 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 BeforeFunc Before BeforeFunc
@ -103,8 +103,8 @@ func (a *App) Setup() {
a.Version = "0.0.0" a.Version = "0.0.0"
} }
if a.BashComplete == nil { if a.ShellComplete == nil {
a.BashComplete = DefaultAppComplete a.ShellComplete = DefaultAppComplete
} }
if a.Action == nil { if a.Action == nil {
@ -136,8 +136,8 @@ func (a *App) Setup() {
} }
} }
if a.EnableBashCompletion { if a.EnableShellCompletion {
a.appendFlag(BashCompletionFlag) a.appendFlag(GenerateCompletionFlag)
a.appendFlag(InitCompletionFlag) a.appendFlag(InitCompletionFlag)
} }
@ -177,9 +177,12 @@ func (a *App) Run(arguments []string) (err error) {
return nil return nil
} }
var done bool if done, cerr := checkInitCompletion(context); done {
if done, err = checkInitCompletion(context); done { if cerr != nil {
return nil err = cerr
} else {
return nil
}
} }
if err != nil { if err != nil {
@ -266,8 +269,8 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
a.Commands = newCmds a.Commands = newCmds
// append flags // append flags
if a.EnableBashCompletion { if a.EnableShellCompletion {
a.appendFlag(BashCompletionFlag) a.appendFlag(GenerateCompletionFlag)
} }
// parse flags // parse flags

View File

@ -14,7 +14,7 @@ import (
) )
type opCounts struct { type opCounts struct {
Total, BashComplete, OnUsageError, Before, CommandNotFound, Action, After, SubCommand int Total, ShellComplete, OnUsageError, Before, CommandNotFound, Action, After, SubCommand int
} }
func ExampleApp_Run() { func ExampleApp_Run() {
@ -112,13 +112,13 @@ func ExampleApp_Run_help() {
// This is how we describe describeit the function // This is how we describe describeit the function
} }
func ExampleApp_Run_bashComplete() { func ExampleApp_Run_shellComplete() {
// set args for examples sake // set args for examples sake
os.Args = []string{"greet", "--generate-bash-completion"} os.Args = []string{"greet", "--generate-completion"}
app := &App{ app := &App{
Name: "greet", Name: "greet",
EnableBashCompletion: true, EnableShellCompletion: true,
Commands: []*Command{ Commands: []*Command{
{ {
Name: "describeit", Name: "describeit",
@ -132,7 +132,7 @@ func ExampleApp_Run_bashComplete() {
}, { }, {
Name: "next", Name: "next",
Usage: "next example", Usage: "next example",
Description: "more stuff to see when generating bash completion", Description: "more stuff to see when generating shell completion",
Action: func(c *Context) error { Action: func(c *Context) error {
fmt.Printf("the next example") fmt.Printf("the next example")
return nil return nil
@ -735,10 +735,10 @@ func TestApp_OrderOfOperations(t *testing.T) {
resetCounts := func() { counts = &opCounts{} } resetCounts := func() { counts = &opCounts{} }
app := &App{ app := &App{
EnableBashCompletion: true, EnableShellCompletion: true,
BashComplete: func(c *Context) { ShellComplete: func(c *Context) {
counts.Total++ counts.Total++
counts.BashComplete = counts.Total counts.ShellComplete = counts.Total
}, },
OnUsageError: func(c *Context, err error, isSubcommand bool) error { OnUsageError: func(c *Context, err error, isSubcommand bool) error {
counts.Total++ counts.Total++
@ -801,8 +801,8 @@ func TestApp_OrderOfOperations(t *testing.T) {
resetCounts() resetCounts()
_ = app.Run([]string{"command", "--generate-bash-completion"}) _ = app.Run([]string{"command", "--generate-completion"})
expect(t, counts.BashComplete, 1) expect(t, counts.ShellComplete, 1)
expect(t, counts.Total, 1) expect(t, counts.Total, 1)
resetCounts() resetCounts()

View File

@ -23,8 +23,8 @@ type Command struct {
ArgsUsage string ArgsUsage string
// The category the command is part of // The category the command is part of
Category string Category string
// The function to call when checking for bash command completions // The function to call when checking for shell command completions
BashComplete BashCompleteFunc ShellComplete ShellCompleteFunc
// An action to execute before any sub-subcommands are run, but after the context is ready // An action to execute before any sub-subcommands are run, but after the context is ready
// If a non-nil error is returned, no sub-subcommands are run // If a non-nil error is returned, no sub-subcommands are run
Before BeforeFunc Before BeforeFunc
@ -71,8 +71,8 @@ func (c *Command) Run(ctx *Context) (err error) {
c.appendFlag(HelpFlag) c.appendFlag(HelpFlag)
} }
if ctx.App.EnableBashCompletion { if ctx.App.EnableShellCompletion {
c.appendFlag(BashCompletionFlag) c.appendFlag(GenerateCompletionFlag)
} }
set := flagSet(c.Name, c.Flags) set := flagSet(c.Name, c.Flags)
@ -202,9 +202,9 @@ func (c *Command) startApp(ctx *Context) error {
sort.Sort(app.Categories.(*commandCategories)) sort.Sort(app.Categories.(*commandCategories))
// bash completion // bash completion
app.EnableBashCompletion = ctx.App.EnableBashCompletion app.EnableShellCompletion = ctx.App.EnableShellCompletion
if c.BashComplete != nil { if c.ShellComplete != nil {
app.BashComplete = c.BashComplete app.ShellComplete = c.ShellComplete
} }
// set the actions // set the actions

View File

@ -21,9 +21,9 @@ var (
commaWhitespace = regexp.MustCompile("[, ]+.*") commaWhitespace = regexp.MustCompile("[, ]+.*")
) )
// BashCompletionFlag enables bash-completion for all commands and subcommands // GenerateCompletionFlag enables completion for all commands and subcommands
var BashCompletionFlag = &BoolFlag{ var GenerateCompletionFlag = &BoolFlag{
Name: "generate-bash-completion", Name: "generate-completion",
Hidden: true, Hidden: true,
} }

View File

@ -1,7 +1,7 @@
package cli package cli
// BashCompleteFunc is an action to execute when the bash-completion flag is set // ShellCompleteFunc is an action to execute when the shell completion flag is set
type BashCompleteFunc func(*Context) type ShellCompleteFunc func(*Context)
// BeforeFunc is an action to execute before any subcommands are run, but after // BeforeFunc is 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 // the context is ready if a non-nil error is returned, no subcommands are run

14
help.go
View File

@ -182,16 +182,16 @@ func printVersion(c *Context) {
// ShowCompletions prints the lists of commands within a given context // ShowCompletions prints the lists of commands within a given context
func ShowCompletions(c *Context) { func ShowCompletions(c *Context) {
a := c.App a := c.App
if a != nil && a.BashComplete != nil { if a != nil && a.ShellComplete != nil {
a.BashComplete(c) a.ShellComplete(c)
} }
} }
// ShowCommandCompletions prints the custom completions for a given command // ShowCommandCompletions prints the custom completions for a given command
func ShowCommandCompletions(ctx *Context, command string) { func ShowCommandCompletions(ctx *Context, command string) {
c := ctx.App.Command(command) c := ctx.App.Command(command)
if c != nil && c.BashComplete != nil { if c != nil && c.ShellComplete != nil {
c.BashComplete(ctx) c.ShellComplete(ctx)
} }
} }
@ -270,7 +270,7 @@ func checkSubcommandHelp(c *Context) bool {
} }
func checkCompletions(c *Context) bool { func checkCompletions(c *Context) bool {
if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion { if c.Bool(GenerateCompletionFlag.Name) && c.App.EnableShellCompletion {
ShowCompletions(c) ShowCompletions(c)
return true return true
} }
@ -279,7 +279,7 @@ func checkCompletions(c *Context) bool {
} }
func checkCommandCompletions(c *Context, name string) bool { func checkCommandCompletions(c *Context, name string) bool {
if c.Bool(BashCompletionFlag.Name) && c.App.EnableBashCompletion { if c.Bool(GenerateCompletionFlag.Name) && c.App.EnableShellCompletion {
ShowCommandCompletions(c, name) ShowCommandCompletions(c, name)
return true return true
} }
@ -310,7 +310,7 @@ func bashCompletionCode(progName string) string {
local cur opts base; local cur opts base;
COMPREPLY=(); COMPREPLY=();
cur="${COMP_WORDS[COMP_CWORD]}"; cur="${COMP_WORDS[COMP_CWORD]}";
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion ); opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-completion );
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ); COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) );
return 0; return 0;
}; };