Cleanup: Collapse flag interfaces

This commit is contained in:
Naveen Gogineni
2022-09-04 22:18:41 -04:00
parent 321610437e
commit ab68d8a69d
24 changed files with 491 additions and 480 deletions

View File

@@ -45,21 +45,38 @@ func (f *{{.TypeName}}) Names() []string {
return {{$.UrfaveCLINamespace}}FlagNames(f.Name, f.Aliases)
}
{{end}}{{/* /if .GenerateFlagInterface */}}
{{if .GenerateRequiredFlagInterface}}
// IsRequired returns whether or not the flag is required
func (f *{{.TypeName}}) IsRequired() bool {
return f.Required
}
{{end}}{{/* /if .GenerateRequiredFlagInterface */}}
{{if .GenerateVisibleFlagInterface}}
// IsVisible returns true if the flag is not hidden, otherwise false
func (f *{{.TypeName}}) IsVisible() bool {
return !f.Hidden
}
{{end}}{{/* /if .GenerateVisibleFlagInterface */}}
// GetCategory returns the category of the flag
func (f *{{.TypeName}}) GetCategory() string {
return f.Category
}
// GetUsage returns the usage string for the flag
func (f *{{.TypeName}}) GetUsage() string {
return f.Usage
}
// GetEnvVars returns the env vars for this flag
func (f *{{.TypeName}}) GetEnvVars() []string {
return f.EnvVars
}
// TakesValue returns true if the flag takes a value, otherwise false
func (f *{{.TypeName}}) TakesValue() bool {
return "{{.TypeName }}" != "BoolFlag"
}
{{end}}{{/* /if .GenerateFlagInterface */}}
{{end}}{{/* /range .SortedFlagTypes */}}
// vim{{/* 👻 */}}:ro

View File

@@ -10,6 +10,19 @@ func Test{{.TypeName}}_SatisfiesFlagInterface(t *testing.T) {
_ = f.IsSet()
_ = f.Names()
}
func Test{{.TypeName}}_SatisfiesRequiredFlagInterface(t *testing.T) {
var f {{$.UrfaveCLITestNamespace}}RequiredFlag = &{{$.UrfaveCLITestNamespace}}{{.TypeName}}{}
_ = f.IsRequired()
}
func Test{{.TypeName}}_SatisfiesVisibleFlagInterface(t *testing.T) {
var f {{$.UrfaveCLITestNamespace}}VisibleFlag = &{{$.UrfaveCLITestNamespace}}{{.TypeName}}{}
_ = f.IsVisible()
}
{{end}}
{{if .GenerateFmtStringerInterface}}
@@ -20,21 +33,7 @@ func Test{{.TypeName}}_SatisfiesFmtStringerInterface(t *testing.T) {
}
{{end}}
{{if .GenerateRequiredFlagInterface}}
func Test{{.TypeName}}_SatisfiesRequiredFlagInterface(t *testing.T) {
var f {{$.UrfaveCLITestNamespace}}RequiredFlag = &{{$.UrfaveCLITestNamespace}}{{.TypeName}}{}
_ = f.IsRequired()
}
{{end}}
{{if .GenerateVisibleFlagInterface}}
func Test{{.TypeName}}_SatisfiesVisibleFlagInterface(t *testing.T) {
var f {{$.UrfaveCLITestNamespace}}VisibleFlag = &{{$.UrfaveCLITestNamespace}}{{.TypeName}}{}
_ = f.IsVisible()
}
{{end}}
{{end}}
// vim{{/* 👻 */}}:ro

View File

@@ -39,10 +39,11 @@ func (gfs *Spec) SortedFlagTypes() []*FlagType {
}
type FlagTypeConfig struct {
SkipInterfaces []string `yaml:"skip_interfaces"`
StructFields []*FlagStructField `yaml:"struct_fields"`
TypeName string `yaml:"type_name"`
ValuePointer bool `yaml:"value_pointer"`
SkipInterfaces []string `yaml:"skip_interfaces"`
StructFields []*FlagStructField `yaml:"struct_fields"`
TypeName string `yaml:"type_name"`
ValuePointer bool `yaml:"value_pointer"`
DoesntNeedValue bool `yaml:"doesnt_need_value"`
}
type FlagStructField struct {
@@ -83,14 +84,6 @@ func (ft *FlagType) GenerateFlagInterface() bool {
return ft.skipInterfaceNamed("Flag")
}
func (ft *FlagType) GenerateRequiredFlagInterface() bool {
return ft.skipInterfaceNamed("RequiredFlag")
}
func (ft *FlagType) GenerateVisibleFlagInterface() bool {
return ft.skipInterfaceNamed("VisibleFlag")
}
func (ft *FlagType) skipInterfaceNamed(name string) bool {
if ft.Config == nil {
return true