Skip to content

Commit 1551d8c

Browse files
committed
Fix:(issue_2209) Add mutex flags to completion
1 parent 08b187a commit 1551d8c

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

completion_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7+
"os"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
@@ -181,6 +182,51 @@ func TestCompletionSubcommand(t *testing.T) {
181182
}
182183
}
183184

185+
func TestMutuallyExclusiveFlagsCompletion(t *testing.T) {
186+
osArgsBak := os.Args
187+
defer func() {
188+
os.Args = osArgsBak
189+
}()
190+
out := &bytes.Buffer{}
191+
192+
cmd := &Command{
193+
EnableShellCompletion: true,
194+
Writer: out,
195+
Flags: []Flag{
196+
&StringFlag{
197+
Name: "l1",
198+
},
199+
},
200+
MutuallyExclusiveFlags: []MutuallyExclusiveFlags{
201+
{
202+
Flags: [][]Flag{
203+
{
204+
&StringFlag{
205+
Name: "m1",
206+
},
207+
&StringFlag{
208+
Name: "m2",
209+
},
210+
},
211+
{
212+
&StringFlag{
213+
Name: "l2",
214+
},
215+
},
216+
},
217+
},
218+
},
219+
}
220+
221+
os.Args = []string{"foo", "-", completionFlag}
222+
err := cmd.Run(buildTestContext(t), os.Args)
223+
assert.NoError(t, err, "Expected no error for completion")
224+
assert.Contains(t, out.String(), "l1", "Expected output to contain flag l1")
225+
assert.Contains(t, out.String(), "m1", "Expected output to contain flag m1")
226+
assert.Contains(t, out.String(), "m2", "Expected output to contain flag m2")
227+
assert.Contains(t, out.String(), "l2", "Expected output to contain flag l2")
228+
}
229+
184230
type mockWriter struct {
185231
err error
186232
}

help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func DefaultCompleteWithFlags(ctx context.Context, cmd *Command) {
271271

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

help_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,7 @@ func TestDefaultCompleteWithFlags(t *testing.T) {
13711371
tc.cmd.parsedArgs = &stringSliceArgs{
13721372
tc.argv[1:],
13731373
}
1374+
tc.cmd.appliedFlags = tc.cmd.Flags
13741375
f := DefaultCompleteWithFlags
13751376
f(buildTestContext(ct), tc.cmd)
13761377

0 commit comments

Comments
 (0)