Skip to content

Commit

Permalink
Merge pull request #1854 from jetexe/backport-no-categoried-fix
Browse files Browse the repository at this point in the history
Backport Show non categorized flags with categorized on help
  • Loading branch information
dearchap authored Dec 27, 2023
2 parents eeebafc + 64b5735 commit 7a9aea9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
18 changes: 16 additions & 2 deletions category.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,24 @@ func newFlagCategories() FlagCategories {

func newFlagCategoriesFromFlags(fs []Flag) FlagCategories {
fc := newFlagCategories()

var categorized bool

for _, fl := range fs {
if cf, ok := fl.(CategorizableFlag); ok {
if cf.GetCategory() != "" {
fc.AddFlag(cf.GetCategory(), fl)
if cat := cf.GetCategory(); cat != "" {
fc.AddFlag(cat, fl)
categorized = true
}
}
}

if categorized {
for _, fl := range fs {
if cf, ok := fl.(CategorizableFlag); ok {
if cf.GetCategory() == "" {
fc.AddFlag("", fl)
}
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,12 +526,15 @@ func TestCommand_VisibleFlagCategories(t *testing.T) {
}

vfc := cmd.VisibleFlagCategories()
require.NotEmpty(t, vfc)
assert.Equal(t, vfc[0].Name(), "cat1", "expected category name cat1")
require.Len(t, vfc, 2)

require.Len(t, vfc[0].Flags(), 1, "expected flag category to have just one flag")
assert.Equal(t, vfc[0].Name(), "", "expected category name to be empty")

fl := vfc[0].Flags()[0]
assert.Equal(t, vfc[1].Name(), "cat1", "expected category name cat1")

require.Len(t, vfc[1].Flags(), 1, "expected flag category to have just one flag")

fl := vfc[1].Flags()[0]
assert.Equal(t, fl.Names(), []string{"intd", "altd1", "altd2"})
}

Expand Down
59 changes: 59 additions & 0 deletions help_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1482,3 +1482,62 @@ OPTIONS:
output.String(),
)
}

func TestCategorizedHelp(t *testing.T) {
// Reset HelpPrinter after this test.
defer func(old helpPrinter) {
HelpPrinter = old
}(HelpPrinter)

output := new(bytes.Buffer)
cmd := &Command{
Name: "cli.test",
Writer: output,
Action: func(context.Context, *Command) error { return nil },
Flags: []Flag{
&StringFlag{
Name: "strd", // no category set
},
&IntFlag{
Name: "intd",
Aliases: []string{"altd1", "altd2"},
Category: "cat1",
},
},
}

HelpPrinter = func(w io.Writer, templ string, data interface{}) {
funcMap := map[string]interface{}{
"wrapAt": func() int {
return 30
},
}

HelpPrinterCustom(w, templ, data, funcMap)
}

r := require.New(t)
r.NoError(cmd.Run(buildTestContext(t), []string{"cli.test", "help"}))

r.Equal(`NAME:
cli.test - A new cli
application
USAGE:
cli.test [global options] [command [command options]] [arguments...]
COMMANDS:
help, h Shows a list of
commands or help
for one command
GLOBAL OPTIONS:
--help, -h show help (default: false)
--strd value
cat1
--intd value, --altd1 value, --altd2 value (default: 0)
`, output.String())
}

0 comments on commit 7a9aea9

Please sign in to comment.