Skip to content

Commit 7f5c797

Browse files
authored
Merge pull request #2266 from siutsin/MutuallyExclusiveFlags-persistent-flags-are-not-propagated-to-subcommands
Fix: propagate MutuallyExclusiveFlags persistent flags to subcommands
2 parents af661dd + c30a566 commit 7f5c797

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

command_parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (cmd *Command) parseFlags(args Args) (Args, error) {
3636
pCmd.Name, cmd.Name,
3737
)
3838

39-
for _, fl := range pCmd.Flags {
39+
for _, fl := range pCmd.allFlags() {
4040
flNames := fl.Names()
4141

4242
pfl, ok := fl.(LocalFlag)

command_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5444,3 +5444,40 @@ func TestCommand_ParallelRun(t *testing.T) {
54445444
})
54455445
}
54465446
}
5447+
5448+
func TestCommand_ExclusiveFlagsPersistentPropagation(t *testing.T) {
5449+
var subCmdAlphaValue string
5450+
5451+
cmd := &Command{
5452+
Name: "root",
5453+
MutuallyExclusiveFlags: []MutuallyExclusiveFlags{
5454+
{
5455+
Flags: [][]Flag{
5456+
{
5457+
&StringFlag{
5458+
Name: "alpha",
5459+
},
5460+
},
5461+
{
5462+
&StringFlag{
5463+
Name: "beta",
5464+
},
5465+
},
5466+
},
5467+
},
5468+
},
5469+
Commands: []*Command{
5470+
{
5471+
Name: "sub",
5472+
Action: func(_ context.Context, cmd *Command) error {
5473+
subCmdAlphaValue = cmd.String("alpha")
5474+
return nil
5475+
},
5476+
},
5477+
},
5478+
}
5479+
5480+
err := cmd.Run(buildTestContext(t), []string{"root", "sub", "--alpha", "hello"})
5481+
require.NoError(t, err)
5482+
assert.Equal(t, "hello", subCmdAlphaValue)
5483+
}

docs/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ require (
1111

1212
require (
1313
github.com/BurntSushi/toml v1.5.0 // indirect
14-
github.com/stretchr/testify v1.10.0 // indirect
14+
github.com/stretchr/testify v1.11.1 // indirect
1515
gopkg.in/yaml.v3 v3.0.1 // indirect
1616
)

docs/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
99
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
1010
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
1111
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
12+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
1213
github.com/urfave/cli-altsrc/v3 v3.0.0-alpha2 h1:j4SaBpPB8++L0c0KuTnz/Yus3UQoWJ54hQjhIMW8rCM=
1314
github.com/urfave/cli-altsrc/v3 v3.0.0-alpha2/go.mod h1:Q79oyIY/z4jtzIrKEK6MUeWC7/szGr46x4QdOaOAIWc=
1415
github.com/urfave/cli-altsrc/v3 v3.0.1 h1:v+gHk59syLk8ao9rYybZs43+D5ut/gzj0omqQ1XYl8k=

0 commit comments

Comments
 (0)