From 1ada1a1c06763e55b2ab5a279e5f442914fd90e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E5=B9=BF=E5=AE=87?= Date: Sat, 2 Jul 2022 18:12:38 +0800 Subject: [PATCH 1/6] fix: stop automatic sorting for --help --- category.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/category.go b/category.go index 8bf325e..a5271a3 100644 --- a/category.go +++ b/category.go @@ -100,6 +100,9 @@ func newFlagCategories() FlagCategories { func newFlagCategoriesFromFlags(fs []Flag) FlagCategories { fc := newFlagCategories() + if !enableCategory(fs) { + return fc + } for _, fl := range fs { if cf, ok := fl.(CategorizableFlag); ok { fc.AddFlag(cf.GetCategory(), cf) @@ -109,6 +112,19 @@ func newFlagCategoriesFromFlags(fs []Flag) FlagCategories { return fc } +func enableCategory(fs []Flag) bool { + for _, fl := range fs { + if cf, ok := fl.(CategorizableFlag); ok { + // When a category is specified, it is assumed that the entire command opens the category + if len(cf.GetCategory()) != 0 { + return true + } + } + } + + return false +} + func (f *defaultFlagCategories) AddFlag(category string, fl Flag) { if _, ok := f.m[category]; !ok { f.m[category] = &defaultVisibleFlagCategory{name: category, m: map[string]Flag{}} From 02613e50bee21550239c39bf880333358f1e0d8d Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Thu, 6 Oct 2022 20:53:05 -0500 Subject: [PATCH 2/6] Fix tests --- app_test.go | 2 +- category.go | 20 +++----------------- command.go | 4 +++- help.go | 6 +----- help_test.go | 12 ++++++++---- template.go | 10 ++++++---- 6 files changed, 22 insertions(+), 32 deletions(-) diff --git a/app_test.go b/app_test.go index 6200718..a4ce72c 100644 --- a/app_test.go +++ b/app_test.go @@ -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 diff --git a/category.go b/category.go index a5271a3..7aca0c7 100644 --- a/category.go +++ b/category.go @@ -100,29 +100,15 @@ func newFlagCategories() FlagCategories { func newFlagCategoriesFromFlags(fs []Flag) FlagCategories { fc := newFlagCategories() - if !enableCategory(fs) { - return fc - } - for _, fl := range fs { - if cf, ok := fl.(CategorizableFlag); ok { - fc.AddFlag(cf.GetCategory(), cf) - } - } - - return fc -} - -func enableCategory(fs []Flag) bool { for _, fl := range fs { if cf, ok := fl.(CategorizableFlag); ok { - // When a category is specified, it is assumed that the entire command opens the category - if len(cf.GetCategory()) != 0 { - return true + if cf.GetCategory() != "" { + fc.AddFlag(cf.GetCategory(), cf) } } } - return false + return fc } func (f *defaultFlagCategories) AddFlag(category string, fl Flag) { diff --git a/command.go b/command.go index e66985e..0707044 100644 --- a/command.go +++ b/command.go @@ -312,7 +312,9 @@ func (c *Command) VisibleFlagCategories() []VisibleFlagCategory { c.flagCategories = newFlagCategories() for _, fl := range c.Flags { if cf, ok := fl.(CategorizableFlag); ok { - c.flagCategories.AddFlag(cf.GetCategory(), cf) + if cf.GetCategory() != "" { + c.flagCategories.AddFlag(cf.GetCategory(), cf) + } } } } diff --git a/help.go b/help.go index d78444a..2930165 100644 --- a/help.go +++ b/help.go @@ -242,11 +242,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 37410a5..72277cc 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 1133b89..5c2a62e 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 }} From e62a08711796973ae6db595224435d69cd5a3fdc Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Thu, 6 Oct 2022 20:55:28 -0500 Subject: [PATCH 3/6] Run make v2approve --- godoc-current.txt | 6 ++++-- testdata/godoc-v2.x.txt | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/godoc-current.txt b/godoc-current.txt index aae4e0b..b6e3d43 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/testdata/godoc-v2.x.txt b/testdata/godoc-v2.x.txt index aae4e0b..b6e3d43 100644 --- a/testdata/godoc-v2.x.txt +++ b/testdata/godoc-v2.x.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. From 8227be1fe9df91e3d69e17514df21b8e609d6cd0 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Fri, 7 Oct 2022 11:06:34 -0500 Subject: [PATCH 4/6] Add tests for command.VisibleFlagCategory --- app.go | 4 +++- app_test.go | 31 +++++++++++++++++++++++++++---- command_test.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/app.go b/app.go index 9f72f1b..0ae3f52 100644 --- a/app.go +++ b/app.go @@ -229,7 +229,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 a4ce72c..7e13c90 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) } @@ -2242,10 +2242,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/command_test.go b/command_test.go index a953e94..ca90430 100644 --- a/command_test.go +++ b/command_test.go @@ -6,6 +6,7 @@ import ( "flag" "fmt" "io/ioutil" + "reflect" "strings" "testing" ) @@ -449,3 +450,37 @@ func TestCommand_VisibleSubcCommands(t *testing.T) { expect(t, c.VisibleCommands(), []*Command{subc1, subc3}) } + +func TestCommand_VisibleFlagCategories(t *testing.T) { + + c := &Command{ + Name: "bar", + Usage: "this is for testing", + Flags: []Flag{ + &StringFlag{ + Name: "strd", // no category set + }, + &Int64Flag{ + Name: "intd", + Aliases: []string{"altd1", "altd2"}, + Category: "cat1", + }, + }, + } + + vfc := c.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()) + } +} From 81f9145708bf1c5409d15f6308037effbe01cdf9 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Fri, 7 Oct 2022 11:17:09 -0500 Subject: [PATCH 5/6] Refactor code --- command.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/command.go b/command.go index 0707044..037ebc5 100644 --- a/command.go +++ b/command.go @@ -309,14 +309,7 @@ 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() - for _, fl := range c.Flags { - if cf, ok := fl.(CategorizableFlag); ok { - if cf.GetCategory() != "" { - c.flagCategories.AddFlag(cf.GetCategory(), cf) - } - } - } + c.flagCategories = newFlagCategoriesFromFlags(c.Flags) } return c.flagCategories.VisibleCategories() } From bffaf3b78c636c263c788271655644aedc312ed4 Mon Sep 17 00:00:00 2001 From: Naveen Gogineni Date: Sun, 9 Oct 2022 16:07:55 -0500 Subject: [PATCH 6/6] Update flags.md for docs change --- docs/v2/examples/flags.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/v2/examples/flags.md b/docs/v2/examples/flags.md index dbb41ea..44c643a 100644 --- a/docs/v2/examples/flags.md +++ b/docs/v2/examples/flags.md @@ -239,7 +239,7 @@ For example this: ```go package main