Skip to content

Commit

Permalink
Merge pull request #1747 from dearchap/issue_1668
Browse files Browse the repository at this point in the history
Fix:(issue_1668) Add test case for sub command of sub command completion
  • Loading branch information
dearchap authored Jun 7, 2023
2 parents 60247a7 + d93839a commit c16be32
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 36 deletions.
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

0 comments on commit c16be32

Please sign in to comment.