Start of category flag support

This adds what I think needs to be done to support categories for flags
but we will see if that works. It also forces the scripts to use python2
since they blow up under python3 which is becoming the default python on
many linux systems.

Small fix to app_test as well so it conforms to the new Flag interface.
This commit is contained in:
Michael Schuett 2019-01-27 01:41:06 -05:00
parent b67dcf995b
commit ff1c0b58dd
6 changed files with 136 additions and 2 deletions

View File

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

View File

@ -42,3 +42,46 @@ func (c *CommandCategory) VisibleCommands() []Command {
} }
return ret return ret
} }
// FlagCategories is a slice of *FlagCategory.
type FlagCategories []*FlagCategory
// FlagCategory is a category containing commands.
type FlagCategory struct {
Name string
Flags Flags
}
func (f FlagCategories) Less(i, j int) bool {
return lexicographicLess(f[i].Name, f[j].Name)
}
func (f FlagCategories) Len() int {
return len(f)
}
func (f FlagCategories) Swap(i, j int) {
f[i], f[j] = f[j], f[i]
}
// AddFlags adds a command to a category.
func (f FlagCategories) AddFlag(category string, flag Flag) FlagCategories {
for _, flagCategory := range f {
if flagCategory.Name == category {
flagCategory.Flags = append(flagCategory.Flags, flag)
return f
}
}
return append(f, &FlagCategory{Name: category, Flags: []Flag{flag}})
}
// VisibleFlags returns a slice of the Flags with Hidden=false
func (c *FlagCategory) VisibleFlags() []Flag {
ret := []Flag{}
for _, flag := range c.Flags {
if !flag.GetHidden() {
ret = append(ret, flag)
}
}
return ret
}

View File

@ -73,8 +73,11 @@ type Flag interface {
// Apply Flag settings to the given flag set // Apply Flag settings to the given flag set
Apply(*flag.FlagSet) Apply(*flag.FlagSet)
GetName() string GetName() string
GetHidden() bool
} }
type Flags []Flag
// errorableFlag is an interface that allows us to return errors during apply // errorableFlag is an interface that allows us to return errors during apply
// it allows flags defined in this library to return errors in a fashion backwards compatible // it allows flags defined in this library to return errors in a fashion backwards compatible
// TODO remove in v2 and modify the existing Flag interface to return errors // TODO remove in v2 and modify the existing Flag interface to return errors

View File

