From 75e4ee69e98bcd59c97c16cdc276d0352afa5ee0 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Fri, 22 Apr 2022 15:00:43 -0400 Subject: [PATCH] Porting remainder of #796 --- app.go | 6 ++++-- category.go | 14 ++++++++------ flag.go | 10 ++++++++-- flag_bool.go | 6 ++++++ flag_duration.go | 6 ++++++ flag_float64.go | 6 ++++++ flag_float64_slice.go | 6 ++++++ flag_generic.go | 6 ++++++ flag_int.go | 6 ++++++ flag_int64.go | 6 ++++++ flag_int64_slice.go | 8 +++++++- flag_int_slice.go | 8 +++++++- flag_path.go | 6 ++++++ flag_string.go | 6 ++++++ flag_string_slice.go | 6 ++++++ flag_timestamp.go | 6 ++++++ flag_uint.go | 6 ++++++ flag_uint64.go | 6 ++++++ 18 files changed, 112 insertions(+), 12 deletions(-) diff --git a/app.go b/app.go index dd7f026498..65813c6158 100644 --- a/app.go +++ b/app.go @@ -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) diff --git a/category.go b/category.go index f9ba86a299..7580e90d6e 100644 --- a/category.go +++ b/category.go @@ -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 { @@ -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 diff --git a/flag.go b/flag.go index 60d592e52c..16778d77e0 100644 --- a/flag.go +++ b/flag.go @@ -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 @@ -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) diff --git a/flag_bool.go b/flag_bool.go index b8e625a1d1..ef5f6346f2 100644 --- a/flag_bool.go +++ b/flag_bool.go @@ -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 @@ -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 { diff --git a/flag_duration.go b/flag_duration.go index e8ca15ec02..7592180fa2 100644 --- a/flag_duration.go +++ b/flag_duration.go @@ -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 @@ -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 { diff --git a/flag_float64.go b/flag_float64.go index 0ac5b43f0d..1cda961438 100644 --- a/flag_float64.go +++ b/flag_float64.go @@ -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 @@ -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 { diff --git a/flag_float64_slice.go b/flag_float64_slice.go index 984f77f7f3..d2328b6765 100644 --- a/flag_float64_slice.go +++ b/flag_float64_slice.go @@ -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 @@ -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 { diff --git a/flag_generic.go b/flag_generic.go index d159507147..6c973f99de 100644 --- a/flag_generic.go +++ b/flag_generic.go @@ -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 @@ -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 { diff --git a/flag_int.go b/flag_int.go index 62c0848688..7979c6409a 100644 --- a/flag_int.go +++ b/flag_int.go @@ -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 @@ -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 { diff --git a/flag_int64.go b/flag_int64.go index 2f0be7aa06..7083c60c30 100644 --- a/flag_int64.go +++ b/flag_int64.go @@ -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 @@ -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 { diff --git a/flag_int64_slice.go b/flag_int64_slice.go index a53b185078..5f883bf3ec 100644 --- a/flag_int64_slice.go +++ b/flag_int64_slice.go @@ -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 @@ -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 { diff --git a/flag_int_slice.go b/flag_int_slice.go index 5f3bd88f05..1a2c679872 100644 --- a/flag_int_slice.go +++ b/flag_int_slice.go @@ -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 @@ -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 { diff --git a/flag_path.go b/flag_path.go index 4010e84c62..8a90685878 100644 --- a/flag_path.go +++ b/flag_path.go @@ -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 @@ -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 { diff --git a/flag_string.go b/flag_string.go index cd3c7dff4a..7d904a0296 100644 --- a/flag_string.go +++ b/flag_string.go @@ -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 @@ -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 { diff --git a/flag_string_slice.go b/flag_string_slice.go index 166424775f..a280505e52 100644 --- a/flag_string_slice.go +++ b/flag_string_slice.go @@ -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 @@ -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 { diff --git a/flag_timestamp.go b/flag_timestamp.go index ed06418284..14be5f8c82 100644 --- a/flag_timestamp.go +++ b/flag_timestamp.go @@ -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 @@ -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 { diff --git a/flag_uint.go b/flag_uint.go index dd10e1c01d..f7efed11a0 100644 --- a/flag_uint.go +++ b/flag_uint.go @@ -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 @@ -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 diff --git a/flag_uint64.go b/flag_uint64.go index 017db53c1d..0b3edfe9b8 100644 --- a/flag_uint64.go +++ b/flag_uint64.go @@ -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 @@ -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