change RequiredFlagsErr to RequiredFlagErr, update to struct for custom message

This commit is contained in:
Aaron Berns 2019-08-20 14:50:57 -04:00
parent fffdd82c00
commit eb1734ba59
3 changed files with 256 additions and 165 deletions

View File

@ -310,14 +310,22 @@ func (e *errRequiredFlags) Error() string {
var allErrors []string var allErrors []string
numberOfMissingFlags := len(missingFlagNames) numberOfMissingFlags := len(missingFlagNames)
if numberOfMissingFlags == 1 { numberOfMissingReqErrFlags := len(missingFlagNamesReqErr)
allErrors = append(allErrors, fmt.Sprintf("Required flag %q not set", missingFlagNames[0]))
} else { if numberOfMissingFlags > 0 {
joinedMissingFlags := strings.Join(missingFlagNames, ", ") if numberOfMissingFlags == 1 {
allErrors = append(allErrors, fmt.Sprintf("Required flags %q not set", joinedMissingFlags)) allErrors = append(allErrors, fmt.Sprintf("Required flag %q not set", missingFlagNames[0]))
} else {
joinedMissingFlags := strings.Join(missingFlagNames, ", ")
allErrors = append(allErrors, fmt.Sprintf("Required flags %q not set", joinedMissingFlags))
}
} }
// handle user defined errors and append if numberOfMissingReqErrFlags > 0 {
// handle user defined errors and append
}
return strings.Join(allErrors, "\n") return strings.Join(allErrors, "\n")
} }

View File

