Skip to content

Commit

Permalink
fix: stop automatic sorting for --help
Browse files Browse the repository at this point in the history
  • Loading branch information
FGYFFFF committed Jul 2, 2022
1 parent d29120f commit e27277f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions category.go
Expand Up @@ -100,6 +100,9 @@ func newFlagCategories() FlagCategories {

func newFlagCategoriesFromFlags(fs []Flag) FlagCategories {
fc := newFlagCategories()
if !enableCategory(fs) {
return fc
}
for _, fl := range fs {
if cf, ok := fl.(CategorizableFlag); ok {
fc.AddFlag(cf.GetCategory(), cf)
Expand All @@ -109,6 +112,19 @@ func newFlagCategoriesFromFlags(fs []Flag) FlagCategories {
return fc
}

func enableCategory(fs []Flag) bool {
for _, fl := range fs {
if cf, ok := fl.(CategorizableFlag); ok {
// When a category is specified, it is assumed that the entire command opens the category
if len(cf.GetCategory()) != 0 {
return true
}
}
}

return false
}

func (f *defaultFlagCategories) AddFlag(category string, fl Flag) {
if _, ok := f.m[category]; !ok {
f.m[category] = &defaultVisibleFlagCategory{name: category, m: map[string]Flag{}}
Expand Down

0 comments on commit e27277f

Please sign in to comment.