From a8e44a8b5b1a75b1bfd95a46f809f9aba3ddb214 Mon Sep 17 00:00:00 2001 From: Zack Scholl Date: Thu, 17 Sep 2020 12:46:05 -0700 Subject: [PATCH 1/2] show only subcommand flags with bash completion --- help.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/help.go b/help.go index c1e974a481..95ba188945 100644 --- a/help.go +++ b/help.go @@ -164,9 +164,10 @@ func DefaultCompleteWithFlags(cmd *Command) func(c *Context) { if len(os.Args) > 2 { lastArg := os.Args[len(os.Args)-2] if strings.HasPrefix(lastArg, "-") { - printFlagSuggestions(lastArg, c.App.Flags, c.App.Writer) if cmd != nil { printFlagSuggestions(lastArg, cmd.Flags, c.App.Writer) + } else { + printFlagSuggestions(lastArg, c.App.Flags, c.App.Writer) } return } From c4c15e14538e59cee39aed139ce75bcf375ff34c Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Thu, 21 Apr 2022 20:07:06 -0400 Subject: [PATCH 2/2] Writing tests around changes from #1186 --- help.go | 14 ++++++--- help_test.go | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 4 deletions(-) diff --git a/help.go b/help.go index 03cead43fc..fad990a6e3 100644 --- a/help.go +++ b/help.go @@ -163,20 +163,26 @@ func DefaultCompleteWithFlags(cmd *Command) func(c *Context) { return func(c *Context) { if len(os.Args) > 2 { lastArg := os.Args[len(os.Args)-2] + if strings.HasPrefix(lastArg, "-") { if cmd != nil { printFlagSuggestions(lastArg, cmd.Flags, c.App.Writer) - } else { - printFlagSuggestions(lastArg, c.App.Flags, c.App.Writer) + + return } + + printFlagSuggestions(lastArg, c.App.Flags, c.App.Writer) + return } } + if cmd != nil { printCommandSuggestions(cmd.Subcommands, c.App.Writer) - } else { - printCommandSuggestions(c.App.Commands, c.App.Writer) + return } + + printCommandSuggestions(c.App.Commands, c.App.Writer) } } diff --git a/help_test.go b/help_test.go index 8dd262d56c..98530dd2a6 100644 --- a/help_test.go +++ b/help_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "io/ioutil" + "os" "runtime" "strings" "testing" @@ -1037,3 +1038,85 @@ func TestHideHelpCommand_WithSubcommands(t *testing.T) { t.Errorf("Run returned unexpected error: %v", err) } } + +func TestDefaultCompleteWithFlags(t *testing.T) { + origArgv := os.Args + + t.Cleanup(func() { + os.Args = origArgv + }) + + for _, tc := range []struct { + name string + c *Context + cmd *Command + argv []string + expected string + }{ + { + name: "empty", + c: &Context{App: &App{}}, + cmd: &Command{}, + argv: []string{"prog", "cmd"}, + expected: "", + }, + { + name: "typical-flag-suggestion", + c: &Context{App: &App{ + Name: "cmd", + Flags: []Flag{ + &BoolFlag{Name: "happiness"}, + &Int64Flag{Name: "everybody-jump-on"}, + }, + Commands: []*Command{ + {Name: "putz"}, + }, + }}, + cmd: &Command{ + Flags: []Flag{ + &BoolFlag{Name: "excitement"}, + &StringFlag{Name: "hat-shape"}, + }, + }, + argv: []string{"cmd", "--e", "--generate-bash-completion"}, + expected: "--excitement\n", + }, + { + name: "typical-command-suggestion", + c: &Context{App: &App{ + Name: "cmd", + Flags: []Flag{ + &BoolFlag{Name: "happiness"}, + &Int64Flag{Name: "everybody-jump-on"}, + }, + }}, + cmd: &Command{ + Name: "putz", + Subcommands: []*Command{ + {Name: "futz"}, + }, + Flags: []Flag{ + &BoolFlag{Name: "excitement"}, + &StringFlag{Name: "hat-shape"}, + }, + }, + argv: []string{"cmd", "--generate-bash-completion"}, + expected: "futz\n", + }, + } { + t.Run(tc.name, func(ct *testing.T) { + writer := &bytes.Buffer{} + tc.c.App.Writer = writer + + os.Args = tc.argv + f := DefaultCompleteWithFlags(tc.cmd) + f(tc.c) + + written := writer.String() + + if written != tc.expected { + ct.Errorf("written help does not match expected %q != %q", written, tc.expected) + } + }) + } +}