Add in Category to Flag interface

This commit is contained in:
Michael Schuett
2019-01-27 11:41:54 -05:00
parent 7c383b0d16
commit 9dd96e9e90
5 changed files with 88 additions and 1 deletions

14
app.go
View File

@@ -51,6 +51,8 @@ type App struct {
HideVersion bool
// Populate on app startup, only gettable through method Categories()
categories CommandCategories
// Populate on app startup, only gettable through method Categories()
flagCategories FlagCategories
// An action to execute when the bash-completion flag is set
BashComplete BashCompleteFunc
// An action to execute before any subcommands are run, but after the context is ready
@@ -164,7 +166,7 @@ func (a *App) Setup() {
}
sort.Sort(a.categories)
if a.Metadata == nil {
if a.Metadata == nil {
a.Metadata = make(map[string]interface{})
}
@@ -192,6 +194,11 @@ func (a *App) Run(arguments []string) (err error) {
return err
}
a.flagCategories = FlagCategories{}
for _, flag := range a.Flags {
a.flagCategories = a.flagCategories.AddFlag(flag.GetCategory(), flag)
}
set.SetOutput(ioutil.Discard)
err = set.Parse(arguments[1:])
nerr := normalizeFlags(a.Flags, set)
@@ -437,6 +444,11 @@ func (a *App) VisibleCommands() []Command {
return ret
}
// Categories returns a slice containing all the categories with the commands they contain
func (a *App) FlagCategories() FlagCategories {
return a.flagCategories
}
// VisibleFlags returns a slice of the Flags with Hidden=false
func (a *App) VisibleFlags() []Flag {
return visibleFlags(a.Flags)