Skip to content

Commit f33a9e0

Browse files
authored
Merge pull request #1869 from dearchap/issue_1860_v3
Fix:(issue_1860) Remove hidden flags from flag categories
2 parents b3e47b0 + 0f95e2a commit f33a9e0

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

category.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ func newFlagCategoriesFromFlags(fs []Flag) FlagCategories {
105105

106106
for _, fl := range fs {
107107
if cf, ok := fl.(CategorizableFlag); ok {
108-
if cat := cf.GetCategory(); cat != "" {
108+
visible := false
109+
if vf, ok := fl.(VisibleFlag); ok {
110+
visible = vf.IsVisible()
111+
}
112+
if cat := cf.GetCategory(); cat != "" && visible {
109113
fc.AddFlag(cat, fl)
110114
categorized = true
111115
}
@@ -115,7 +119,11 @@ func newFlagCategoriesFromFlags(fs []Flag) FlagCategories {
115119
if categorized {
116120
for _, fl := range fs {
117121
if cf, ok := fl.(CategorizableFlag); ok {
118-
if cf.GetCategory() == "" {
122+
visible := false
123+
if vf, ok := fl.(VisibleFlag); ok {
124+
visible = vf.IsVisible()
125+
}
126+
if cf.GetCategory() == "" && visible {
119127
fc.AddFlag("", fl)
120128
}
121129
}

command_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,20 @@ func TestCommand_VisibleFlagCategories(t *testing.T) {
518518
&StringFlag{
519519
Name: "strd", // no category set
520520
},
521+
&StringFlag{
522+
Name: "strd1", // no category set and also hidden
523+
Hidden: true,
524+
},
521525
&IntFlag{
522526
Name: "intd",
523527
Aliases: []string{"altd1", "altd2"},
524528
Category: "cat1",
525529
},
530+
&StringFlag{
531+
Name: "sfd",
532+
Category: "cat2", // category set and hidden
533+
Hidden: true,
534+
},
526535
},
527536
MutuallyExclusiveFlags: []MutuallyExclusiveFlags{{
528537
Category: "cat2",

0 commit comments

Comments
 (0)