Skip to content

Commit

Permalink
MutuallyExclusiveFlags group category is propagated to all flags
Browse files Browse the repository at this point in the history
  • Loading branch information
joshfrench committed Feb 5, 2024
1 parent 873755e commit d754265
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 30 deletions.
10 changes: 10 additions & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ func (cmd *Command) setupDefaults(osArgs []string) {
tracef("sorting command categories (cmd=%[1]q)", cmd.Name)
sort.Sort(cmd.categories.(*commandCategories))

tracef("setting category on mutually exclusive flags (cmd=%[1]q)", cmd.Name)
for _, grp := range cmd.MutuallyExclusiveFlags {
grp.propagateCategory()
}

tracef("setting flag categories (cmd=%[1]q)", cmd.Name)
cmd.flagCategories = newFlagCategoriesFromFlags(cmd.allFlags())

Expand Down Expand Up @@ -324,6 +329,11 @@ func (cmd *Command) setupSubcommand() {
tracef("sorting command categories (cmd=%[1]q)", cmd.Name)
sort.Sort(cmd.categories.(*commandCategories))

tracef("setting category on mutually exclusive flags (cmd=%[1]q)", cmd.Name)
for _, grp := range cmd.MutuallyExclusiveFlags {
grp.propagateCategory()
}

tracef("setting flag categories (cmd=%[1]q)", cmd.Name)
cmd.flagCategories = newFlagCategoriesFromFlags(cmd.allFlags())
}
Expand Down
22 changes: 8 additions & 14 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,38 +525,32 @@ func TestCommand_VisibleFlagCategories(t *testing.T) {
},
},
MutuallyExclusiveFlags: []MutuallyExclusiveFlags{{
Category: "cat2",
Flags: [][]Flag{
{
&BoolFlag{
Name: "mutexStrd",
},
&BoolFlag{
Name: "mutexCat1",
Category: "cat1",
},
&BoolFlag{
Name: "mutexCat2",
Category: "cat2",
&StringFlag{
Name: "mutex",
},
},
},
}},
}

cmd.MutuallyExclusiveFlags[0].propagateCategory()

vfc := cmd.VisibleFlagCategories()
require.Len(t, vfc, 3)

assert.Equal(t, vfc[0].Name(), "", "expected category name to be empty")
assert.Equal(t, vfc[0].Flags()[0].Names(), []string{"mutexStrd"})
assert.Equal(t, vfc[0].Flags()[0].Names(), []string{"strd"})

assert.Equal(t, vfc[1].Name(), "cat1", "expected category name cat1")
require.Len(t, vfc[1].Flags(), 2, "expected flag category to have two flags")
require.Len(t, vfc[1].Flags(), 1, "expected flag category to have one flag")
assert.Equal(t, vfc[1].Flags()[0].Names(), []string{"intd", "altd1", "altd2"})
assert.Equal(t, vfc[1].Flags()[1].Names(), []string{"mutexCat1"})

assert.Equal(t, vfc[2].Name(), "cat2", "expected category name cat2")
require.Len(t, vfc[2].Flags(), 1, "expected flag category to have one flag")
assert.Equal(t, vfc[2].Flags()[0].Names(), []string{"mutexCat2"})
assert.Equal(t, vfc[2].Flags()[0].Names(), []string{"mutex"})
}

func TestCommand_RunSubcommandWithDefault(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ type VisibleFlag interface {
type CategorizableFlag interface {
// Returns the category of the flag
GetCategory() string

// Sets the category of the flag
SetCategory(string)
}

// PersistentFlag is an interface to enable detection of flags which are persistent
Expand Down
4 changes: 4 additions & 0 deletions flag_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ func (f *FlagBase[T, C, V]) GetCategory() string {
return f.Category
}

func (f *FlagBase[T, C, V]) SetCategory(c string) {
f.Category = c
}

// GetUsage returns the usage string for the flag
func (f *FlagBase[T, C, V]) GetUsage() string {
return f.Usage
Expand Down
13 changes: 13 additions & 0 deletions flag_mutex.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ type MutuallyExclusiveFlags struct {

// whether this group is required
Required bool

// Category to apply to all flags within group
Category string
}

func (grp MutuallyExclusiveFlags) check(cmd *Command) error {
Expand Down Expand Up @@ -41,3 +44,13 @@ func (grp MutuallyExclusiveFlags) check(cmd *Command) error {
}
return nil
}

func (grp MutuallyExclusiveFlags) propagateCategory() {
for _, grpf := range grp.Flags {
for _, f := range grpf {
if cf, ok := f.(CategorizableFlag); ok {
cf.SetCategory(grp.Category)
}
}
}
}
30 changes: 14 additions & 16 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1528,20 +1528,22 @@ func TestCategorizedHelp(t *testing.T) {
},
MutuallyExclusiveFlags: []MutuallyExclusiveFlags{
{
Category: "cat1",
Flags: [][]Flag{
{
&StringFlag{
Name: "mutexstd",
},
&IntFlag{
Name: "mutexcat1",
Category: "cat1",
Name: "m1",
Category: "overridden",
},
},
},
},
{
Flags: [][]Flag{
{
&IntFlag{
Name: "mutexcat2",
Category: "cat2",
&StringFlag{
Name: "m2",
Category: "ignored",
},
},
},
Expand Down Expand Up @@ -1575,18 +1577,14 @@ COMMANDS:
for one command
GLOBAL OPTIONS:
--help, -h show help (default: false)
--mutexstd value
--strd value
--help, -h show help (default: false)
--m2 value
--strd value
cat1
--intd value, --altd1 value, --altd2 value (default: 0)
--mutexcat1 value (default: 0)
cat2
--mutexcat2 value (default: 0)
--m1 value
`, output.String())
}

0 comments on commit d754265

Please sign in to comment.