Porting remainder of #796

This commit is contained in:
Dan Buch 2022-04-22 15:00:43 -04:00
parent 44958693a1
commit 75e4ee69e9
Signed by: meatballhat
GPG Key ID: A12F782281063434
18 changed files with 112 additions and 12 deletions

6
app.go
View File

@ -185,8 +185,10 @@ func (a *App) Setup() {
} }
fc := FlagCategories{} fc := FlagCategories{}
for _, flag := range c.Flags { for _, fl := range c.Flags {
fc = fc.AddFlag(flag.GetCategory(), flag) if cf, ok := fl.(CategorizableFlag); ok {
fc = fc.AddFlag(cf.GetCategory(), cf)
}
} }
sort.Sort(fc) sort.Sort(fc)

View File

@ -84,7 +84,7 @@ type FlagCategories []*FlagCategory
// FlagCategory is a category containing commands. // FlagCategory is a category containing commands.
type FlagCategory struct { type FlagCategory struct {
Name string Name string
Flags Flags Flags []Flag
} }
func (f FlagCategories) Less(i, j int) bool { func (f FlagCategories) Less(i, j int) bool {
@ -111,11 +111,13 @@ func (f FlagCategories) AddFlag(category string, flag Flag) FlagCategories {
} }
// VisibleFlags returns a slice of the Flags with Hidden=false // VisibleFlags returns a slice of the Flags with Hidden=false
func (c *FlagCategory) VisibleFlags() []Flag { func (c *FlagCategory) VisibleFlags() []VisibleFlag {
ret := []Flag{} ret := []VisibleFlag{}
for _, flag := range c.Flags { for _, fl := range c.Flags {
if !flag.GetHidden() { if vf, ok := fl.(VisibleFlag); ok {
ret = append(ret, flag) if vf.IsVisible() {
ret = append(ret, vf)
}
} }
} }
return ret return ret

10
flag.go
View File

@ -94,8 +94,6 @@ type Flag interface {
Apply(*flag.FlagSet) error Apply(*flag.FlagSet) error
Names() []string Names() []string
IsSet() bool IsSet() bool
GetCategory() string
GetHidden() bool
} }
// RequiredFlag is an interface that allows us to mark flags as required // RequiredFlag is an interface that allows us to mark flags as required
@ -135,6 +133,14 @@ type VisibleFlag interface {
IsVisible() bool IsVisible() bool
} }
// CategorizableFlag is an interface that allows us to potentially
// use a flag in a categorized representation.
type CategorizableFlag interface {
VisibleFlag
GetCategory() string
}
func flagSet(name string, flags []Flag) (*flag.FlagSet, error) { func flagSet(name string, flags []Flag) (*flag.FlagSet, error) {
set := flag.NewFlagSet(name, flag.ContinueOnError) set := flag.NewFlagSet(name, flag.ContinueOnError)

View File

@ -19,6 +19,7 @@ type BoolFlag struct {
DefaultText string DefaultText string
Destination *bool Destination *bool
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -52,6 +53,11 @@ func (f *BoolFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *BoolFlag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *BoolFlag) GetValue() string { func (f *BoolFlag) GetValue() string {

View File

@ -19,6 +19,7 @@ type DurationFlag struct {
DefaultText string DefaultText string
Destination *time.Duration Destination *time.Duration
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -52,6 +53,11 @@ func (f *DurationFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *DurationFlag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *DurationFlag) GetValue() string { func (f *DurationFlag) GetValue() string {

View File

@ -19,6 +19,7 @@ type Float64Flag struct {
DefaultText string DefaultText string
Destination *float64 Destination *float64
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -52,6 +53,11 @@ func (f *Float64Flag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *Float64Flag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *Float64Flag) GetValue() string { func (f *Float64Flag) GetValue() string {

View File

@ -85,6 +85,7 @@ type Float64SliceFlag struct {
Value *Float64Slice Value *Float64Slice
DefaultText string DefaultText string
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -118,6 +119,11 @@ func (f *Float64SliceFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *Float64SliceFlag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *Float64SliceFlag) GetValue() string { func (f *Float64SliceFlag) GetValue() string {

View File

@ -24,6 +24,7 @@ type GenericFlag struct {
Value Generic Value Generic
DefaultText string DefaultText string
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -57,6 +58,11 @@ func (f *GenericFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *GenericFlag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *GenericFlag) GetValue() string { func (f *GenericFlag) GetValue() string {

View File

@ -19,6 +19,7 @@ type IntFlag struct {
DefaultText string DefaultText string
Destination *int Destination *int
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -52,6 +53,11 @@ func (f *IntFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *IntFlag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *IntFlag) GetValue() string { func (f *IntFlag) GetValue() string {

View File

@ -19,6 +19,7 @@ type Int64Flag struct {
DefaultText string DefaultText string
Destination *int64 Destination *int64
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -52,6 +53,11 @@ func (f *Int64Flag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *Int64Flag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *Int64Flag) GetValue() string { func (f *Int64Flag) GetValue() string {

View File

@ -86,6 +86,7 @@ type Int64SliceFlag struct {
Value *Int64Slice Value *Int64Slice
DefaultText string DefaultText string
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -115,10 +116,15 @@ func (f *Int64SliceFlag) TakesValue() bool {
} }
// GetUsage returns the usage string for the flag // GetUsage returns the usage string for the flag
func (f Int64SliceFlag) GetUsage() string { func (f *Int64SliceFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *Int64SliceFlag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *Int64SliceFlag) GetValue() string { func (f *Int64SliceFlag) GetValue() string {

View File

@ -97,6 +97,7 @@ type IntSliceFlag struct {
Value *IntSlice Value *IntSlice
DefaultText string DefaultText string
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -126,10 +127,15 @@ func (f *IntSliceFlag) TakesValue() bool {
} }
// GetUsage returns the usage string for the flag // GetUsage returns the usage string for the flag
func (f IntSliceFlag) GetUsage() string { func (f *IntSliceFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *IntSliceFlag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *IntSliceFlag) GetValue() string { func (f *IntSliceFlag) GetValue() string {

View File

@ -18,6 +18,7 @@ type PathFlag struct {
DefaultText string DefaultText string
Destination *string Destination *string
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -51,6 +52,11 @@ func (f *PathFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *PathFlag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *PathFlag) GetValue() string { func (f *PathFlag) GetValue() string {

View File

@ -19,6 +19,7 @@ type StringFlag struct {
DefaultText string DefaultText string
Destination *string Destination *string
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -52,6 +53,11 @@ func (f *StringFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *StringFlag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *StringFlag) GetValue() string { func (f *StringFlag) GetValue() string {

View File

@ -82,6 +82,7 @@ type StringSliceFlag struct {
DefaultText string DefaultText string
HasBeenSet bool HasBeenSet bool
Destination *StringSlice Destination *StringSlice
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -115,6 +116,11 @@ func (f *StringSliceFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *StringSliceFlag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *StringSliceFlag) GetValue() string { func (f *StringSliceFlag) GetValue() string {

View File

@ -72,6 +72,7 @@ type TimestampFlag struct {
DefaultText string DefaultText string
HasBeenSet bool HasBeenSet bool
Destination *Timestamp Destination *Timestamp
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -105,6 +106,11 @@ func (f *TimestampFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *TimestampFlag) GetCategory() string {
return f.Category
}
// GetValue returns the flags value as string representation and an empty // GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all. // string if the flag takes no value at all.
func (f *TimestampFlag) GetValue() string { func (f *TimestampFlag) GetValue() string {

View File

@ -19,6 +19,7 @@ type UintFlag struct {
DefaultText string DefaultText string
Destination *uint Destination *uint
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -52,6 +53,11 @@ func (f *UintFlag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *UintFlag) GetCategory() string {
return f.Category
}
// IsVisible returns true if the flag is not hidden, otherwise false // IsVisible returns true if the flag is not hidden, otherwise false
func (f *UintFlag) IsVisible() bool { func (f *UintFlag) IsVisible() bool {
return !f.Hidden return !f.Hidden

View File

@ -19,6 +19,7 @@ type Uint64Flag struct {
DefaultText string DefaultText string
Destination *uint64 Destination *uint64
HasBeenSet bool HasBeenSet bool
Category string
} }
// IsSet returns whether or not the flag has been set through env or file // IsSet returns whether or not the flag has been set through env or file
@ -52,6 +53,11 @@ func (f *Uint64Flag) GetUsage() string {
return f.Usage return f.Usage
} }
// GetCategory returns the category for the flag
func (f *Uint64Flag) GetCategory() string {
return f.Category
}
// IsVisible returns true if the flag is not hidden, otherwise false // IsVisible returns true if the flag is not hidden, otherwise false
func (f *Uint64Flag) IsVisible() bool { func (f *Uint64Flag) IsVisible() bool {
return !f.Hidden return !f.Hidden