Skip to content

Commit

Permalink
Extend flag categorization to top-level (global) flags
Browse files Browse the repository at this point in the history
  • Loading branch information
meatballhat committed Apr 22, 2022
1 parent 75e4ee6 commit e4580f0
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 38 deletions.
10 changes: 9 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type App struct {
HideVersion bool
// categories contains the categorized commands and is populated on app startup
categories CommandCategories
// Populate on app startup, only gettable through method Categories()
// flagCategories contains the categorized flags and is populated on app startup
flagCategories FlagCategories
// An action to execute when the shell completion flag is set
BashComplete BashCompleteFunc
Expand Down Expand Up @@ -217,6 +217,14 @@ func (a *App) Setup() {
}
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 {
a.Metadata = make(map[string]interface{})
}
Expand Down
83 changes: 50 additions & 33 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1789,20 +1789,23 @@ func TestApp_VisibleCategories(t *testing.T) {
HideHelp: true,
Commands: []*Command{
{
Name: "command1",
Category: "1",
HelpName: "foo command1",
Hidden: true,
Name: "command1",
Category: "1",
HelpName: "foo command1",
Hidden: true,
FlagCategories: FlagCategories{},
},
{
Name: "command2",
Category: "2",
HelpName: "foo command2",
Name: "command2",
Category: "2",
HelpName: "foo command2",
FlagCategories: FlagCategories{},
},
{
Name: "command3",
Category: "3",
HelpName: "foo command3",
Name: "command3",
Category: "3",
HelpName: "foo command3",
FlagCategories: FlagCategories{},
},
},
}
Expand Down Expand Up @@ -1830,21 +1833,24 @@ func TestApp_VisibleCategories(t *testing.T) {
HideHelp: true,
Commands: []*Command{
{
Name: "command1",
Category: "1",
HelpName: "foo command1",
Hidden: true,
Name: "command1",
Category: "1",
HelpName: "foo command1",
Hidden: true,
FlagCategories: FlagCategories{},
},
{
Name: "command2",
Category: "2",
HelpName: "foo command2",
Hidden: true,
Name: "command2",
Category: "2",
HelpName: "foo command2",
Hidden: true,
FlagCategories: FlagCategories{},
},
{
Name: "command3",
Category: "3",
HelpName: "foo command3",
Name: "command3",
Category: "3",
HelpName: "foo command3",
FlagCategories: FlagCategories{},
},
},
}
Expand All @@ -1866,22 +1872,25 @@ func TestApp_VisibleCategories(t *testing.T) {
HideHelp: true,
Commands: []*Command{
{
Name: "command1",
Category: "1",
HelpName: "foo command1",
Hidden: true,
Name: "command1",
Category: "1",
HelpName: "foo command1",
Hidden: true,
FlagCategories: FlagCategories{},
},
{
Name: "command2",
Category: "2",
HelpName: "foo command2",
Hidden: true,
Name: "command2",
Category: "2",
HelpName: "foo command2",
Hidden: true,
FlagCategories: FlagCategories{},
},
{
Name: "command3",
Category: "3",
HelpName: "foo command3",
Hidden: true,
Name: "command3",
Category: "3",
HelpName: "foo command3",
Hidden: true,
FlagCategories: FlagCategories{},
},
},
}
Expand All @@ -1890,6 +1899,14 @@ func TestApp_VisibleCategories(t *testing.T) {
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) {
app := &App{
Action: func(c *Context) error { return nil },
Expand Down
18 changes: 14 additions & 4 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ AUTHOR{{with $length := len .Authors}}{{if ne 1 $length}}S{{end}}{{end}}:
COMMANDS:{{range .VisibleCategories}}{{if .Name}}
{{.Name}}:{{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:
{{range $index, $option := .VisibleFlags}}{{if $index}}
{{end}}{{$option}}{{end}}{{end}}{{if .Copyright}}
{{end}}{{$option}}{{end}}{{end}}{{end}}{{if .Copyright}}
COPYRIGHT:
{{.Copyright}}{{end}}
Expand All @@ -45,11 +50,16 @@ CATEGORY:
{{.Category}}{{end}}{{if .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:
{{range .VisibleFlags}}{{.}}
{{end}}{{end}}
{{end}}{{end}}{{end}}
`

// SubcommandHelpTemplate is the text template for the subcommand help topic.
Expand Down

0 comments on commit e4580f0

Please sign in to comment.