@ -85,10 +85,12 @@ type RequiredFlag interface {
// RequiredFlagsErr is an interface that allows users to redefine errors on required flags // RequiredFlagsErr is an interface that allows users to redefine errors on required flags
// it allows flags with user-defined errors to be backwards compatible with the Flag interface // it allows flags with user-defined errors to be backwards compatible with the Flag interface
type RequiredFlagsErr interface { type RequiredFlagErr interface {
Flag Flag
FlagsErrRequired() bool IsCustom() bool
GetMessage() string
HasInterpolation() bool
} }
// DocGenerationFlag is an interface that allows documentation generation for the flag // DocGenerationFlag is an interface that allows documentation generation for the flag

View File

@ -9,17 +9,23 @@ import (
"time" "time"
) )
type FlagErr struct {
Custom bool
Message string
Interpolate bool
}
// BoolFlag is a flag with type bool // BoolFlag is a flag with type bool
type BoolFlag struct { type BoolFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Destination *bool Destination *bool
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -38,9 +44,14 @@ func (f BoolFlag) IsRequired() bool {
return f.Required return f.Required
} }
// FlagsErrRequired returns whether or not the flag is required // IsCustom returns whether or not the required flag has a custom errorj
func (f BoolFlag) FlagsErrRequired() bool { func (f BoolFlag) IsCustom() bool {
return f.RequiredFlagsErr return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f BoolFlag) GetMessage() string {
return f.RequiredFlagErr.Message
} }
// TakesValue returns true of the flag takes a value, otherwise false // TakesValue returns true of the flag takes a value, otherwise false
@ -88,15 +99,15 @@ func lookupBool(name string, set *flag.FlagSet) bool {
// BoolTFlag is a flag with type bool that is true by default // BoolTFlag is a flag with type bool that is true by default
type BoolTFlag struct { type BoolTFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Destination *bool Destination *bool
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -115,6 +126,16 @@ func (f BoolTFlag) IsRequired() bool {
return f.Required return f.Required
} }
// IsCustom returns whether or not the required flag has a custom errorj
func (f BoolTFlag) IsCustom() bool {
return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f BoolTFlag) GetMessage() string {
return f.RequiredFlagErr.Message
}
// FlagsErrRequired returns whether or not the flag is required // FlagsErrRequired returns whether or not the flag is required
func (f BoolTFlag) FlagsErrRequired() bool { func (f BoolTFlag) FlagsErrRequired() bool {
return f.RequiredFlagsErr return f.RequiredFlagsErr
@ -165,16 +186,16 @@ func lookupBoolT(name string, set *flag.FlagSet) bool {
// DurationFlag is a flag with type time.Duration (see https://golang.org/pkg/time/#ParseDuration) // DurationFlag is a flag with type time.Duration (see https://golang.org/pkg/time/#ParseDuration)
type DurationFlag struct { type DurationFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Value time.Duration Value time.Duration
Destination *time.Duration Destination *time.Duration
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -193,6 +214,16 @@ func (f DurationFlag) IsRequired() bool {
return f.Required return f.Required
} }
// IsCustom returns whether or not the required flag has a custom errorj
func (f DurationFlag) IsCustom() bool {
return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f DurationFlag) GetMessage() string {
return f.RequiredFlagErr.Message
}
// FlagsErrRequired returns whether or not the flag is required // FlagsErrRequired returns whether or not the flag is required
func (f DurationFlag) FlagsErrRequired() bool { func (f DurationFlag) FlagsErrRequired() bool {
return f.RequiredFlagsErr return f.RequiredFlagsErr
@ -243,16 +274,16 @@ func lookupDuration(name string, set *flag.FlagSet) time.Duration {
// Float64Flag is a flag with type float64 // Float64Flag is a flag with type float64
type Float64Flag struct { type Float64Flag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Value float64 Value float64
Destination *float64 Destination *float64
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -271,9 +302,14 @@ func (f Float64Flag) IsRequired() bool {
return f.Required return f.Required
} }
// FlagsErrRequired returns whether or not the flag is required // IsCustom returns whether or not the required flag has a custom errorj
func (f Float64Flag) FlagsErrRequired() bool { func (f Float64Flag) IsCustom() bool {
return f.RequiredFlagsErr return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f Float64Flag) GetMessage() string {
return f.RequiredFlagErr.Message
} }
// TakesValue returns true of the flag takes a value, otherwise false // TakesValue returns true of the flag takes a value, otherwise false
@ -321,15 +357,15 @@ func lookupFloat64(name string, set *flag.FlagSet) float64 {
// GenericFlag is a flag with type Generic // GenericFlag is a flag with type Generic
type GenericFlag struct { type GenericFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Value Generic Value Generic
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -348,9 +384,14 @@ func (f GenericFlag) IsRequired() bool {
return f.Required return f.Required
} }
// FlagsErrRequired returns whether or not the flag is required // IsCustom returns whether or not the required flag has a custom errorj
func (f GenericFlag) FlagsErrRequired() bool { func (f GenericFlag) IsCustom() bool {
return f.RequiredFlagsErr return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f GenericFlag) GetMessage() string {
return f.RequiredFlagErr.Message
} }
// TakesValue returns true of the flag takes a value, otherwise false // TakesValue returns true of the flag takes a value, otherwise false
@ -401,16 +442,16 @@ func lookupGeneric(name string, set *flag.FlagSet) interface{} {
// Int64Flag is a flag with type int64 // Int64Flag is a flag with type int64
type Int64Flag struct { type Int64Flag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Value int64 Value int64
Destination *int64 Destination *int64
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -429,9 +470,14 @@ func (f Int64Flag) IsRequired() bool {
return f.Required return f.Required
} }
// FlagsErrRequired returns whether or not the flag is required // IsCustom returns whether or not the required flag has a custom errorj
func (f Int64Flag) FlagsErrRequired() bool { func (f Int64Flag) IsCustom() bool {
return f.RequiredFlagsErr return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f Int64Flag) GetMessage() string {
return f.RequiredFlagErr.Message
} }
// TakesValue returns true of the flag takes a value, otherwise false // TakesValue returns true of the flag takes a value, otherwise false
@ -479,16 +525,16 @@ func lookupInt64(name string, set *flag.FlagSet) int64 {
// IntFlag is a flag with type int // IntFlag is a flag with type int
type IntFlag struct { type IntFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Value int Value int
Destination *int Destination *int
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -507,9 +553,14 @@ func (f IntFlag) IsRequired() bool {
return f.Required return f.Required
} }
// FlagsErrRequired returns whether or not the flag is required // IsCustom returns whether or not the required flag has a custom errorj
func (f IntFlag) FlagsErrRequired() bool { func (f IntFlag) IsCustom() bool {
return f.RequiredFlagsErr return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f IntFlag) GetMessage() string {
return f.RequiredFlagErr.Message
} }
// TakesValue returns true of the flag takes a value, otherwise false // TakesValue returns true of the flag takes a value, otherwise false
@ -557,15 +608,15 @@ func lookupInt(name string, set *flag.FlagSet) int {
// IntSliceFlag is a flag with type *IntSlice // IntSliceFlag is a flag with type *IntSlice
type IntSliceFlag struct { type IntSliceFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Value *IntSlice Value *IntSlice
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -584,9 +635,14 @@ func (f IntSliceFlag) IsRequired() bool {
return f.Required return f.Required
} }
// FlagsErrRequired returns whether or not the flag is required // IsCustom returns whether or not the required flag has a custom errorj
func (f IntSliceFlag) FlagsErrRequired() bool { func (f IntSliceFlag) IsCustom() bool {
return f.RequiredFlagsErr return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f IntSliceFlag) GetMessage() string {
return f.RequiredFlagErr.Message
} }
// TakesValue returns true of the flag takes a value, otherwise false // TakesValue returns true of the flag takes a value, otherwise false
@ -637,15 +693,15 @@ func lookupIntSlice(name string, set *flag.FlagSet) []int {
// Int64SliceFlag is a flag with type *Int64Slice // Int64SliceFlag is a flag with type *Int64Slice
type Int64SliceFlag struct { type Int64SliceFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Value *Int64Slice Value *Int64Slice
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -664,9 +720,14 @@ func (f Int64SliceFlag) IsRequired() bool {
return f.Required return f.Required
} }
// FlagsErrRequired returns whether or not the flag is required // IsCustom returns whether or not the required flag has a custom errorj
func (f Int64SliceFlag) FlagsErrRequired() bool { func (f Int64SliceFlag) IsCustom() bool {
return f.RequiredFlagsErr return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f Int64SliceFlag) GetMessage() string {
return f.RequiredFlagErr.Message
} }
// TakesValue returns true of the flag takes a value, otherwise false // TakesValue returns true of the flag takes a value, otherwise false
@ -717,16 +778,16 @@ func lookupInt64Slice(name string, set *flag.FlagSet) []int64 {
// StringFlag is a flag with type string // StringFlag is a flag with type string
type StringFlag struct { type StringFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Value string Value string
Destination *string Destination *string
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -745,9 +806,14 @@ func (f StringFlag) IsRequired() bool {
return f.Required return f.Required
} }
// FlagsErrRequired returns whether or not the flag is required // IsCustom returns whether or not the required flag has a custom errorj
func (f StringFlag) FlagsErrRequired() bool { func (f StringFlag) IsCustom() bool {
return f.RequiredFlagsErr return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f StringFlag) GetMessage() string {
return f.RequiredFlagErr.Message
} }
// TakesValue returns true of the flag takes a value, otherwise false // TakesValue returns true of the flag takes a value, otherwise false
@ -795,15 +861,15 @@ func lookupString(name string, set *flag.FlagSet) string {
// StringSliceFlag is a flag with type *StringSlice // StringSliceFlag is a flag with type *StringSlice
type StringSliceFlag struct { type StringSliceFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Value *StringSlice Value *StringSlice
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -822,9 +888,14 @@ func (f StringSliceFlag) IsRequired() bool {
return f.Required return f.Required
} }
// FlagsErrRequired returns whether or not the flag is required // IsCustom returns whether or not the required flag has a custom errorj
func (f StringSliceFlag) FlagsErrRequired() bool { func (f StringSliceFlag) IsCustom() bool {
return f.RequiredFlagsErr return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f StringSliceFlag) GetMessage() string {
return f.RequiredFlagErr.Message
} }
// TakesValue returns true of the flag takes a value, otherwise false // TakesValue returns true of the flag takes a value, otherwise false
@ -875,16 +946,16 @@ func lookupStringSlice(name string, set *flag.FlagSet) []string {
// Uint64Flag is a flag with type uint64 // Uint64Flag is a flag with type uint64
type Uint64Flag struct { type Uint64Flag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Value uint64 Value uint64
Destination *uint64 Destination *uint64
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -903,9 +974,14 @@ func (f Uint64Flag) IsRequired() bool {
return f.Required return f.Required
} }
// FlagsErrRequired returns whether or not the flag is required // IsCustom returns whether or not the required flag has a custom errorj
func (f Uint64Flag) FlagsErrRequired() bool { func (f Uint64Flag) IsCustom() bool {
return f.RequiredFlagsErr return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f Uint64Flag) GetMessage() string {
return f.RequiredFlagErr.Message
} }
// TakesValue returns true of the flag takes a value, otherwise false // TakesValue returns true of the flag takes a value, otherwise false
@ -953,16 +1029,16 @@ func lookupUint64(name string, set *flag.FlagSet) uint64 {
// UintFlag is a flag with type uint // UintFlag is a flag with type uint
type UintFlag struct { type UintFlag struct {
Name string Name string
Usage string Usage string
EnvVar string EnvVar string
FilePath string FilePath string
Required bool Required bool
RequiredFlagsErr bool RequiredFlagErr FlagErr
Hidden bool Hidden bool
TakesFile bool TakesFile bool
Value uint Value uint
Destination *uint Destination *uint
} }
// String returns a readable representation of this value // String returns a readable representation of this value
@ -981,9 +1057,14 @@ func (f UintFlag) IsRequired() bool {
return f.Required return f.Required
} }
// FlagsErrRequired returns whether or not the flag is required // IsCustom returns whether or not the required flag has a custom errorj
func (f UintFlag) FlagsErrRequired() bool { func (f UintFlag) IsCustom() bool {
return f.RequiredFlagsErr return f.RequiredFlagErr.Custom
}
// GetMessage returns the custom error message
func (f UintFlag) GetMessage() string {
return f.RequiredFlagErr.Message
} }
// TakesValue returns true of the flag takes a value, otherwise false // TakesValue returns true of the flag takes a value, otherwise false