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_1668) Add test case for sub command of sub command completion #1747

Merged
merged 4 commits into from
Jun 7, 2023
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
8 changes: 7 additions & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,15 @@ func printFlagSuggestions(lastArg string, flags []Flag, writer io.Writer) {

func DefaultCompleteWithFlags(cmd *Command) func(cCtx *Context) {
return func(cCtx *Context) {
var lastArg string

// TODO: This shouldnt depend on os.Args rather it should
// depend on root arguments passed to App
if len(os.Args) > 2 {
lastArg := os.Args[len(os.Args)-2]
lastArg = os.Args[len(os.Args)-2]
}

if lastArg != "" {
if strings.HasPrefix(lastArg, "-") {
if cmd != nil {
printFlagSuggestions(lastArg, cmd.Flags, cCtx.App.Writer)
Expand Down
117 changes: 82 additions & 35 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1162,21 +1162,19 @@ func TestDefaultCompleteWithFlags(t *testing.T) {

for _, tc := range []struct {
name string
c *Context
cmd *Command
a *App
argv []string
expected string
}{
{
name: "empty",
c: &Context{App: &App{}},
cmd: &Command{},
a: &App{},
argv: []string{"prog", "cmd"},
expected: "",
},
{
name: "typical-flag-suggestion",
c: &Context{App: &App{
a: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
Expand All @@ -1185,68 +1183,117 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
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",
expected: "--everybody-jump-on\n",
},
{
name: "typical-command-suggestion",
c: &Context{App: &App{
a: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
&Int64Flag{Name: "everybody-jump-on"},
},
}},
cmd: &Command{
Name: "putz",
Subcommands: []*Command{
{Name: "futz"},
Commands: []*Command{
{
Name: "putz",
Subcommands: []*Command{
{Name: "futz"},
},
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
},
},
},
argv: []string{"cmd", "--generate-bash-completion"},
expected: "putz\n",
},
{
name: "typical-subcommand-suggestion",
a: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
&BoolFlag{Name: "happiness"},
&Int64Flag{Name: "everybody-jump-on"},
},
Commands: []*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",
argv: []string{"cmd", "--happiness", "putz", "--generate-bash-completion"},
expected: "futz\nhelp\nh\n",
},
{
name: "autocomplete-with-spaces",
c: &Context{App: &App{
name: "typical-subcommand-subcommand-suggestion",
a: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "happiness"},
&Int64Flag{Name: "everybody-jump-on"},
},
}},
cmd: &Command{
Name: "putz",
Subcommands: []*Command{
{Name: "help"},
Commands: []*Command{
{
Name: "putz",
Subcommands: []*Command{
{
Name: "futz",
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
},
},
},
},
},
argv: []string{"cmd", "--happiness", "putz", "futz", "-e", "--generate-bash-completion"},
expected: "--excitement\n",
},
{
name: "autocomplete-with-spaces",
a: &App{
Name: "cmd",
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
&BoolFlag{Name: "happiness"},
&Int64Flag{Name: "everybody-jump-on"},
},
Commands: []*Command{
{
Name: "putz",
Subcommands: []*Command{
{Name: "help"},
},
Flags: []Flag{
&BoolFlag{Name: "excitement"},
&StringFlag{Name: "hat-shape"},
},
},
},
},
argv: []string{"cmd", "--url", "http://localhost:8000", "h", "--generate-bash-completion"},
argv: []string{"cmd", "--happiness", "putz", "h", "--generate-bash-completion"},
expected: "help\n",
},
} {
t.Run(tc.name, func(ct *testing.T) {
writer := &bytes.Buffer{}
tc.c.App.Writer = writer

tc.a.EnableBashCompletion = true
tc.a.HideHelp = true
tc.a.Writer = writer
os.Args = tc.argv
f := DefaultCompleteWithFlags(tc.cmd)
f(tc.c)
tc.a.Run(tc.argv)

written := writer.String()

Expand Down