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
numberOfMissingFlags := len(missingFlagNames)
if numberOfMissingFlags == 1 {
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))
numberOfMissingReqErrFlags := len(missingFlagNamesReqErr)
if numberOfMissingFlags > 0 {
if numberOfMissingFlags == 1 {
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")
}

View File

@ -85,10 +85,12 @@ type RequiredFlag interface {
// 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
type RequiredFlagsErr interface {
type RequiredFlagErr interface {
Flag
FlagsErrRequired() bool
IsCustom() bool
GetMessage() string
HasInterpolation() bool
}
// DocGenerationFlag is an interface that allows documentation generation for the flag

View File

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