From a5ec63b31f602fd9b0979d18a4df4780e103c217 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 8 Nov 2022 10:49:42 -0500 Subject: [PATCH] More porting fixes and un-lost changes --- app.go | 4 +++- app_test.go | 33 ++++++++++++++++++++++++++++----- category.go | 4 +++- command.go | 9 +-------- command_test.go | 22 +++++++++------------- godoc-current.txt | 6 ++++-- help.go | 6 +----- help_test.go | 12 ++++++++---- template.go | 10 ++++++---- 9 files changed, 63 insertions(+), 43 deletions(-) diff --git a/app.go b/app.go index 2f1557a..e7f79c5 100644 --- a/app.go +++ b/app.go @@ -251,7 +251,9 @@ func (a *App) Setup() { a.flagCategories = newFlagCategories() for _, fl := range a.Flags { if cf, ok := fl.(CategorizableFlag); ok { - a.flagCategories.AddFlag(cf.GetCategory(), cf) + if cf.GetCategory() != "" { + a.flagCategories.AddFlag(cf.GetCategory(), cf) + } } } diff --git a/app_test.go b/app_test.go index 2e21c47..b85848f 100644 --- a/app_test.go +++ b/app_test.go @@ -144,8 +144,8 @@ func ExampleApp_Run_appHelp() { // help, h Shows a list of commands or help for one command // // GLOBAL OPTIONS: - // --help, -h show help (default: false) // --name value a name to say (default: "bob") + // --help, -h show help (default: false) // --version, -v print the version (default: false) } @@ -177,7 +177,7 @@ func ExampleApp_Run_commandHelp() { // greet describeit - use it to see a description // // USAGE: - // greet describeit [arguments...] + // greet describeit [command options] [arguments...] // // DESCRIPTION: // This is how we describe describeit the function @@ -2304,10 +2304,33 @@ func TestApp_VisibleCategories(t *testing.T) { } func TestApp_VisibleFlagCategories(t *testing.T) { - app := &App{} + 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) != 0 { - t.Errorf("unexpected visible flag categories %+v", vfc) + 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()) } } diff --git a/category.go b/category.go index 8bf325e..7aca0c7 100644 --- a/category.go +++ b/category.go @@ -102,7 +102,9 @@ func newFlagCategoriesFromFlags(fs []Flag) FlagCategories { fc := newFlagCategories() for _, fl := range fs { if cf, ok := fl.(CategorizableFlag); ok { - fc.AddFlag(cf.GetCategory(), cf) + if cf.GetCategory() != "" { + fc.AddFlag(cf.GetCategory(), cf) + } } } diff --git a/command.go b/command.go index f02fe2f..c5939d4 100644 --- a/command.go +++ b/command.go @@ -376,15 +376,8 @@ func (c *Command) VisibleCommands() []*Command { // VisibleFlagCategories returns a slice containing all the visible flag categories with the flags they contain func (c *Command) VisibleFlagCategories() []VisibleFlagCategory { if c.flagCategories == nil { - c.flagCategories = newFlagCategories() + c.flagCategories = newFlagCategoriesFromFlags(c.Flags) } - - 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 6c2ecde..478c325 100644 --- a/command_test.go +++ b/command_test.go @@ -470,21 +470,17 @@ func TestCommand_VisibleFlagCategories(t *testing.T) { } vfc := c.VisibleFlagCategories() - if len(vfc) < 2 { - t.Fatalf("unexpected visible flag categories %+#v", vfc) + 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()) } - 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] - + fl := vfc[0].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 a6cb6f0..9abda4b 100644 --- a/godoc-current.txt +++ b/godoc-current.txt @@ -73,7 +73,8 @@ DESCRIPTION: OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}} -OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}` +OPTIONS:{{template "visibleFlagTemplate" .}}{{end}} +` CommandHelpTemplate is the text template for the command help topic. cli.go uses text/template to render templates. You can render custom help text by setting this variable. @@ -144,7 +145,8 @@ COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategori OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}} -OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}` +OPTIONS:{{template "visibleFlagTemplate" .}}{{end}} +` SubcommandHelpTemplate is the text template for the subcommand help topic. cli.go uses text/template to render templates. You can render custom help text by setting this variable. diff --git a/help.go b/help.go index a7cc96a..2ccd3b7 100644 --- a/help.go +++ b/help.go @@ -250,11 +250,7 @@ func ShowCommandHelp(ctx *Context, command string) error { c.Subcommands = append(c.Subcommands, helpCommandDontUse) } if !ctx.App.HideHelp && HelpFlag != nil { - if c.flagCategories == nil { - c.flagCategories = newFlagCategoriesFromFlags([]Flag{HelpFlag}) - } else { - c.flagCategories.AddFlag("", HelpFlag) - } + c.appendFlag(HelpFlag) } templ := c.CustomHelpTemplate if templ == "" { diff --git a/help_test.go b/help_test.go index e5f37e3..b1b456c 100644 --- a/help_test.go +++ b/help_test.go @@ -1366,7 +1366,8 @@ DESCRIPTION: case OPTIONS: - --help, -h show help (default: false) + --help, -h show help + (default: false) ` if output.String() != expected { @@ -1435,7 +1436,8 @@ USAGE: even more OPTIONS: - --help, -h show help (default: false) + --help, -h show help + (default: false) ` if output.String() != expected { @@ -1510,8 +1512,10 @@ USAGE: even more OPTIONS: - --help, -h show help (default: false) - --test-f value my test usage + --test-f value my test + usage + --help, -h show help + (default: false) ` if output.String() != expected { diff --git a/template.go b/template.go index 23390c7..b565ba6 100644 --- a/template.go +++ b/template.go @@ -18,8 +18,8 @@ var visibleFlagCategoryTemplate = `{{range .VisibleFlagCategories}} {{else}}{{$e}} {{end}}{{end}}{{end}}` -var visibleFlagTemplate = `{{range $index, $option := .VisibleFlags}}{{if $index}}{{end}} - {{wrap $option.String 6}}{{end}}` +var visibleFlagTemplate = `{{range $i, $e := .VisibleFlags}} + {{wrap $e.String 6}}{{end}}` var versionTemplate = `{{if .Version}}{{if not .HideVersion}} @@ -73,7 +73,8 @@ DESCRIPTION: OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}} -OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}` +OPTIONS:{{template "visibleFlagTemplate" .}}{{end}} +` // SubcommandHelpTemplate is the text template for the subcommand help topic. // cli.go uses text/template to render templates. You can @@ -91,7 +92,8 @@ COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategori OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}} -OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}` +OPTIONS:{{template "visibleFlagTemplate" .}}{{end}} +` var MarkdownDocTemplate = `{{if gt .SectionNum 0}}% {{ .App.Name }} {{ .SectionNum }}