Skip to content

Commit

Permalink
Porting remainder of #796
Browse files Browse the repository at this point in the history
  • Loading branch information
meatballhat committed Apr 22, 2022
1 parent 4495869 commit 75e4ee6
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 12 deletions.
6 changes: 4 additions & 2 deletions app.go
Expand Up @@ -185,8 +185,10 @@ func (a *App) Setup() {
}

fc := FlagCategories{}
for _, flag := range c.Flags {
fc = fc.AddFlag(flag.GetCategory(), flag)
for _, fl := range c.Flags {
if cf, ok := fl.(CategorizableFlag); ok {
fc = fc.AddFlag(cf.GetCategory(), cf)
}
}

sort.Sort(fc)
Expand Down
14 changes: 8 additions & 6 deletions category.go
Expand Up @@ -84,7 +84,7 @@ type FlagCategories []*FlagCategory
// FlagCategory is a category containing commands.
type FlagCategory struct {
Name string
Flags Flags
Flags []Flag
}

func (f FlagCategories) Less(i, j int) bool {
Expand All @@ -111,11 +111,13 @@ func (f FlagCategories) AddFlag(category string, flag Flag) FlagCategories {
}

// 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)
func (c *FlagCategory) VisibleFlags() []VisibleFlag {
ret := []VisibleFlag{}
for _, fl := range c.Flags {
if vf, ok := fl.(VisibleFlag); ok {
if vf.IsVisible() {
ret = append(ret, vf)
}
}
}
return ret
Expand Down
10 changes: 8 additions & 2 deletions flag.go
Expand Up @@ -94,8 +94,6 @@ type Flag interface {
Apply(*flag.FlagSet) error
Names() []string
IsSet() bool
GetCategory() string
GetHidden() bool
}

// RequiredFlag is an interface that allows us to mark flags as required
Expand Down Expand Up @@ -135,6 +133,14 @@ type VisibleFlag interface {
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) {
set := flag.NewFlagSet(name, flag.ContinueOnError)

Expand Down
6 changes: 6 additions & 0 deletions flag_bool.go
Expand Up @@ -19,6 +19,7 @@ type BoolFlag struct {
DefaultText string
Destination *bool
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -52,6 +53,11 @@ func (f *BoolFlag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *BoolFlag) GetValue() string {
Expand Down
6 changes: 6 additions & 0 deletions flag_duration.go
Expand Up @@ -19,6 +19,7 @@ type DurationFlag struct {
DefaultText string
Destination *time.Duration
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -52,6 +53,11 @@ func (f *DurationFlag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *DurationFlag) GetValue() string {
Expand Down
6 changes: 6 additions & 0 deletions flag_float64.go
Expand Up @@ -19,6 +19,7 @@ type Float64Flag struct {
DefaultText string
Destination *float64
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -52,6 +53,11 @@ func (f *Float64Flag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *Float64Flag) GetValue() string {
Expand Down
6 changes: 6 additions & 0 deletions flag_float64_slice.go
Expand Up @@ -85,6 +85,7 @@ type Float64SliceFlag struct {
Value *Float64Slice
DefaultText string
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -118,6 +119,11 @@ func (f *Float64SliceFlag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *Float64SliceFlag) GetValue() string {
Expand Down
6 changes: 6 additions & 0 deletions flag_generic.go
Expand Up @@ -24,6 +24,7 @@ type GenericFlag struct {
Value Generic
DefaultText string
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -57,6 +58,11 @@ func (f *GenericFlag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *GenericFlag) GetValue() string {
Expand Down
6 changes: 6 additions & 0 deletions flag_int.go
Expand Up @@ -19,6 +19,7 @@ type IntFlag struct {
DefaultText string
Destination *int
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -52,6 +53,11 @@ func (f *IntFlag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *IntFlag) GetValue() string {
Expand Down
6 changes: 6 additions & 0 deletions flag_int64.go
Expand Up @@ -19,6 +19,7 @@ type Int64Flag struct {
DefaultText string
Destination *int64
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -52,6 +53,11 @@ func (f *Int64Flag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *Int64Flag) GetValue() string {
Expand Down
8 changes: 7 additions & 1 deletion flag_int64_slice.go
Expand Up @@ -86,6 +86,7 @@ type Int64SliceFlag struct {
Value *Int64Slice
DefaultText string
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -115,10 +116,15 @@ func (f *Int64SliceFlag) TakesValue() bool {
}

// GetUsage returns the usage string for the flag
func (f Int64SliceFlag) GetUsage() string {
func (f *Int64SliceFlag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *Int64SliceFlag) GetValue() string {
Expand Down
8 changes: 7 additions & 1 deletion flag_int_slice.go
Expand Up @@ -97,6 +97,7 @@ type IntSliceFlag struct {
Value *IntSlice
DefaultText string
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -126,10 +127,15 @@ func (f *IntSliceFlag) TakesValue() bool {
}

// GetUsage returns the usage string for the flag
func (f IntSliceFlag) GetUsage() string {
func (f *IntSliceFlag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *IntSliceFlag) GetValue() string {
Expand Down
6 changes: 6 additions & 0 deletions flag_path.go
Expand Up @@ -18,6 +18,7 @@ type PathFlag struct {
DefaultText string
Destination *string
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -51,6 +52,11 @@ func (f *PathFlag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *PathFlag) GetValue() string {
Expand Down
6 changes: 6 additions & 0 deletions flag_string.go
Expand Up @@ -19,6 +19,7 @@ type StringFlag struct {
DefaultText string
Destination *string
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -52,6 +53,11 @@ func (f *StringFlag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *StringFlag) GetValue() string {
Expand Down
6 changes: 6 additions & 0 deletions flag_string_slice.go
Expand Up @@ -82,6 +82,7 @@ type StringSliceFlag struct {
DefaultText string
HasBeenSet bool
Destination *StringSlice
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -115,6 +116,11 @@ func (f *StringSliceFlag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *StringSliceFlag) GetValue() string {
Expand Down
6 changes: 6 additions & 0 deletions flag_timestamp.go
Expand Up @@ -72,6 +72,7 @@ type TimestampFlag struct {
DefaultText string
HasBeenSet bool
Destination *Timestamp
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -105,6 +106,11 @@ func (f *TimestampFlag) GetUsage() string {
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
// string if the flag takes no value at all.
func (f *TimestampFlag) GetValue() string {
Expand Down
6 changes: 6 additions & 0 deletions flag_uint.go
Expand Up @@ -19,6 +19,7 @@ type UintFlag struct {
DefaultText string
Destination *uint
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -52,6 +53,11 @@ func (f *UintFlag) GetUsage() string {
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
func (f *UintFlag) IsVisible() bool {
return !f.Hidden
Expand Down
6 changes: 6 additions & 0 deletions flag_uint64.go
Expand Up @@ -19,6 +19,7 @@ type Uint64Flag struct {
DefaultText string
Destination *uint64
HasBeenSet bool
Category string
}

// IsSet returns whether or not the flag has been set through env or file
Expand Down Expand Up @@ -52,6 +53,11 @@ func (f *Uint64Flag) GetUsage() string {
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
func (f *Uint64Flag) IsVisible() bool {
return !f.Hidden
Expand Down

0 comments on commit 75e4ee6

Please sign in to comment.