Skip to content

Commit

Permalink
Fix:(issue_1617) Fix Bash completion for subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Dec 7, 2022
1 parent f9652e3 commit badc19f
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 38 deletions.
10 changes: 5 additions & 5 deletions command.go
Original file line number Diff line number Diff line change
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
3 changes: 0 additions & 3 deletions godoc-current.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
32 changes: 5 additions & 27 deletions help.go
Original file line number Diff line number Diff line change
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,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
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
3 changes: 0 additions & 3 deletions testdata/godoc-v2.x.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit badc19f

Please sign in to comment.