From d294a88a475662fe366f7968cb8605c418327ba5 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 8 Nov 2022 08:17:56 -0500 Subject: [PATCH] Adjust expectations around categorized flags --- command.go | 10 ++++++---- command_test.go | 18 +++++++++++------- godoc-current.txt | 26 +++++++++++++------------- 3 files changed, 30 insertions(+), 24 deletions(-) diff --git a/command.go b/command.go index 8ccf486..f02fe2f 100644 --- a/command.go +++ b/command.go @@ -377,12 +377,14 @@ func (c *Command) VisibleCommands() []*Command { func (c *Command) VisibleFlagCategories() []VisibleFlagCategory { if c.flagCategories == nil { c.flagCategories = newFlagCategories() - for _, fl := range c.Flags { - if cf, ok := fl.(CategorizableFlag); ok { - c.flagCategories.AddFlag(cf.GetCategory(), fl) - } + } + + for _, fl := range c.Flags { + if cf, ok := fl.(CategorizableFlag); ok { + c.flagCategories.AddFlag(cf.GetCategory(), fl) } } + return c.flagCategories.VisibleCategories() } diff --git a/command_test.go b/command_test.go index 478c325..6c2ecde 100644 --- a/command_test.go +++ b/command_test.go @@ -470,17 +470,21 @@ func TestCommand_VisibleFlagCategories(t *testing.T) { } vfc := c.VisibleFlagCategories() - if len(vfc) != 1 { - t.Fatalf("unexpected visible flag categories %+v", vfc) + if len(vfc) < 2 { + t.Fatalf("unexpected visible flag categories %+#v", vfc) } - if vfc[0].Name() != "cat1" { - t.Errorf("expected category name cat1 got %s", vfc[0].Name()) + + intdCatFlag := vfc[1] + + if intdCatFlag.Name() != "cat1" { + t.Errorf("expected category name cat1 got %q", intdCatFlag.Name()) } - if len(vfc[0].Flags()) != 1 { - t.Fatalf("expected flag category to have just one flag got %+v", vfc[0].Flags()) + if len(intdCatFlag.Flags()) != 1 { + t.Fatalf("expected flag category to have just one flag got %+v", intdCatFlag.Flags()) } - fl := vfc[0].Flags()[0] + fl := intdCatFlag.Flags()[0] + if !reflect.DeepEqual(fl.Names(), []string{"intd", "altd1", "altd2"}) { t.Errorf("unexpected flag %+v", fl.Names()) } diff --git a/godoc-current.txt b/godoc-current.txt index a6ea145..a6cb6f0 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -5,24 +5,24 @@ line Go applications. cli is designed to be easy to understand and write, the most simple cli application can be written as follows: func main() { - (&cli.App{}).Run(os.Args) + (&cli.App{}).Run(os.Args) } Of course this application does not do much, so let's make this an actual application: - func main() { - app := &cli.App{ - Name: "greet", - Usage: "say a greeting", - Action: func(c *cli.Context) error { - fmt.Println("Greetings") - return nil - }, - } - - app.Run(os.Args) - } + func main() { + app := &cli.App{ + Name: "greet", + Usage: "say a greeting", + Action: func(c *cli.Context) error { + fmt.Println("Greetings") + return nil + }, + } + + app.Run(os.Args) + } VARIABLES