Skip to content

Commit

Permalink
Remove all flag interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
dearchap committed Sep 5, 2022
1 parent ab68d8a commit 268cb97
Show file tree
Hide file tree
Showing 28 changed files with 192 additions and 262 deletions.
4 changes: 1 addition & 3 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ func (a *App) Setup() {

a.flagCategories = newFlagCategories()
for _, fl := range a.Flags {
if cf, ok := fl.(CategorizableFlag); ok {
a.flagCategories.AddFlag(cf.GetCategory(), cf)
}
a.flagCategories.AddFlag(fl.GetCategory(), fl)
}

if a.Metadata == nil {
Expand Down
4 changes: 4 additions & 0 deletions app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2268,6 +2268,10 @@ func (c *customBoolFlag) GetEnvVars() []string {
return nil
}

func (c *customBoolFlag) GetDefaultText() string {
return ""
}

func TestCustomFlagsUnused(t *testing.T) {
app := &App{
Flags: []Flag{&customBoolFlag{"custom"}},
Expand Down
18 changes: 7 additions & 11 deletions category.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ func newFlagCategories() FlagCategories {
func newFlagCategoriesFromFlags(fs []Flag) FlagCategories {
fc := newFlagCategories()
for _, fl := range fs {
if cf, ok := fl.(CategorizableFlag); ok {
fc.AddFlag(cf.GetCategory(), cf)
}
fc.AddFlag(fl.GetCategory(), fl)
}

return fc
Expand Down Expand Up @@ -138,7 +136,7 @@ type VisibleFlagCategory interface {
// Name returns the category name string
Name() string
// Flags returns a slice of VisibleFlag sorted by name
Flags() []VisibleFlag
Flags() []Flag
}

type defaultVisibleFlagCategory struct {
Expand All @@ -150,21 +148,19 @@ func (fc *defaultVisibleFlagCategory) Name() string {
return fc.name
}

func (fc *defaultVisibleFlagCategory) Flags() []VisibleFlag {
func (fc *defaultVisibleFlagCategory) Flags() []Flag {
vfNames := []string{}
for flName, fl := range fc.m {
if vf, ok := fl.(VisibleFlag); ok {
if vf.IsVisible() {
vfNames = append(vfNames, flName)
}
if fl.IsVisible() {
vfNames = append(vfNames, flName)
}
}

sort.Strings(vfNames)

ret := make([]VisibleFlag, len(vfNames))
ret := make([]Flag, len(vfNames))
for i, flName := range vfNames {
ret[i] = fc.m[flName].(VisibleFlag)
ret[i] = fc.m[flName]
}

return ret
Expand Down
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (cCtx *Context) lookupFlagSet(name string) *flag.FlagSet {
func (cCtx *Context) checkRequiredFlags(flags []Flag) requiredFlagsErr {
var missingFlags []string
for _, f := range flags {
if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() {
if f.IsRequired() {
var flagPresent bool
var flagName string

Expand Down
8 changes: 2 additions & 6 deletions docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,7 @@ func prepareFlags(
addDetails bool,
) []string {
args := []string{}
for _, f := range flags {
flag, ok := f.(DocGenerationFlag)
if !ok {
continue
}
for _, flag := range flags {
modifiedArg := opener

for _, s := range flag.Names() {
Expand Down Expand Up @@ -151,7 +147,7 @@ func prepareFlags(
}

// flagDetails returns a string containing the flags metadata
func flagDetails(flag DocGenerationFlag) string {
func flagDetails(flag Flag) string {
description := flag.GetUsage()
value := flag.GetValue()
if value != "" {
Expand Down
9 changes: 2 additions & 7 deletions fish.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,15 @@ func (a *App) prepareFishCommands(commands []*Command, allCommands *[]string, pr

func (a *App) prepareFishFlags(flags []Flag, previousCommands []string) []string {
completions := []string{}
for _, f := range flags {
flag, ok := f.(DocGenerationFlag)
if !ok {
continue
}

for _, flag := range flags {
completion := &strings.Builder{}
completion.WriteString(fmt.Sprintf(
"complete -c %s -n '%s'",
a.Name,
a.fishSubcommandHelper(previousCommands),
))

fishAddFileFlag(f, completion)
fishAddFileFlag(flag, completion)

for idx, opt := range flag.Names() {
if idx == 0 {
Expand Down
5 changes: 4 additions & 1 deletion flag-spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# `genflags.Spec` type that maps to this file structure.

flag_types:
bool: {}
bool:
no_default_text: true
float64: {}
int64: {}
int: {}
Expand All @@ -12,12 +13,14 @@ flag_types:
uint: {}

string:
no_default_text: true
struct_fields:
- { name: TakesFile, type: bool }
Generic:
struct_fields:
- { name: TakesFile, type: bool }
Path:
no_default_text: true
struct_fields:
- { name: TakesFile, type: bool }

Expand Down
47 changes: 8 additions & 39 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,38 +101,13 @@ type Flag interface {
GetUsage() string
// GetEnvVars returns the env vars for this flag
GetEnvVars() []string
}

// RequiredFlag is an interface that allows us to mark flags as required
// it allows flags required flags to be backwards compatible with the Flag interface
type RequiredFlag interface {
Flag
}

// DocGenerationFlag is an interface that allows documentation generation for the flag
type DocGenerationFlag interface {
Flag

// TakesValue returns true if the flag takes a value, otherwise false
TakesValue() bool

// GetDefaultText returns the default text for this flag
GetDefaultText() string
// GetValue returns the flags value as string representation and an empty
// string if the flag takes no value at all.
GetValue() string

// GetDefaultText returns the default text for this flag
GetDefaultText() string
}

// VisibleFlag is an interface that allows to check if a flag is visible
type VisibleFlag interface {
Flag
}

// CategorizableFlag is an interface that allows us to potentially
// use a flag in a categorized representation.
type CategorizableFlag interface {
VisibleFlag
}

func flagSet(name string, flags []Flag) (*flag.FlagSet, error) {
Expand Down Expand Up @@ -192,7 +167,7 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error {
func visibleFlags(fl []Flag) []Flag {
var visible []Flag
for _, f := range fl {
if vf, ok := f.(VisibleFlag); ok && vf.IsVisible() {
if f.IsVisible() {
visible = append(visible, f)
}
}
Expand Down Expand Up @@ -288,29 +263,23 @@ func formatDefault(format string) string {
}

func stringifyFlag(f Flag) string {
// enforce DocGeneration interface on flags to avoid reflection
df, ok := f.(DocGenerationFlag)
if !ok {
return ""
}

placeholder, usage := unquoteUsage(df.GetUsage())
needsPlaceholder := df.TakesValue()
placeholder, usage := unquoteUsage(f.GetUsage())
needsPlaceholder := f.TakesValue()

if needsPlaceholder && placeholder == "" {
placeholder = defaultPlaceholder
}

defaultValueString := ""

if s := df.GetDefaultText(); s != "" {
if s := f.GetDefaultText(); s != "" {
defaultValueString = fmt.Sprintf(formatDefault("%s"), s)
}

usageWithDefault := strings.TrimSpace(usage + defaultValueString)

return withEnvHint(df.GetEnvVars(),
fmt.Sprintf("%s\t%s", prefixedNames(df.Names(), placeholder), usageWithDefault))
return withEnvHint(f.GetEnvVars(),
fmt.Sprintf("%s\t%s", prefixedNames(f.Names(), placeholder), usageWithDefault))
}

func stringifyIntSliceFlag(f *IntSliceFlag) string {
Expand Down
8 changes: 0 additions & 8 deletions flag_duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ func (f *DurationFlag) GetValue() string {
return f.Value.String()
}

// GetDefaultText returns the default text for this flag
func (f *DurationFlag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
return f.GetValue()
}

// Apply populates the flag given the flag set and environment
func (f *DurationFlag) Apply(set *flag.FlagSet) error {
if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
Expand Down
8 changes: 0 additions & 8 deletions flag_float64.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ func (f *Float64Flag) GetValue() string {
return fmt.Sprintf("%v", f.Value)
}

// GetDefaultText returns the default text for this flag
func (f *Float64Flag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
return f.GetValue()
}

// Apply populates the flag given the flag set and environment
func (f *Float64Flag) Apply(set *flag.FlagSet) error {
if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
Expand Down
8 changes: 0 additions & 8 deletions flag_float64_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,6 @@ func (f *Float64SliceFlag) GetValue() string {
return ""
}

// GetDefaultText returns the default text for this flag
func (f *Float64SliceFlag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
return f.GetValue()
}

// Apply populates the flag given the flag set and environment
func (f *Float64SliceFlag) Apply(set *flag.FlagSet) error {
// apply any default
Expand Down
8 changes: 0 additions & 8 deletions flag_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ func (f *GenericFlag) GetValue() string {
return ""
}

// GetDefaultText returns the default text for this flag
func (f *GenericFlag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
return f.GetValue()
}

// Apply takes the flagset and calls Set on the generic flag with the value
// provided by the user for parsing by the flag
func (f GenericFlag) Apply(set *flag.FlagSet) error {
Expand Down
8 changes: 0 additions & 8 deletions flag_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ func (f *IntFlag) GetValue() string {
return fmt.Sprintf("%d", f.Value)
}

// GetDefaultText returns the default text for this flag
func (f *IntFlag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
return f.GetValue()
}

// Apply populates the flag given the flag set and environment
func (f *IntFlag) Apply(set *flag.FlagSet) error {
if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
Expand Down
8 changes: 0 additions & 8 deletions flag_int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ func (f *Int64Flag) GetValue() string {
return fmt.Sprintf("%d", f.Value)
}

// GetDefaultText returns the default text for this flag
func (f *Int64Flag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
return f.GetValue()
}

// Apply populates the flag given the flag set and environment
func (f *Int64Flag) Apply(set *flag.FlagSet) error {
if val, source, found := flagFromEnvOrFile(f.EnvVars, f.FilePath); found {
Expand Down
8 changes: 0 additions & 8 deletions flag_int64_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,6 @@ func (f *Int64SliceFlag) GetValue() string {
return ""
}

// GetDefaultText returns the default text for this flag
func (f *Int64SliceFlag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
return f.GetValue()
}

// Apply populates the flag given the flag set and environment
func (f *Int64SliceFlag) Apply(set *flag.FlagSet) error {
// apply any default
Expand Down
8 changes: 0 additions & 8 deletions flag_int_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,6 @@ func (f *IntSliceFlag) GetValue() string {
return ""
}

// GetDefaultText returns the default text for this flag
func (f *IntSliceFlag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
return f.GetValue()
}

// Apply populates the flag given the flag set and environment
func (f *IntSliceFlag) Apply(set *flag.FlagSet) error {
// apply any default
Expand Down
8 changes: 0 additions & 8 deletions flag_string_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,6 @@ func (f *StringSliceFlag) GetValue() string {
return ""
}

// GetDefaultText returns the default text for this flag
func (f *StringSliceFlag) GetDefaultText() string {
if f.DefaultText != "" {
return f.DefaultText
}
return f.GetValue()
}

// Apply populates the flag given the flag set and environment
func (f *StringSliceFlag) Apply(set *flag.FlagSet) error {
// apply any default
Expand Down
Loading

0 comments on commit 268cb97

Please sign in to comment.