Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions completion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -181,6 +182,51 @@ func TestCompletionSubcommand(t *testing.T) {
}
}

func TestMutuallyExclusiveFlagsCompletion(t *testing.T) {
osArgsBak := os.Args
defer func() {
os.Args = osArgsBak
}()
out := &bytes.Buffer{}

cmd := &Command{
EnableShellCompletion: true,
Writer: out,
Flags: []Flag{
&StringFlag{
Name: "l1",
},
},
MutuallyExclusiveFlags: []MutuallyExclusiveFlags{
{
Flags: [][]Flag{
{
&StringFlag{
Name: "m1",
},
&StringFlag{
Name: "m2",
},
},
{
&StringFlag{
Name: "l2",
},
},
},
},
},
}

os.Args = []string{"foo", "-", completionFlag}
err := cmd.Run(buildTestContext(t), os.Args)
assert.NoError(t, err, "Expected no error for completion")
assert.Contains(t, out.String(), "l1", "Expected output to contain flag l1")
assert.Contains(t, out.String(), "m1", "Expected output to contain flag m1")
assert.Contains(t, out.String(), "m2", "Expected output to contain flag m2")
assert.Contains(t, out.String(), "l2", "Expected output to contain flag l2")
}

type mockWriter struct {
err error
}
Expand Down
2 changes: 1 addition & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func DefaultCompleteWithFlags(ctx context.Context, cmd *Command) {

if strings.HasPrefix(lastArg, "-") {
tracef("printing flag suggestion for flag[%v] on command %[1]q", lastArg, cmd.Name)
printFlagSuggestions(lastArg, cmd.Flags, cmd.Root().Writer)
printFlagSuggestions(lastArg, cmd.appliedFlags, cmd.Root().Writer)
return
}

Expand Down
1 change: 1 addition & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
tc.cmd.parsedArgs = &stringSliceArgs{
tc.argv[1:],
}
tc.cmd.appliedFlags = tc.cmd.Flags
f := DefaultCompleteWithFlags
f(buildTestContext(ct), tc.cmd)

Expand Down