Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix:(issue_1617) Fix Bash completion for subcommands #1618

Merged
merged 2 commits into from Dec 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions command.go
Expand Up @@ -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) {
Expand All @@ -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
}

Expand Down
21 changes: 6 additions & 15 deletions help.go
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -308,15 +308,15 @@ 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)
c := ctx.Command.Command(command)
if c != nil {
if c.BashComplete != nil {
c.BashComplete(ctx)
Expand Down Expand Up @@ -453,7 +453,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
}
Expand All @@ -463,15 +463,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
}
Expand Down