change RequiredFlagsErr to RequiredFlagErr, update to struct for custom message

main
Aaron Berns 5 years ago
parent fffdd82c00
commit eb1734ba59

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

@ -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

@ -9,6 +9,12 @@ 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
@ -16,7 +22,7 @@ type BoolFlag struct {
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
@ -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
@ -93,7 +104,7 @@ type BoolTFlag struct {
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
@ -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
@ -170,7 +191,7 @@ type DurationFlag struct {
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
@ -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
@ -248,7 +279,7 @@ type Float64Flag struct {
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
@ -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
@ -326,7 +362,7 @@ type GenericFlag struct {
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
@ -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
@ -406,7 +447,7 @@ type Int64Flag struct {
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
@ -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
@ -484,7 +530,7 @@ type IntFlag struct {
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
@ -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
@ -562,7 +613,7 @@ type IntSliceFlag struct {
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
@ -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
@ -642,7 +698,7 @@ type Int64SliceFlag struct {
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
@ -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
@ -722,7 +783,7 @@ type StringFlag struct {
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
@ -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
@ -800,7 +866,7 @@ type StringSliceFlag struct {
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
@ -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
@ -880,7 +951,7 @@ type Uint64Flag struct {
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
@ -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
@ -958,7 +1034,7 @@ type UintFlag struct {
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
@ -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

Loading…
Cancel
Save