Skip to content

Commit

Permalink
Merge pull request #1673 from skelouse/v2-maint
Browse files Browse the repository at this point in the history
Show non categorized flags with categorized on help
  • Loading branch information
dearchap committed Jan 29, 2023
2 parents 9013351 + 31d7502 commit 2389448
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 45 deletions.
9 changes: 1 addition & 8 deletions app.go
Expand Up @@ -261,14 +261,7 @@ func (a *App) Setup() {
}
sort.Sort(a.categories.(*commandCategories))

a.flagCategories = newFlagCategories()
for _, fl := range a.Flags {
if cf, ok := fl.(CategorizableFlag); ok {
if cf.GetCategory() != "" {
a.flagCategories.AddFlag(cf.GetCategory(), cf)
}
}
}
a.flagCategories = newFlagCategoriesFromFlags(a.Flags)

if a.Metadata == nil {
a.Metadata = make(map[string]interface{})
Expand Down
31 changes: 0 additions & 31 deletions app_test.go
Expand Up @@ -2303,37 +2303,6 @@ func TestApp_VisibleCategories(t *testing.T) {
expect(t, []CommandCategory{}, app.VisibleCategories())
}

func TestApp_VisibleFlagCategories(t *testing.T) {
app := &App{
Flags: []Flag{
&StringFlag{
Name: "strd", // no category set
},
&Int64Flag{
Name: "intd",
Aliases: []string{"altd1", "altd2"},
Category: "cat1",
},
},
}
app.Setup()
vfc := app.VisibleFlagCategories()
if len(vfc) != 1 {
t.Fatalf("unexpected visible flag categories %+v", vfc)
}
if vfc[0].Name() != "cat1" {
t.Errorf("expected category name cat1 got %s", vfc[0].Name())
}
if len(vfc[0].Flags()) != 1 {
t.Fatalf("expected flag category to have just one flag got %+v", vfc[0].Flags())
}

fl := vfc[0].Flags()[0]
if !reflect.DeepEqual(fl.Names(), []string{"intd", "altd1", "altd2"}) {
t.Errorf("unexpected flag %+v", fl.Names())
}
}

func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
app := &App{
Action: func(c *Context) error { return nil },
Expand Down
17 changes: 15 additions & 2 deletions category.go
Expand Up @@ -100,10 +100,23 @@ 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(), cf)
if cat := cf.GetCategory(); cat != "" {
fc.AddFlag(cat, cf)
categorized = true
}
}
}

if categorized == true {
for _, fl := range fs {
if cf, ok := fl.(CategorizableFlag); ok {
if cf.GetCategory() == "" {
fc.AddFlag("", fl)
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions command_test.go
Expand Up @@ -470,17 +470,17 @@ func TestCommand_VisibleFlagCategories(t *testing.T) {
}

vfc := c.VisibleFlagCategories()
if len(vfc) != 1 {
if len(vfc) != 2 {
t.Fatalf("unexpected visible flag categories %+v", vfc)
}
if vfc[0].Name() != "cat1" {
if vfc[1].Name() != "cat1" {
t.Errorf("expected category name cat1 got %s", vfc[0].Name())
}
if len(vfc[0].Flags()) != 1 {
if len(vfc[1].Flags()) != 1 {
t.Fatalf("expected flag category to have just one flag got %+v", vfc[0].Flags())
}

fl := vfc[0].Flags()[0]
fl := vfc[1].Flags()[0]
if !reflect.DeepEqual(fl.Names(), []string{"intd", "altd1", "altd2"}) {
t.Errorf("unexpected flag %+v", fl.Names())
}
Expand Down
65 changes: 65 additions & 0 deletions help_test.go
Expand Up @@ -1520,3 +1520,68 @@ OPTIONS:
output.String(), expected)
}
}

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

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

c := NewContext(app, nil, nil)
app.Setup()

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

HelpPrinterCustom(w, templ, data, funcMap)
}

_ = ShowAppHelp(c)

expected := `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
--strd value
cat1
--intd value, --altd1 value, --altd2 value (default: 0)
`
if output.String() != expected {
t.Errorf("Unexpected wrapping, got:\n%s\nexpected:\n%s",
output.String(), expected)
}
}

0 comments on commit 2389448

Please sign in to comment.