@ -15,6 +15,7 @@ type BoolFlag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Destination *bool Destination *bool
} }
@ -29,6 +30,11 @@ func (f BoolFlag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f BoolFlag) GetHidden() bool {
return f.Hidden
}
// Bool looks up the value of a local BoolFlag, returns // Bool looks up the value of a local BoolFlag, returns
// false if not found // false if not found
func (c *Context) Bool(name string) bool { func (c *Context) Bool(name string) bool {
@ -63,6 +69,7 @@ type BoolTFlag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Destination *bool Destination *bool
} }
@ -77,6 +84,11 @@ func (f BoolTFlag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f BoolTFlag) GetHidden() bool {
return f.Hidden
}
// BoolT looks up the value of a local BoolTFlag, returns // BoolT looks up the value of a local BoolTFlag, returns
// false if not found // false if not found
func (c *Context) BoolT(name string) bool { func (c *Context) BoolT(name string) bool {
@ -111,6 +123,7 @@ type DurationFlag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Value time.Duration Value time.Duration
Destination *time.Duration Destination *time.Duration
} }
@ -126,6 +139,11 @@ func (f DurationFlag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f DurationFlag) GetHidden() bool {
return f.Hidden
}
// Duration looks up the value of a local DurationFlag, returns // Duration looks up the value of a local DurationFlag, returns
// 0 if not found // 0 if not found
func (c *Context) Duration(name string) time.Duration { func (c *Context) Duration(name string) time.Duration {
@ -160,6 +178,7 @@ type Float64Flag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Value float64 Value float64
Destination *float64 Destination *float64
} }
@ -175,6 +194,11 @@ func (f Float64Flag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f Float64Flag) GetHidden() bool {
return f.Hidden
}
// Float64 looks up the value of a local Float64Flag, returns // Float64 looks up the value of a local Float64Flag, returns
// 0 if not found // 0 if not found
func (c *Context) Float64(name string) float64 { func (c *Context) Float64(name string) float64 {
@ -209,6 +233,7 @@ type GenericFlag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Value Generic Value Generic
} }
@ -223,6 +248,11 @@ func (f GenericFlag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f GenericFlag) GetHidden() bool {
return f.Hidden
}
// Generic looks up the value of a local GenericFlag, returns // Generic looks up the value of a local GenericFlag, returns
// nil if not found // nil if not found
func (c *Context) Generic(name string) interface{} { func (c *Context) Generic(name string) interface{} {
@ -257,6 +287,7 @@ type Int64Flag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Value int64 Value int64
Destination *int64 Destination *int64
} }
@ -272,6 +303,11 @@ func (f Int64Flag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f Int64Flag) GetHidden() bool {
return f.Hidden
}
// Int64 looks up the value of a local Int64Flag, returns // Int64 looks up the value of a local Int64Flag, returns
// 0 if not found // 0 if not found
func (c *Context) Int64(name string) int64 { func (c *Context) Int64(name string) int64 {
@ -306,6 +342,7 @@ type IntFlag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Value int Value int
Destination *int Destination *int
} }
@ -321,6 +358,11 @@ func (f IntFlag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f IntFlag) GetHidden() bool {
return f.Hidden
}
// Int looks up the value of a local IntFlag, returns // Int looks up the value of a local IntFlag, returns
// 0 if not found // 0 if not found
func (c *Context) Int(name string) int { func (c *Context) Int(name string) int {
@ -355,6 +397,7 @@ type IntSliceFlag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Value *IntSlice Value *IntSlice
} }
@ -369,6 +412,11 @@ func (f IntSliceFlag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f IntSliceFlag) GetHidden() bool {
return f.Hidden
}
// IntSlice looks up the value of a local IntSliceFlag, returns // IntSlice looks up the value of a local IntSliceFlag, returns
// nil if not found // nil if not found
func (c *Context) IntSlice(name string) []int { func (c *Context) IntSlice(name string) []int {
@ -403,6 +451,7 @@ type Int64SliceFlag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Value *Int64Slice Value *Int64Slice
} }
@ -417,6 +466,11 @@ func (f Int64SliceFlag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f Int64SliceFlag) GetHidden() bool {
return f.Hidden
}
// Int64Slice looks up the value of a local Int64SliceFlag, returns // Int64Slice looks up the value of a local Int64SliceFlag, returns
// nil if not found // nil if not found
func (c *Context) Int64Slice(name string) []int64 { func (c *Context) Int64Slice(name string) []int64 {
@ -451,6 +505,7 @@ type StringFlag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Value string Value string
Destination *string Destination *string
} }
@ -466,6 +521,11 @@ func (f StringFlag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f StringFlag) GetHidden() bool {
return f.Hidden
}
// String looks up the value of a local StringFlag, returns // String looks up the value of a local StringFlag, returns
// "" if not found // "" if not found
func (c *Context) String(name string) string { func (c *Context) String(name string) string {
@ -500,6 +560,7 @@ type StringSliceFlag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Value *StringSlice Value *StringSlice
} }
@ -514,6 +575,11 @@ func (f StringSliceFlag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f StringSliceFlag) GetHidden() bool {
return f.Hidden
}
// StringSlice looks up the value of a local StringSliceFlag, returns // StringSlice looks up the value of a local StringSliceFlag, returns
// nil if not found // nil if not found
func (c *Context) StringSlice(name string) []string { func (c *Context) StringSlice(name string) []string {
@ -548,6 +614,7 @@ type Uint64Flag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Value uint64 Value uint64
Destination *uint64 Destination *uint64
} }
@ -563,6 +630,11 @@ func (f Uint64Flag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f Uint64Flag) GetHidden() bool {
return f.Hidden
}
// Uint64 looks up the value of a local Uint64Flag, returns // Uint64 looks up the value of a local Uint64Flag, returns
// 0 if not found // 0 if not found
func (c *Context) Uint64(name string) uint64 { func (c *Context) Uint64(name string) uint64 {
@ -597,6 +669,7 @@ type UintFlag struct {
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
Value uint Value uint
Destination *uint Destination *uint
} }
@ -612,6 +685,11 @@ func (f UintFlag) GetName() string {
return f.Name return f.Name
} }
// GetHidden lets us know if the flag is hidden or not
func (f UintFlag) GetHidden() bool {
return f.Hidden
}
// Uint looks up the value of a local UintFlag, returns // Uint looks up the value of a local UintFlag, returns
// 0 if not found // 0 if not found
func (c *Context) Uint(name string) uint { func (c *Context) Uint(name string) uint {

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python2
""" """
The flag types that ship with the cli library have many things in common, and The flag types that ship with the cli library have many things in common, and
so we can take advantage of the `go generate` command to create much of the so we can take advantage of the `go generate` command to create much of the
@ -144,6 +144,7 @@ def _write_cli_flag_types(outfile, types):
EnvVar string EnvVar string
FilePath string FilePath string
Hidden bool Hidden bool
Category string
""".format(**typedef)) """.format(**typedef))
if typedef['value']: if typedef['value']:
@ -170,6 +171,11 @@ def _write_cli_flag_types(outfile, types):
return f.Name return f.Name
}} }}
// GetHidden lets us know if the flag is hidden or not
func (f {name}Flag) GetHidden() bool {{
return f.Hidden
}}
// {name} looks up the value of a local {name}Flag, returns // {name} looks up the value of a local {name}Flag, returns
// {context_default} if not found // {context_default} if not found
func (c *Context) {name}(name string) {context_type} {{ func (c *Context) {name}(name string) {context_type} {{

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/env python2
from __future__ import print_function from __future__ import print_function
import argparse import argparse