Extend flag categorization to top-level (global) flags
This commit is contained in:
parent
75e4ee69e9
commit
e4580f0c50
10
app.go
10
app.go
@ -52,7 +52,7 @@ type App struct {
|
|||||||
HideVersion bool
|
HideVersion bool
|
||||||
// categories contains the categorized commands and is populated on app startup
|
// categories contains the categorized commands and is populated on app startup
|
||||||
categories CommandCategories
|
categories CommandCategories
|
||||||
// Populate on app startup, only gettable through method Categories()
|
// flagCategories contains the categorized flags and is populated on app startup
|
||||||
flagCategories FlagCategories
|
flagCategories FlagCategories
|
||||||
// An action to execute when the shell completion flag is set
|
// An action to execute when the shell completion flag is set
|
||||||
BashComplete BashCompleteFunc
|
BashComplete BashCompleteFunc
|
||||||
@ -217,6 +217,14 @@ func (a *App) Setup() {
|
|||||||
}
|
}
|
||||||
sort.Sort(a.categories.(*commandCategories))
|
sort.Sort(a.categories.(*commandCategories))
|
||||||
|
|
||||||
|
a.flagCategories = FlagCategories{}
|
||||||
|
for _, fl := range a.Flags {
|
||||||
|
if cf, ok := fl.(CategorizableFlag); ok {
|
||||||
|
a.flagCategories.AddFlag(cf.GetCategory(), cf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.Sort(a.flagCategories)
|
||||||
|
|
||||||
if a.Metadata == nil {
|
if a.Metadata == nil {
|
||||||
a.Metadata = make(map[string]interface{})
|
a.Metadata = make(map[string]interface{})
|
||||||
}
|
}
|
||||||
|
83
app_test.go
83
app_test.go
@ -1789,20 +1789,23 @@ func TestApp_VisibleCategories(t *testing.T) {
|
|||||||
HideHelp: true,
|
HideHelp: true,
|
||||||
Commands: []*Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "command1",
|
Name: "command1",
|
||||||
Category: "1",
|
Category: "1",
|
||||||
HelpName: "foo command1",
|
HelpName: "foo command1",
|
||||||
Hidden: true,
|
Hidden: true,
|
||||||
|
FlagCategories: FlagCategories{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "command2",
|
Name: "command2",
|
||||||
Category: "2",
|
Category: "2",
|
||||||
HelpName: "foo command2",
|
HelpName: "foo command2",
|
||||||
|
FlagCategories: FlagCategories{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "command3",
|
Name: "command3",
|
||||||
Category: "3",
|
Category: "3",
|
||||||
HelpName: "foo command3",
|
HelpName: "foo command3",
|
||||||
|
FlagCategories: FlagCategories{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -1830,21 +1833,24 @@ func TestApp_VisibleCategories(t *testing.T) {
|
|||||||
HideHelp: true,
|
HideHelp: true,
|
||||||
Commands: []*Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "command1",
|
Name: "command1",
|
||||||
Category: "1",
|
Category: "1",
|
||||||
HelpName: "foo command1",
|
HelpName: "foo command1",
|
||||||
Hidden: true,
|
Hidden: true,
|
||||||
|
FlagCategories: FlagCategories{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "command2",
|
Name: "command2",
|
||||||
Category: "2",
|
Category: "2",
|
||||||
HelpName: "foo command2",
|
HelpName: "foo command2",
|
||||||
Hidden: true,
|
Hidden: true,
|
||||||
|
FlagCategories: FlagCategories{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "command3",
|
Name: "command3",
|
||||||
Category: "3",
|
Category: "3",
|
||||||
HelpName: "foo command3",
|
HelpName: "foo command3",
|
||||||
|
FlagCategories: FlagCategories{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -1866,22 +1872,25 @@ func TestApp_VisibleCategories(t *testing.T) {
|
|||||||
HideHelp: true,
|
HideHelp: true,
|
||||||
Commands: []*Command{
|
Commands: []*Command{
|
||||||
{
|
{
|
||||||
Name: "command1",
|
Name: "command1",
|
||||||
Category: "1",
|
Category: "1",
|
||||||
HelpName: "foo command1",
|
HelpName: "foo command1",
|
||||||
Hidden: true,
|
Hidden: true,
|
||||||
|
FlagCategories: FlagCategories{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "command2",
|
Name: "command2",
|
||||||
Category: "2",
|
Category: "2",
|
||||||
HelpName: "foo command2",
|
HelpName: "foo command2",
|
||||||
Hidden: true,
|
Hidden: true,
|
||||||
|
FlagCategories: FlagCategories{},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "command3",
|
Name: "command3",
|
||||||
Category: "3",
|
Category: "3",
|
||||||
HelpName: "foo command3",
|
HelpName: "foo command3",
|
||||||
Hidden: true,
|
Hidden: true,
|
||||||
|
FlagCategories: FlagCategories{},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@ -1890,6 +1899,14 @@ func TestApp_VisibleCategories(t *testing.T) {
|
|||||||
expect(t, []CommandCategory{}, app.VisibleCategories())
|
expect(t, []CommandCategory{}, app.VisibleCategories())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestApp_VisibleFlagCategories(t *testing.T) {
|
||||||
|
app := &App{}
|
||||||
|
vfc := app.VisibleFlagCategories()
|
||||||
|
if len(vfc) != 0 {
|
||||||
|
t.Errorf("unexpected visible flag categories %+v", vfc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
|
func TestApp_Run_DoesNotOverwriteErrorFromBefore(t *testing.T) {
|
||||||
app := &App{
|
app := &App{
|
||||||
Action: func(c *Context) error { return nil },
|
Action: func(c *Context) error { return nil },
|
||||||
|
18
template.go
18
template.go
@ -22,11 +22,16 @@ AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
|
|||||||
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
|
||||||
{{.Name}}:{{range .VisibleCommands}}
|
{{.Name}}:{{range .VisibleCommands}}
|
||||||
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}}
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{else}}{{range .VisibleCommands}}
|
||||||
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{end}}{{if .VisibleFlags}}
|
{{join .Names ", "}}{{"\t"}}{{.Usage}}{{end}}{{end}}{{end}}{{end}}{{if .VisibleFlagCategories}}
|
||||||
|
|
||||||
|
GLOBAL OPTIONS:{{range .VisibleFlagCategories}}
|
||||||
|
{{.Name}}
|
||||||
|
{{range .VisibleFlags}}{{.}}
|
||||||
|
{{end}}{{end}}{{else}}{{if .VisibleFlags}}
|
||||||
|
|
||||||
GLOBAL OPTIONS:
|
GLOBAL OPTIONS:
|
||||||
{{range $index, $option := .VisibleFlags}}{{if $index}}
|
{{range $index, $option := .VisibleFlags}}{{if $index}}
|
||||||
{{end}}{{$option}}{{end}}{{end}}{{if .Copyright}}
|
{{end}}{{$option}}{{end}}{{end}}{{end}}{{if .Copyright}}
|
||||||
|
|
||||||
COPYRIGHT:
|
COPYRIGHT:
|
||||||
{{.Copyright}}{{end}}
|
{{.Copyright}}{{end}}
|
||||||
@ -45,11 +50,16 @@ CATEGORY:
|
|||||||
{{.Category}}{{end}}{{if .Description}}
|
{{.Category}}{{end}}{{if .Description}}
|
||||||
|
|
||||||
DESCRIPTION:
|
DESCRIPTION:
|
||||||
{{.Description | nindent 3 | trim}}{{end}}{{if .VisibleFlags}}
|
{{.Description | nindent 3 | trim}}{{end}}{{if .VisibleFlagCategories}}
|
||||||
|
|
||||||
|
OPTIONS:{{range .VisibleFlagCategories}}
|
||||||
|
{{.Name}}
|
||||||
|
{{range .VisibleFlags}}{{.}}
|
||||||
|
{{end}}{{end}}{{else}}{{if .VisibleFlags}}
|
||||||
|
|
||||||
OPTIONS:
|
OPTIONS:
|
||||||
{{range .VisibleFlags}}{{.}}
|
{{range .VisibleFlags}}{{.}}
|
||||||
{{end}}{{end}}
|
{{end}}{{end}}{{end}}
|
||||||
`
|
`
|
||||||
|
|
||||||
// SubcommandHelpTemplate is the text template for the subcommand help topic.
|
// SubcommandHelpTemplate is the text template for the subcommand help topic.
|
||||||
|
Loading…
Reference in New Issue
Block a user