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

12
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
@ -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)

View File

@ -1607,6 +1607,10 @@ func (c *customBoolFlag) GetHidden() bool {
return false
}
func (c *customBoolFlag) GetCategory() string {
return ""
}
func (c *customBoolFlag) Apply(set *flag.FlagSet) {
set.String(c.Nombre, c.Nombre, "")
}

View File

@ -73,6 +73,7 @@ type Flag interface {
// Apply Flag settings to the given flag set
Apply(*flag.FlagSet)
GetName() string
GetCategory() string
GetHidden() bool
}

View File

@ -35,6 +35,11 @@ func (f BoolFlag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f BoolFlag) GetCategory() string {
return f.Category
}
// Bool looks up the value of a local BoolFlag, returns
// false if not found
func (c *Context) Bool(name string) bool {
@ -89,6 +94,11 @@ func (f BoolTFlag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f BoolTFlag) GetCategory() string {
return f.Category
}
// BoolT looks up the value of a local BoolTFlag, returns
// false if not found
func (c *Context) BoolT(name string) bool {
@ -144,6 +154,11 @@ func (f DurationFlag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f DurationFlag) GetCategory() string {
return f.Category
}
// Duration looks up the value of a local DurationFlag, returns
// 0 if not found
func (c *Context) Duration(name string) time.Duration {
@ -199,6 +214,11 @@ func (f Float64Flag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f Float64Flag) GetCategory() string {
return f.Category
}
// Float64 looks up the value of a local Float64Flag, returns
// 0 if not found
func (c *Context) Float64(name string) float64 {
@ -253,6 +273,11 @@ func (f GenericFlag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f GenericFlag) GetCategory() string {
return f.Category
}
// Generic looks up the value of a local GenericFlag, returns
// nil if not found
func (c *Context) Generic(name string) interface{} {
@ -308,6 +333,11 @@ func (f Int64Flag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f Int64Flag) GetCategory() string {
return f.Category
}
// Int64 looks up the value of a local Int64Flag, returns
// 0 if not found
func (c *Context) Int64(name string) int64 {
@ -363,6 +393,11 @@ func (f IntFlag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f IntFlag) GetCategory() string {
return f.Category
}
// Int looks up the value of a local IntFlag, returns
// 0 if not found
func (c *Context) Int(name string) int {
@ -417,6 +452,11 @@ func (f IntSliceFlag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f IntSliceFlag) GetCategory() string {
return f.Category
}
// IntSlice looks up the value of a local IntSliceFlag, returns
// nil if not found
func (c *Context) IntSlice(name string) []int {
@ -471,6 +511,11 @@ func (f Int64SliceFlag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f Int64SliceFlag) GetCategory() string {
return f.Category
}
// Int64Slice looks up the value of a local Int64SliceFlag, returns
// nil if not found
func (c *Context) Int64Slice(name string) []int64 {
@ -526,6 +571,11 @@ func (f StringFlag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f StringFlag) GetCategory() string {
return f.Category
}
// String looks up the value of a local StringFlag, returns
// "" if not found
func (c *Context) String(name string) string {
@ -580,6 +630,11 @@ func (f StringSliceFlag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f StringSliceFlag) GetCategory() string {
return f.Category
}
// StringSlice looks up the value of a local StringSliceFlag, returns
// nil if not found
func (c *Context) StringSlice(name string) []string {
@ -635,6 +690,11 @@ func (f Uint64Flag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f Uint64Flag) GetCategory() string {
return f.Category
}
// Uint64 looks up the value of a local Uint64Flag, returns
// 0 if not found
func (c *Context) Uint64(name string) uint64 {
@ -690,6 +750,11 @@ func (f UintFlag) GetHidden() bool {
return f.Hidden
}
// GetCategory lets us access the flag category
func (f UintFlag) GetCategory() string {
return f.Category
}
// Uint looks up the value of a local UintFlag, returns
// 0 if not found
func (c *Context) Uint(name string) uint {

View File

@ -176,6 +176,11 @@ def _write_cli_flag_types(outfile, types):
return f.Hidden
}}
// GetCategory lets us access the flag category
func (f {name}Flag) GetCategory() string {{
return f.Category
}}
// {name} looks up the value of a local {name}Flag, returns
// {context_default} if not found
func (c *Context) {name}(name string) {context_type} {{