From badc19f4b52fe16841190d53a76211710fc1ce3c Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Wed, 7 Dec 2022 18:02:14 -0500 Subject: [PATCH 1/2] Fix:(issue_1617) Fix Bash completion for subcommands --- command.go | 10 +++++----- godoc-current.txt | 3 --- help.go | 32 +++++--------------------------- testdata/godoc-v2.x.txt | 3 --- 4 files changed, 10 insertions(+), 38 deletions(-) diff --git a/command.go b/command.go index b8a944d641..da9cf5302a 100644 --- a/command.go +++ b/command.go @@ -136,6 +136,10 @@ func (c *Command) setup(ctx *Context) { newCmds = append(newCmds, scmd) } c.Subcommands = newCmds + + if c.BashComplete == nil { + c.BashComplete = DefaultCompleteWithFlags(c) + } } func (c *Command) Run(cCtx *Context, arguments ...string) (err error) { @@ -148,11 +152,7 @@ func (c *Command) Run(cCtx *Context, arguments ...string) (err error) { set, err := c.parseFlags(&a, cCtx.shellComplete) cCtx.flagSet = set - if c.isRoot { - if checkCompletions(cCtx) { - return nil - } - } else if checkCommandCompletions(cCtx, c.Name) { + if checkCompletions(cCtx) { return nil } diff --git a/godoc-current.txt b/godoc-current.txt index 6afd244f25..13dcbe2bca 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -205,9 +205,6 @@ func ShowAppHelpAndExit(c *Context, exitCode int) ShowAppHelpAndExit - Prints the list of subcommands for the app and exits with exit code. -func ShowCommandCompletions(ctx *Context, command string) - ShowCommandCompletions prints the custom completions for a given command - func ShowCommandHelp(ctx *Context, command string) error ShowCommandHelp prints help for the given command diff --git a/help.go b/help.go index 2ccd3b71e6..c40cacbad6 100644 --- a/help.go +++ b/help.go @@ -227,7 +227,7 @@ func DefaultCompleteWithFlags(cmd *Command) func(cCtx *Context) { return } - printCommandSuggestions(cCtx.App.Commands, cCtx.App.Writer) + printCommandSuggestions(cCtx.Command.Subcommands, cCtx.App.Writer) } } @@ -308,25 +308,12 @@ func printVersion(cCtx *Context) { // ShowCompletions prints the lists of commands within a given context func ShowCompletions(cCtx *Context) { - a := cCtx.App - if a != nil && a.BashComplete != nil { - a.BashComplete(cCtx) + c := cCtx.Command + if c != nil && c.BashComplete != nil { + c.BashComplete(cCtx) } } -// ShowCommandCompletions prints the custom completions for a given command -func ShowCommandCompletions(ctx *Context, command string) { - c := ctx.App.Command(command) - if c != nil { - if c.BashComplete != nil { - c.BashComplete(ctx) - } else { - DefaultCompleteWithFlags(c)(ctx) - } - } - -} - // printHelpCustom is the default implementation of HelpPrinterCustom. // // The customFuncs map will be combined with a default template.FuncMap to @@ -453,7 +440,7 @@ func checkCompletions(cCtx *Context) bool { if args := cCtx.Args(); args.Present() { name := args.First() - if cmd := cCtx.App.Command(name); cmd != nil { + if cmd := cCtx.Command.Command(name); cmd != nil { // let the command handle the completion return false } @@ -463,15 +450,6 @@ func checkCompletions(cCtx *Context) bool { return true } -func checkCommandCompletions(c *Context, name string) bool { - if !c.shellComplete { - return false - } - - ShowCommandCompletions(c, name) - return true -} - func subtract(a, b int) int { return a - b } diff --git a/testdata/godoc-v2.x.txt b/testdata/godoc-v2.x.txt index 6afd244f25..13dcbe2bca 100644 --- a/testdata/godoc-v2.x.txt +++ b/testdata/godoc-v2.x.txt @@ -205,9 +205,6 @@ func ShowAppHelpAndExit(c *Context, exitCode int) ShowAppHelpAndExit - Prints the list of subcommands for the app and exits with exit code. -func ShowCommandCompletions(ctx *Context, command string) - ShowCommandCompletions prints the custom completions for a given command - func ShowCommandHelp(ctx *Context, command string) error ShowCommandHelp prints help for the given command From 659672b1bb9b2c0651bace5408984d08d7baf9b8 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Wed, 7 Dec 2022 18:46:33 -0500 Subject: [PATCH 2/2] Fix docs issue --- godoc-current.txt | 3 +++ help.go | 13 +++++++++++++ testdata/godoc-v2.x.txt | 3 +++ 3 files changed, 19 insertions(+) diff --git a/godoc-current.txt b/godoc-current.txt index 13dcbe2bca..6afd244f25 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -205,6 +205,9 @@ func ShowAppHelpAndExit(c *Context, exitCode int) ShowAppHelpAndExit - Prints the list of subcommands for the app and exits with exit code. +func ShowCommandCompletions(ctx *Context, command string) + ShowCommandCompletions prints the custom completions for a given command + func ShowCommandHelp(ctx *Context, command string) error ShowCommandHelp prints help for the given command diff --git a/help.go b/help.go index c40cacbad6..c7b8f55a58 100644 --- a/help.go +++ b/help.go @@ -314,6 +314,19 @@ func ShowCompletions(cCtx *Context) { } } +// ShowCommandCompletions prints the custom completions for a given command +func ShowCommandCompletions(ctx *Context, command string) { + c := ctx.Command.Command(command) + if c != nil { + if c.BashComplete != nil { + c.BashComplete(ctx) + } else { + DefaultCompleteWithFlags(c)(ctx) + } + } + +} + // printHelpCustom is the default implementation of HelpPrinterCustom. // // The customFuncs map will be combined with a default template.FuncMap to diff --git a/testdata/godoc-v2.x.txt b/testdata/godoc-v2.x.txt index 13dcbe2bca..6afd244f25 100644 --- a/testdata/godoc-v2.x.txt +++ b/testdata/godoc-v2.x.txt @@ -205,6 +205,9 @@ func ShowAppHelpAndExit(c *Context, exitCode int) ShowAppHelpAndExit - Prints the list of subcommands for the app and exits with exit code. +func ShowCommandCompletions(ctx *Context, command string) + ShowCommandCompletions prints the custom completions for a given command + func ShowCommandHelp(ctx *Context, command string) error ShowCommandHelp prints help for the given command