Merge pull request #1430 from FGYFFFF/fix/stop_auto_sort_for_help
fix: stop automatic sorting for --help
This commit is contained in:
commit
f37b9d9b60
4
app.go
4
app.go
@ -229,7 +229,9 @@ func (a *App) Setup() {
|
|||||||
a.flagCategories = newFlagCategories()
|
a.flagCategories = newFlagCategories()
|
||||||
for _, fl := range a.Flags {
|
for _, fl := range a.Flags {
|
||||||
if cf, ok := fl.(CategorizableFlag); ok {
|
if cf, ok := fl.(CategorizableFlag); ok {
|
||||||
a.flagCategories.AddFlag(cf.GetCategory(), cf)
|
if cf.GetCategory() != "" {
|
||||||
|
a.flagCategories.AddFlag(cf.GetCategory(), cf)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
33
app_test.go
33
app_test.go
@ -144,8 +144,8 @@ func ExampleApp_Run_appHelp() {
|
|||||||
// help, h Shows a list of commands or help for one command
|
// help, h Shows a list of commands or help for one command
|
||||||
//
|
//
|
||||||
// GLOBAL OPTIONS:
|
// GLOBAL OPTIONS:
|
||||||
// --help, -h show help (default: false)
|
|
||||||
// --name value a name to say (default: "bob")
|
// --name value a name to say (default: "bob")
|
||||||
|
// --help, -h show help (default: false)
|
||||||
// --version, -v print the version (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
|
// greet describeit - use it to see a description
|
||||||
//
|
//
|
||||||
// USAGE:
|
// USAGE:
|
||||||
// greet describeit [arguments...]
|
// greet describeit [command options] [arguments...]
|
||||||
//
|
//
|
||||||
// DESCRIPTION:
|
// DESCRIPTION:
|
||||||
// This is how we describe describeit the function
|
// This is how we describe describeit the function
|
||||||
@ -2242,10 +2242,33 @@ func TestApp_VisibleCategories(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestApp_VisibleFlagCategories(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()
|
vfc := app.VisibleFlagCategories()
|
||||||
if len(vfc) != 0 {
|
if len(vfc) != 1 {
|
||||||
t.Errorf("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]
|
||||||
|
if !reflect.DeepEqual(fl.Names(), []string{"intd", "altd1", "altd2"}) {
|
||||||
|
t.Errorf("unexpected flag %+v", fl.Names())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,7 +102,9 @@ func newFlagCategoriesFromFlags(fs []Flag) FlagCategories {
|
|||||||
fc := newFlagCategories()
|
fc := newFlagCategories()
|
||||||
for _, fl := range fs {
|
for _, fl := range fs {
|
||||||
if cf, ok := fl.(CategorizableFlag); ok {
|
if cf, ok := fl.(CategorizableFlag); ok {
|
||||||
fc.AddFlag(cf.GetCategory(), cf)
|
if cf.GetCategory() != "" {
|
||||||
|
fc.AddFlag(cf.GetCategory(), cf)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,12 +309,7 @@ func (c *Command) VisibleCommands() []*Command {
|
|||||||
// VisibleFlagCategories returns a slice containing all the visible flag categories with the flags they contain
|
// VisibleFlagCategories returns a slice containing all the visible flag categories with the flags they contain
|
||||||
func (c *Command) VisibleFlagCategories() []VisibleFlagCategory {
|
func (c *Command) VisibleFlagCategories() []VisibleFlagCategory {
|
||||||
if c.flagCategories == nil {
|
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(), cf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return c.flagCategories.VisibleCategories()
|
return c.flagCategories.VisibleCategories()
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -449,3 +450,37 @@ func TestCommand_VisibleSubcCommands(t *testing.T) {
|
|||||||
|
|
||||||
expect(t, c.VisibleCommands(), []*Command{subc1, subc3})
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -239,7 +239,7 @@ For example this:
|
|||||||
|
|
||||||
<!-- {
|
<!-- {
|
||||||
"args": ["--help"],
|
"args": ["--help"],
|
||||||
"output": ".*Load configuration from FILE\n.*\n.*Language for the greeting.*"
|
"output": ".*Load configuration from FILE\n.*Language for the greeting.*"
|
||||||
} -->
|
} -->
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
@ -73,7 +73,8 @@ DESCRIPTION:
|
|||||||
|
|
||||||
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
|
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
|
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
|
uses text/template to render templates. You can render custom help text by
|
||||||
setting this variable.
|
setting this variable.
|
||||||
@ -144,7 +145,8 @@ COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategori
|
|||||||
|
|
||||||
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
|
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
|
||||||
|
|
||||||
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}`
|
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
|
||||||
|
`
|
||||||
SubcommandHelpTemplate is the text template for the subcommand help topic.
|
SubcommandHelpTemplate is the text template for the subcommand help topic.
|
||||||
cli.go uses text/template to render templates. You can render custom help
|
cli.go uses text/template to render templates. You can render custom help
|
||||||
text by setting this variable.
|
text by setting this variable.
|
||||||
|
6
help.go
6
help.go
@ -242,11 +242,7 @@ func ShowCommandHelp(ctx *Context, command string) error {
|
|||||||
c.Subcommands = append(c.Subcommands, helpCommandDontUse)
|
c.Subcommands = append(c.Subcommands, helpCommandDontUse)
|
||||||
}
|
}
|
||||||
if !ctx.App.HideHelp && HelpFlag != nil {
|
if !ctx.App.HideHelp && HelpFlag != nil {
|
||||||
if c.flagCategories == nil {
|
c.appendFlag(HelpFlag)
|
||||||
c.flagCategories = newFlagCategoriesFromFlags([]Flag{HelpFlag})
|
|
||||||
} else {
|
|
||||||
c.flagCategories.AddFlag("", HelpFlag)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
templ := c.CustomHelpTemplate
|
templ := c.CustomHelpTemplate
|
||||||
if templ == "" {
|
if templ == "" {
|
||||||
|
12
help_test.go
12
help_test.go
@ -1366,7 +1366,8 @@ DESCRIPTION:
|
|||||||
case
|
case
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
--help, -h show help (default: false)
|
--help, -h show help
|
||||||
|
(default: false)
|
||||||
`
|
`
|
||||||
|
|
||||||
if output.String() != expected {
|
if output.String() != expected {
|
||||||
@ -1435,7 +1436,8 @@ USAGE:
|
|||||||
even more
|
even more
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
--help, -h show help (default: false)
|
--help, -h show help
|
||||||
|
(default: false)
|
||||||
`
|
`
|
||||||
|
|
||||||
if output.String() != expected {
|
if output.String() != expected {
|
||||||
@ -1510,8 +1512,10 @@ USAGE:
|
|||||||
even more
|
even more
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
--help, -h show help (default: false)
|
--test-f value my test
|
||||||
--test-f value my test usage
|
usage
|
||||||
|
--help, -h show help
|
||||||
|
(default: false)
|
||||||
`
|
`
|
||||||
|
|
||||||
if output.String() != expected {
|
if output.String() != expected {
|
||||||
|
10
template.go
10
template.go
@ -18,8 +18,8 @@ var visibleFlagCategoryTemplate = `{{range .VisibleFlagCategories}}
|
|||||||
{{else}}{{$e}}
|
{{else}}{{$e}}
|
||||||
{{end}}{{end}}{{end}}`
|
{{end}}{{end}}{{end}}`
|
||||||
|
|
||||||
var visibleFlagTemplate = `{{range $index, $option := .VisibleFlags}}{{if $index}}{{end}}
|
var visibleFlagTemplate = `{{range $i, $e := .VisibleFlags}}
|
||||||
{{wrap $option.String 6}}{{end}}`
|
{{wrap $e.String 6}}{{end}}`
|
||||||
|
|
||||||
var versionTemplate = `{{if .Version}}{{if not .HideVersion}}
|
var versionTemplate = `{{if .Version}}{{if not .HideVersion}}
|
||||||
|
|
||||||
@ -73,7 +73,8 @@ DESCRIPTION:
|
|||||||
|
|
||||||
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
|
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
|
||||||
|
|
||||||
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}`
|
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
|
||||||
|
`
|
||||||
|
|
||||||
// SubcommandHelpTemplate is the text template for the subcommand help topic.
|
// SubcommandHelpTemplate is the text template for the subcommand help topic.
|
||||||
// cli.go uses text/template to render templates. You can
|
// 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 "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
|
||||||
|
|
||||||
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}`
|
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
|
||||||
|
`
|
||||||
|
|
||||||
var MarkdownDocTemplate = `{{if gt .SectionNum 0}}% {{ .App.Name }} {{ .SectionNum }}
|
var MarkdownDocTemplate = `{{if gt .SectionNum 0}}% {{ .App.Name }} {{ .SectionNum }}
|
||||||
|
|
||||||
|
6
testdata/godoc-v2.x.txt
vendored
6
testdata/godoc-v2.x.txt
vendored
@ -73,7 +73,8 @@ DESCRIPTION:
|
|||||||
|
|
||||||
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
|
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
|
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
|
uses text/template to render templates. You can render custom help text by
|
||||||
setting this variable.
|
setting this variable.
|
||||||
@ -144,7 +145,8 @@ COMMANDS:{{template "visibleCommandTemplate" .}}{{end}}{{if .VisibleFlagCategori
|
|||||||
|
|
||||||
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
|
OPTIONS:{{template "visibleFlagCategoryTemplate" .}}{{else if .VisibleFlags}}
|
||||||
|
|
||||||
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}`
|
OPTIONS:{{template "visibleFlagTemplate" .}}{{end}}
|
||||||
|
`
|
||||||
SubcommandHelpTemplate is the text template for the subcommand help topic.
|
SubcommandHelpTemplate is the text template for the subcommand help topic.
|
||||||
cli.go uses text/template to render templates. You can render custom help
|
cli.go uses text/template to render templates. You can render custom help
|
||||||
text by setting this variable.
|
text by setting this variable.
|
||||||
|
Loading…
Reference in New Issue
Block a user