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:
parent
34a8b004d2
commit
94bc26fd1c
30
README.md
30
README.md
@ -35,7 +35,7 @@ applications in an expressive way.
|
||||
* [Subcommands](#subcommands)
|
||||
* [Subcommands categories](#subcommands-categories)
|
||||
* [Exit code](#exit-code)
|
||||
* [Bash Completion](#bash-completion)
|
||||
* [Shell Completion](#shell-completion)
|
||||
+ [Enabling](#enabling)
|
||||
+ [Distribution](#distribution)
|
||||
+ [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
|
||||
show an app's subcommands, but you can write your own completion methods for
|
||||
the App or its subcommands.
|
||||
|
||||
<!-- {
|
||||
"args": ["complete", "--generate-bash-completion"],
|
||||
"args": ["complete", "--generate-completion"],
|
||||
"output": "laundry"
|
||||
} -->
|
||||
``` go
|
||||
@ -765,7 +765,7 @@ func main() {
|
||||
tasks := []string{"cook", "clean", "laundry", "eat", "sleep", "code"}
|
||||
|
||||
app := &cli.App{
|
||||
EnableBashCompletion: true,
|
||||
EnableShellCompletion: true,
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "complete",
|
||||
@ -775,7 +775,7 @@ func main() {
|
||||
fmt.Println("completed task: ", c.Args().First())
|
||||
return nil
|
||||
},
|
||||
BashComplete: func(c *cli.Context) {
|
||||
ShellComplete: func(c *cli.Context) {
|
||||
// This will complete if no args are passed
|
||||
if c.NArg() > 0 {
|
||||
return
|
||||
@ -794,7 +794,7 @@ func main() {
|
||||
|
||||
#### 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:
|
||||
|
||||
@ -825,8 +825,8 @@ to the name of their program (as above).
|
||||
|
||||
#### Customization
|
||||
|
||||
The default bash completion flag (`--generate-bash-completion`) is defined as
|
||||
`cli.BashCompletionFlag`, and may be redefined if desired, e.g.:
|
||||
The default shell completion flag (`--generate-completion`) is defined as
|
||||
`cli.GenerateCompletionFlag`, and may be redefined if desired, e.g.:
|
||||
|
||||
<!-- {
|
||||
"args": ["--compgen"],
|
||||
@ -842,13 +842,13 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
cli.BashCompletionFlag = &cli.BoolFlag{
|
||||
cli.GenerateCompletionFlag = &cli.BoolFlag{
|
||||
Name: "compgen",
|
||||
Hidden: true,
|
||||
}
|
||||
|
||||
app := &cli.App{
|
||||
EnableBashCompletion: true,
|
||||
EnableShellCompletion: true,
|
||||
Commands: []*cli.Command{
|
||||
{
|
||||
Name: "wat",
|
||||
@ -1058,7 +1058,7 @@ func init() {
|
||||
cli.SubcommandHelpTemplate += "\nor something\n"
|
||||
|
||||
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.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
|
||||
@ -1138,7 +1138,7 @@ func main() {
|
||||
HideHelp: false,
|
||||
Hidden: false,
|
||||
HelpName: "doo!",
|
||||
BashComplete: func(c *cli.Context) {
|
||||
ShellComplete: func(c *cli.Context) {
|
||||
fmt.Fprintf(c.App.Writer, "--better\n")
|
||||
},
|
||||
Before: func(c *cli.Context) error {
|
||||
@ -1181,10 +1181,10 @@ func main() {
|
||||
&cli.UintFlag{Name: "age"},
|
||||
&cli.Uint64Flag{Name: "bigage"},
|
||||
},
|
||||
EnableBashCompletion: true,
|
||||
EnableShellCompletion: true,
|
||||
HideHelp: false,
|
||||
HideVersion: false,
|
||||
BashComplete: func(c *cli.Context) {
|
||||
ShellComplete: func(c *cli.Context) {
|
||||
fmt.Fprintf(c.App.Writer, "lipstick\nkiss\nme\nlipstick\nringo\n")
|
||||
},
|
||||
Before: func(c *cli.Context) error {
|
||||
|
29
app.go
29
app.go
@ -29,16 +29,16 @@ type App struct {
|
||||
Commands []*Command
|
||||
// List of flags to parse
|
||||
Flags []Flag
|
||||
// Boolean to enable bash completion commands
|
||||
EnableBashCompletion bool
|
||||
// Boolean to enable shell completion commands
|
||||
EnableShellCompletion bool
|
||||
// Boolean to hide built-in help command
|
||||
HideHelp bool
|
||||
// Boolean to hide built-in version flag and the VERSION section of help
|
||||
HideVersion bool
|
||||
// Categories contains the categorized commands and is populated on app startup
|
||||
Categories CommandCategories
|
||||
// An action to execute when the bash-completion flag is set
|
||||
BashComplete BashCompleteFunc
|
||||
// An action to execute when the shell completion flag is set
|
||||
ShellComplete ShellCompleteFunc
|
||||
// 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 BeforeFunc
|
||||
@ -103,8 +103,8 @@ func (a *App) Setup() {
|
||||
a.Version = "0.0.0"
|
||||
}
|
||||
|
||||
if a.BashComplete == nil {
|
||||
a.BashComplete = DefaultAppComplete
|
||||
if a.ShellComplete == nil {
|
||||
a.ShellComplete = DefaultAppComplete
|
||||
}
|
||||
|
||||
if a.Action == nil {
|
||||
@ -136,8 +136,8 @@ func (a *App) Setup() {
|
||||
}
|
||||
}
|
||||
|
||||
if a.EnableBashCompletion {
|
||||
a.appendFlag(BashCompletionFlag)
|
||||
if a.EnableShellCompletion {
|
||||
a.appendFlag(GenerateCompletionFlag)
|
||||
a.appendFlag(InitCompletionFlag)
|
||||
}
|
||||
|
||||
@ -177,9 +177,12 @@ func (a *App) Run(arguments []string) (err error) {
|
||||
return nil
|
||||
}
|
||||
|
||||
var done bool
|
||||
if done, err = checkInitCompletion(context); done {
|
||||
return nil
|
||||
if done, cerr := checkInitCompletion(context); done {
|
||||
if cerr != nil {
|
||||
err = cerr
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@ -266,8 +269,8 @@ func (a *App) RunAsSubcommand(ctx *Context) (err error) {
|
||||
a.Commands = newCmds
|
||||
|
||||
// append flags
|
||||
if a.EnableBashCompletion {
|
||||
a.appendFlag(BashCompletionFlag)
|
||||
if a.EnableShellCompletion {
|
||||
a.appendFlag(GenerateCompletionFlag)
|
||||
}
|
||||
|
||||
// parse flags
|
||||
|
20
app_test.go
20
app_test.go
@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
type opCounts struct {
|
||||
Total, BashComplete, OnUsageError, Before, CommandNotFound, Action, After, SubCommand int
|
||||
Total, ShellComplete, OnUsageError, Before, CommandNotFound, Action, After, SubCommand int
|
||||
}
|
||||
|
||||
func ExampleApp_Run() {
|
||||
@ -112,13 +112,13 @@ func ExampleApp_Run_help() {
|
||||
// This is how we describe describeit the function
|
||||
}
|
||||
|
||||
func ExampleApp_Run_bashComplete() {
|
||||
func ExampleApp_Run_shellComplete() {
|
||||
// set args for examples sake
|
||||
os.Args = []string{"greet", "--generate-bash-completion"}
|
||||
os.Args = []string{"greet", "--generate-completion"}
|
||||
|
||||
app := &App{
|
||||
Name: "greet",
|
||||
EnableBashCompletion: true,
|
||||
EnableShellCompletion: true,
|
||||
Commands: []*Command{
|
||||
{
|
||||
Name: "describeit",
|
||||
@ -132,7 +132,7 @@ func ExampleApp_Run_bashComplete() {
|
||||
}, {
|
||||
Name: "next",
|
||||
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 {
|
||||
fmt.Printf("the next example")
|
||||
return nil
|
||||
@ -735,10 +735,10 @@ func TestApp_OrderOfOperations(t *testing.T) {
|
||||
resetCounts := func() { counts = &opCounts{} }
|
||||
|
||||
app := &App{
|
||||
EnableBashCompletion: true,
|
||||
BashComplete: func(c *Context) {
|
||||
EnableShellCompletion: true,
|
||||
ShellComplete: func(c *Context) {
|
||||
counts.Total++
|
||||
counts.BashComplete = counts.Total
|
||||
counts.ShellComplete = counts.Total
|
||||
},
|
||||
OnUsageError: func(c *Context, err error, isSubcommand bool) error {
|
||||
counts.Total++
|
||||
@ -801,8 +801,8 @@ func TestApp_OrderOfOperations(t *testing.T) {
|
||||
|
||||
resetCounts()
|
||||
|
||||
_ = app.Run([]string{"command", "--generate-bash-completion"})
|
||||
expect(t, counts.BashComplete, 1)
|
||||
_ = app.Run([]string{"command", "--generate-completion"})
|
||||
expect(t, counts.ShellComplete, 1)
|
||||
expect(t, counts.Total, 1)
|
||||
|
||||
resetCounts()
|
||||
|
14
command.go
14
command.go
@ -23,8 +23,8 @@ type Command struct {
|
||||
ArgsUsage string
|
||||
// The category the command is part of
|
||||
Category string
|
||||
// The function to call when checking for bash command completions
|
||||
BashComplete BashCompleteFunc
|
||||
// The function to call when checking for shell command completions
|
||||
ShellComplete ShellCompleteFunc
|
||||
// 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
|
||||
Before BeforeFunc
|
||||
@ -71,8 +71,8 @@ func (c *Command) Run(ctx *Context) (err error) {
|
||||
c.appendFlag(HelpFlag)
|
||||
}
|
||||
|
||||
if ctx.App.EnableBashCompletion {
|
||||
c.appendFlag(BashCompletionFlag)
|
||||
if ctx.App.EnableShellCompletion {
|
||||
c.appendFlag(GenerateCompletionFlag)
|
||||
}
|
||||
|
||||
set := flagSet(c.Name, c.Flags)
|
||||
@ -202,9 +202,9 @@ func (c *Command) startApp(ctx *Context) error {
|
||||
sort.Sort(app.Categories.(*commandCategories))
|
||||
|
||||
// bash completion
|
||||
app.EnableBashCompletion = ctx.App.EnableBashCompletion
|
||||
if c.BashComplete != nil {
|
||||
app.BashComplete = c.BashComplete
|
||||
app.EnableShellCompletion = ctx.App.EnableShellCompletion
|
||||
if c.ShellComplete != nil {
|
||||
app.ShellComplete = c.ShellComplete
|
||||
}
|
||||
|
||||
// set the actions
|
||||
|
6
flag.go
6
flag.go
@ -21,9 +21,9 @@ var (
|
||||
commaWhitespace = regexp.MustCompile("[, ]+.*")
|
||||
)
|
||||
|
||||
// BashCompletionFlag enables bash-completion for all commands and subcommands
|
||||
var BashCompletionFlag = &BoolFlag{
|
||||
Name: "generate-bash-completion",
|
||||
// GenerateCompletionFlag enables completion for all commands and subcommands
|
||||
var GenerateCompletionFlag = &BoolFlag{
|
||||
Name: "generate-completion",
|
||||
Hidden: true,
|
||||
}
|
||||
|
||||
|
4
funcs.go
4
funcs.go
@ -1,7 +1,7 @@
|
||||
package cli
|
||||
|
||||
// BashCompleteFunc is an action to execute when the bash-completion flag is set
|
||||
type BashCompleteFunc func(*Context)
|
||||
// ShellCompleteFunc is an action to execute when the shell completion flag is set
|
||||
type ShellCompleteFunc func(*Context)
|
||||
|
||||
// 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
|
||||
|
14
help.go
14
help.go
@ -182,16 +182,16 @@ func printVersion(c *Context) {
|
||||
// ShowCompletions prints the lists of commands within a given context
|
||||
func ShowCompletions(c *Context) {
|
||||
a := c.App
|
||||
if a != nil && a.BashComplete != nil {
|
||||
a.BashComplete(c)
|
||||
if a != nil && a.ShellComplete != nil {
|
||||
a.ShellComplete(c)
|
||||
}
|
||||
}
|
||||
|
||||
// ShowCommandCompletions prints the custom completions for a given command
|
||||
func ShowCommandCompletions(ctx *Context, command string) {
|
||||
c := ctx.App.Command(command)
|
||||
if c != nil && c.BashComplete != nil {
|
||||
c.BashComplete(ctx)
|
||||
if c != nil && c.ShellComplete != nil {
|
||||
c.ShellComplete(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
@ -270,7 +270,7 @@ func checkSubcommandHelp(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)
|
||||
return true
|
||||
}
|
||||
@ -279,7 +279,7 @@ func checkCompletions(c *Context) 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)
|
||||
return true
|
||||
}
|
||||
@ -310,7 +310,7 @@ func bashCompletionCode(progName string) string {
|
||||
local cur opts base;
|
||||
COMPREPLY=();
|
||||
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}) );
|
||||
return 0;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user