fix: stop automatic sorting for --help

This commit is contained in:
范广宇 2022-07-02 18:12:38 +08:00 committed by Naveen Gogineni
parent f64acc4404
commit 1ada1a1c06

View File

@ -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)
@ -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{}}