Some changes per feedback in #1368

This commit is contained in:
Dan Buch 2022-05-06 22:23:17 -04:00
parent 156eaafb22
commit 16d5d5a3df
Signed by: meatballhat
GPG Key ID: A12F782281063434
3 changed files with 14 additions and 10 deletions

11
app.go
View File

@ -184,14 +184,7 @@ func (a *App) Setup() {
c.HelpName = fmt.Sprintf("%s %s", a.HelpName, c.Name)
}
fc := newFlagCategories()
for _, fl := range c.Flags {
if cf, ok := fl.(CategorizableFlag); ok {
fc.AddFlag(cf.GetCategory(), cf)
}
}
c.flagCategories = fc
c.flagCategories = newFlagCategoriesFromFlags(c.Flags)
newCommands = append(newCommands, c)
}
a.Commands = newCommands
@ -502,7 +495,7 @@ func (a *App) VisibleCommands() []*Command {
// VisibleFlagCategories returns a slice containing all the categories with the flags they contain
func (a *App) VisibleFlagCategories() []VisibleFlagCategory {
if a.flagCategories == nil {
a.flagCategories = newFlagCategories()
return []VisibleFlagCategory{}
}
return a.flagCategories.VisibleCategories()
}

View File

@ -98,6 +98,17 @@ func newFlagCategories() FlagCategories {
}
}
func newFlagCategoriesFromFlags(fs []Flag) FlagCategories {
fc := newFlagCategories()
for _, fl := range fs {
if cf, ok := fl.(CategorizableFlag); ok {
fc.AddFlag(cf.GetCategory(), cf)
}
}
return fc
}
func (f *defaultFlagCategories) AddFlag(category string, fl Flag) {
if _, ok := f.m[category]; !ok {
f.m[category] = &defaultVisibleFlagCategory{name: category, m: map[string]Flag{}}

View File

@ -284,7 +284,7 @@ func (c *Command) startApp(ctx *Context) error {
// VisibleFlagCategories returns a slice containing all the visible flag categories with the flags they contain
func (c *Command) VisibleFlagCategories() []VisibleFlagCategory {
if c.flagCategories == nil {
c.flagCategories = newFlagCategories()
return []VisibleFlagCategory{}
}
return c.flagCategories.VisibleCategories()
}