Adjust expectations around categorized flags

This commit is contained in:
Dan Buch 2022-11-08 08:17:56 -05:00
parent 75f2d1a83a
commit d294a88a47
Signed by: meatballhat
GPG Key ID: A12F782281063434
3 changed files with 31 additions and 25 deletions

View File

@ -377,12 +377,14 @@ func (c *Command) VisibleCommands() []*Command {
func (c *Command) VisibleFlagCategories() []VisibleFlagCategory { func (c *Command) VisibleFlagCategories() []VisibleFlagCategory {
if c.flagCategories == nil { if c.flagCategories == nil {
c.flagCategories = newFlagCategories() 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() return c.flagCategories.VisibleCategories()
} }

View File

@ -470,17 +470,21 @@ func TestCommand_VisibleFlagCategories(t *testing.T) {
} }
vfc := c.VisibleFlagCategories() vfc := c.VisibleFlagCategories()
if len(vfc) != 1 { if len(vfc) < 2 {
t.Fatalf("unexpected visible flag categories %+v", vfc) 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] intdCatFlag := vfc[1]
if intdCatFlag.Name() != "cat1" {
t.Errorf("expected category name cat1 got %q", intdCatFlag.Name())
}
if len(intdCatFlag.Flags()) != 1 {
t.Fatalf("expected flag category to have just one flag got %+v", intdCatFlag.Flags())
}
fl := intdCatFlag.Flags()[0]
if !reflect.DeepEqual(fl.Names(), []string{"intd", "altd1", "altd2"}) { if !reflect.DeepEqual(fl.Names(), []string{"intd", "altd1", "altd2"}) {
t.Errorf("unexpected flag %+v", fl.Names()) t.Errorf("unexpected flag %+v", fl.Names())
} }

View File

@ -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: the most simple cli application can be written as follows:
func main() { 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 Of course this application does not do much, so let's make this an actual
application: application:
func main() { func main() {
app := &cli.App{ app := &cli.App{
Name: "greet", Name: "greet",
Usage: "say a greeting", Usage: "say a greeting",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
fmt.Println("Greetings") fmt.Println("Greetings")
return nil return nil
}, },
} }
app.Run(os.Args) app.Run(os.Args)
} }
VARIABLES VARIABLES