Skip to content

Commit c605461

Browse files
authored
Merge pull request #1130 from carapace-sh/flagset-exclusive-self
flagset: skip self in mutually exclusive check
2 parents 8143247 + cfca7fe commit c605461

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

example/cmd/special.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ func init() {
2828
carapace.Gen(specialCmd).Standalone()
2929

3030
specialCmd.Flags().CountP("count", "c", "count flag")
31-
specialCmd.Flags().Bool("exclusive1", false, "mutually exclusive flag")
32-
specialCmd.Flags().Bool("exclusive2", false, "mutually exclusive flag")
31+
specialCmd.Flags().Bool("exclusive", false, "mutually exclusive flag")
32+
specialCmd.Flags().Count("exclusiveRepeatable", "mutually exclusive repeatable flag")
3333
specialCmd.Flags().StringP("optarg", "o", "", "optional argument")
3434

3535
specialCmd.Flag("optarg").NoOptDefVal = " "
3636

37-
specialCmd.MarkFlagsMutuallyExclusive("exclusive1", "exclusive2")
37+
specialCmd.MarkFlagsMutuallyExclusive("exclusive", "exclusiveRepeatable")
3838

3939
rootCmd.AddCommand(specialCmd)
4040

example/cmd/special_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/carapace-sh/carapace"
7+
"github.com/carapace-sh/carapace/pkg/sandbox"
8+
)
9+
10+
func TestMutuallyExclusive(t *testing.T) {
11+
sandbox.Package(t, "github.com/carapace-sh/carapace/example")(func(s *sandbox.Sandbox) {
12+
s.Run("special", "--e").
13+
Expect(carapace.ActionValuesDescribed(
14+
"--exclusive", "mutually exclusive flag",
15+
"--exclusiveRepeatable", "mutually exclusive repeatable flag",
16+
).NoSpace('.').
17+
Tag("longhand flags"))
18+
19+
s.Run("special", "--exclusive", "--e").
20+
Expect(carapace.ActionValues().NoSpace('.'))
21+
22+
s.Run("special", "--exclusiveRepeatable", "--e").
23+
Expect(carapace.ActionValuesDescribed(
24+
"--exclusiveRepeatable", "mutually exclusive repeatable flag",
25+
).NoSpace('.').
26+
Tag("longhand flags"))
27+
28+
})
29+
}

internal/pflagfork/flagset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (f FlagSet) IsMutuallyExclusive(flag *pflag.Flag) bool {
3737
if groups, ok := flag.Annotations["cobra_annotation_mutually_exclusive"]; ok {
3838
for _, group := range groups {
3939
for _, name := range strings.Split(group, " ") {
40-
if other := f.Lookup(name); other != nil && other.Changed {
40+
if other := f.Lookup(name); other != nil && other.Changed && other != flag {
4141
return true
4242
}
4343
}

0 commit comments

Comments
 (0)