From 95f45c1e60e67a27f7095088e67196eb323628fd Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Mon, 19 Aug 2019 12:06:19 -0400 Subject: [PATCH 01/11] add RequiredFlagsErr interface --- flag.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/flag.go b/flag.go index be55a3307b..abee563551 100644 --- a/flag.go +++ b/flag.go @@ -83,6 +83,14 @@ type RequiredFlag interface { IsRequired() bool } +// 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 { + Flag + + IsRequired() bool +} + // DocGenerationFlag is an interface that allows documentation generation for the flag type DocGenerationFlag interface { Flag From a064d9082cc67b74d823c4e75e2a5fe6ccd73ae0 Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Tue, 20 Aug 2019 08:56:35 -0400 Subject: [PATCH 02/11] manually add FlagsErrRequired field and function to generated flags --- flag.go | 2 +- flag_generated.go | 301 +++++++++++++++++++++++++++++----------------- 2 files changed, 190 insertions(+), 113 deletions(-) diff --git a/flag.go b/flag.go index abee563551..868680f5f5 100644 --- a/flag.go +++ b/flag.go @@ -88,7 +88,7 @@ type RequiredFlag interface { type RequiredFlagsErr interface { Flag - IsRequired() bool + FlagsErrRequired() bool } // DocGenerationFlag is an interface that allows documentation generation for the flag diff --git a/flag_generated.go b/flag_generated.go index bae7d123f6..4d9ed7ef3d 100644 --- a/flag_generated.go +++ b/flag_generated.go @@ -11,14 +11,15 @@ import ( // BoolFlag is a flag with type bool type BoolFlag struct { - Name string - Usage string - EnvVar string - FilePath string - Required bool - Hidden bool - TakesFile bool - Destination *bool + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Destination *bool } // String returns a readable representation of this value @@ -37,6 +38,11 @@ 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 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f BoolFlag) TakesValue() bool { return false @@ -82,14 +88,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 - Hidden bool - TakesFile bool - Destination *bool + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Destination *bool } // String returns a readable representation of this value @@ -108,6 +115,11 @@ func (f BoolTFlag) IsRequired() bool { return f.Required } +// FlagsErrRequired returns whether or not the flag is required +func (f BoolTFlag) FlagsErrRequired() bool { + return f.RequiredFlagsErr +} + // TakesValue returns true of the flag takes a value, otherwise false func (f BoolTFlag) TakesValue() bool { return false @@ -153,15 +165,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 - Hidden bool - TakesFile bool - Value time.Duration - Destination *time.Duration + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Value time.Duration + Destination *time.Duration } // String returns a readable representation of this value @@ -180,6 +193,11 @@ func (f DurationFlag) IsRequired() bool { return f.Required } +// FlagsErrRequired returns whether or not the flag is required +func (f DurationFlag) FlagsErrRequired() bool { + return f.RequiredFlagsErr +} + // TakesValue returns true of the flag takes a value, otherwise false func (f DurationFlag) TakesValue() bool { return true @@ -225,15 +243,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 - Hidden bool - TakesFile bool - Value float64 - Destination *float64 + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Value float64 + Destination *float64 } // String returns a readable representation of this value @@ -252,6 +271,11 @@ 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 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f Float64Flag) TakesValue() bool { return true @@ -297,14 +321,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 - Hidden bool - TakesFile bool - Value Generic + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Value Generic } // String returns a readable representation of this value @@ -323,6 +348,11 @@ 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 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f GenericFlag) TakesValue() bool { return true @@ -371,15 +401,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 - Hidden bool - TakesFile bool - Value int64 - Destination *int64 + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Value int64 + Destination *int64 } // String returns a readable representation of this value @@ -398,6 +429,11 @@ 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 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f Int64Flag) TakesValue() bool { return true @@ -443,15 +479,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 - Hidden bool - TakesFile bool - Value int - Destination *int + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Value int + Destination *int } // String returns a readable representation of this value @@ -470,6 +507,11 @@ 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 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f IntFlag) TakesValue() bool { return true @@ -515,14 +557,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 - Hidden bool - TakesFile bool - Value *IntSlice + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Value *IntSlice } // String returns a readable representation of this value @@ -541,6 +584,11 @@ 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 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f IntSliceFlag) TakesValue() bool { return true @@ -589,14 +637,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 - Hidden bool - TakesFile bool - Value *Int64Slice + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Value *Int64Slice } // String returns a readable representation of this value @@ -615,6 +664,11 @@ 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 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f Int64SliceFlag) TakesValue() bool { return true @@ -663,15 +717,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 - Hidden bool - TakesFile bool - Value string - Destination *string + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Value string + Destination *string } // String returns a readable representation of this value @@ -690,6 +745,11 @@ 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 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f StringFlag) TakesValue() bool { return true @@ -735,14 +795,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 - Hidden bool - TakesFile bool - Value *StringSlice + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Value *StringSlice } // String returns a readable representation of this value @@ -761,6 +822,11 @@ 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 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f StringSliceFlag) TakesValue() bool { return true @@ -809,15 +875,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 - Hidden bool - TakesFile bool - Value uint64 - Destination *uint64 + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Value uint64 + Destination *uint64 } // String returns a readable representation of this value @@ -836,6 +903,11 @@ 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 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f Uint64Flag) TakesValue() bool { return true @@ -881,15 +953,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 - Hidden bool - TakesFile bool - Value uint - Destination *uint + Name string + Usage string + EnvVar string + FilePath string + Required bool + RequiredFlagsErr bool + Hidden bool + TakesFile bool + Value uint + Destination *uint } // String returns a readable representation of this value @@ -908,6 +981,11 @@ 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 +} + // TakesValue returns true of the flag takes a value, otherwise false func (f UintFlag) TakesValue() bool { return true @@ -950,4 +1028,3 @@ func lookupUint(name string, set *flag.FlagSet) uint { } return 0 } - From addd467e5b30964f47a54ceac0dfcb689b0b1928 Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Tue, 20 Aug 2019 10:03:29 -0400 Subject: [PATCH 03/11] update checkRequiredFlags function to check RequiredFlagsErr field and return map of missing flag names with matching bool for RequiredFlagsErr value --- context.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index db7cd6977e..85ba419039 100644 --- a/context.go +++ b/context.go @@ -310,12 +310,16 @@ func (e *errRequiredFlags) getMissingFlags() []string { } func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr { - var missingFlags []string + missingFlags := make(map[string]bool) for _, f := range flags { if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() { key := strings.Split(f.GetName(), ",")[0] if !context.IsSet(key) { - missingFlags = append(missingFlags, key) + if re, ok := f.(RequiredFlagsErr); ok && re.FlagsErrRequired() { + missingFlags[key] = true + } else { + missingFlags[key] = false + } } } } From ed4ac1876d5eaa4b961c480a51b941676efd03d4 Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Tue, 20 Aug 2019 10:39:55 -0400 Subject: [PATCH 04/11] update requiredFlagsErr interface, errRequiredFlags struct and associated functions to handle map --- context.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/context.go b/context.go index 85ba419039..4273389078 100644 --- a/context.go +++ b/context.go @@ -289,23 +289,34 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error { type requiredFlagsErr interface { error - getMissingFlags() []string + getMissingFlags() map[string]bool } type errRequiredFlags struct { - missingFlags []string + missingFlags map[string]bool } func (e *errRequiredFlags) Error() string { - numberOfMissingFlags := len(e.missingFlags) + var missingFlagNames []string + var missingFlagNamesReqErr []string + + for k, v := range e.missingFlags { + if v == false { + missingFlagNames = append(missingFlagNames, k) + } else { + missingFlagNamesReqErr = append(missingFlagNamesReqErr, k) + } + } + + numberOfMissingFlags := len(missingFlagNames) if numberOfMissingFlags == 1 { - return fmt.Sprintf("Required flag %q not set", e.missingFlags[0]) + return fmt.Sprintf("Required flag %q not set", missingFlagNames[0]) } - joinedMissingFlags := strings.Join(e.missingFlags, ", ") + joinedMissingFlags := strings.Join(missingFlagNames, ", ") return fmt.Sprintf("Required flags %q not set", joinedMissingFlags) } -func (e *errRequiredFlags) getMissingFlags() []string { +func (e *errRequiredFlags) getMissingFlags() map[string]bool { return e.missingFlags } From fffdd82c00a338f427815ef8844cf5135e49e1f4 Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Tue, 20 Aug 2019 11:09:53 -0400 Subject: [PATCH 05/11] add ability to combine multiple error strings to Error() function --- context.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/context.go b/context.go index 4273389078..11bb4f181b 100644 --- a/context.go +++ b/context.go @@ -308,12 +308,18 @@ func (e *errRequiredFlags) Error() string { } } + var allErrors []string numberOfMissingFlags := len(missingFlagNames) if numberOfMissingFlags == 1 { - return fmt.Sprintf("Required flag %q not set", missingFlagNames[0]) + 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)) } - joinedMissingFlags := strings.Join(missingFlagNames, ", ") - return fmt.Sprintf("Required flags %q not set", joinedMissingFlags) + + // handle user defined errors and append + + return strings.Join(allErrors, "\n") } func (e *errRequiredFlags) getMissingFlags() map[string]bool { From eb1734ba59120625817e9c3e3d4ff497090fbe71 Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Tue, 20 Aug 2019 14:50:57 -0400 Subject: [PATCH 06/11] change RequiredFlagsErr to RequiredFlagErr, update to struct for custom message --- context.go | 20 ++- flag.go | 6 +- flag_generated.go | 395 ++++++++++++++++++++++++++++------------------ 3 files changed, 256 insertions(+), 165 deletions(-) diff --git a/context.go b/context.go index 11bb4f181b..0fc153258f 100644 --- a/context.go +++ b/context.go @@ -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") } diff --git a/flag.go b/flag.go index 868680f5f5..1f77c3ea56 100644 --- a/flag.go +++ b/flag.go @@ -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 diff --git a/flag_generated.go b/flag_generated.go index 4d9ed7ef3d..8cc022db43 100644 --- a/flag_generated.go +++ b/flag_generated.go @@ -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 From 52a016034a3efb885cd7f1a269d6be4581a9ccf1 Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Tue, 20 Aug 2019 15:39:18 -0400 Subject: [PATCH 07/11] adjust custom-error related interface, struct and methods to reflect change in RequiredFlag interface --- context.go | 55 +++++++++++++++++++++++------------------------ flag.go | 3 +-- flag_generated.go | 15 ++----------- 3 files changed, 30 insertions(+), 43 deletions(-) diff --git a/context.go b/context.go index 0fc153258f..2daa2e7115 100644 --- a/context.go +++ b/context.go @@ -289,68 +289,67 @@ func normalizeFlags(flags []Flag, set *flag.FlagSet) error { type requiredFlagsErr interface { error - getMissingFlags() map[string]bool + getMissingDefaultFlags() []string + getMissingCustomFlags() []string } type errRequiredFlags struct { - missingFlags map[string]bool + missingDefaultFlags []string + missingCustomFlags []string } func (e *errRequiredFlags) Error() string { - var missingFlagNames []string - var missingFlagNamesReqErr []string - - for k, v := range e.missingFlags { - if v == false { - missingFlagNames = append(missingFlagNames, k) - } else { - missingFlagNamesReqErr = append(missingFlagNamesReqErr, k) - } - } - var allErrors []string - numberOfMissingFlags := len(missingFlagNames) - numberOfMissingReqErrFlags := len(missingFlagNamesReqErr) + numberOfMissingFlags := len(e.missingDefaultFlags) + numberOfMissingReqErrFlags := len(e.missingCustomFlags) if numberOfMissingFlags > 0 { 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", e.missingDefaultFlags[0])) } else { - joinedMissingFlags := strings.Join(missingFlagNames, ", ") + joinedMissingFlags := strings.Join(e.missingDefaultFlags, ", ") allErrors = append(allErrors, fmt.Sprintf("Required flags %q not set", joinedMissingFlags)) } } if numberOfMissingReqErrFlags > 0 { - - // handle user defined errors and append - + for i := range e.missingCustomFlags { + allErrors = append(allErrors, e.missingCustomFlags[i]) + } } return strings.Join(allErrors, "\n") } -func (e *errRequiredFlags) getMissingFlags() map[string]bool { - return e.missingFlags +func (e *errRequiredFlags) getMissingDefaultFlags() []string { + return e.missingDefaultFlags +} + +func (e *errRequiredFlags) getMissingCustomFlags() []string { + return e.missingCustomFlags } func checkRequiredFlags(flags []Flag, context *Context) requiredFlagsErr { - missingFlags := make(map[string]bool) + var missingDefaultFlags []string + var missingCustomFlags []string for _, f := range flags { if rf, ok := f.(RequiredFlag); ok && rf.IsRequired() { key := strings.Split(f.GetName(), ",")[0] if !context.IsSet(key) { - if re, ok := f.(RequiredFlagsErr); ok && re.FlagsErrRequired() { - missingFlags[key] = true + if re, ok := f.(RequiredFlagErr); ok && re.IsCustom() { + missingCustomFlags = append(missingCustomFlags, re.GetMessage()) } else { - missingFlags[key] = false + missingDefaultFlags = append(missingDefaultFlags, key) } } } } - if len(missingFlags) != 0 { - return &errRequiredFlags{missingFlags: missingFlags} + if len(missingDefaultFlags) != 0 || len(missingCustomFlags) != 0 { + return &errRequiredFlags{ + missingDefaultFlags: missingDefaultFlags, + missingCustomFlags: missingCustomFlags, + } } return nil diff --git a/flag.go b/flag.go index 1f77c3ea56..be6b1842d9 100644 --- a/flag.go +++ b/flag.go @@ -83,14 +83,13 @@ type RequiredFlag interface { IsRequired() bool } -// RequiredFlagsErr is an interface that allows users to redefine errors on required flags +// RequiredFlagErr 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 RequiredFlagErr interface { Flag IsCustom() bool GetMessage() string - HasInterpolation() bool } // DocGenerationFlag is an interface that allows documentation generation for the flag diff --git a/flag_generated.go b/flag_generated.go index 8cc022db43..c4d1591b98 100644 --- a/flag_generated.go +++ b/flag_generated.go @@ -10,9 +10,8 @@ import ( ) type FlagErr struct { - Custom bool - Message string - Interpolate bool + Custom bool + Message string } // BoolFlag is a flag with type bool @@ -136,11 +135,6 @@ 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 -} - // TakesValue returns true of the flag takes a value, otherwise false func (f BoolTFlag) TakesValue() bool { return false @@ -224,11 +218,6 @@ 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 -} - // TakesValue returns true of the flag takes a value, otherwise false func (f DurationFlag) TakesValue() bool { return true From f8eb02c99d101961632763a4a2e24ede8886c365 Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Fri, 30 Aug 2019 13:02:26 -0400 Subject: [PATCH 08/11] add required flag and custom required flag error message example to README --- README.md | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/README.md b/README.md index 29ed171da7..60ac106cc0 100644 --- a/README.md +++ b/README.md @@ -560,6 +560,85 @@ Will result in help output like: --lang value, -l value Language for the greeting (default: "english") ``` +#### Required Flags + +You can make a flag required by setting the `Required` field to `true`. If an end-user +fails to provide a required flag, they will be shown a default error message, or a +custom message if one is defined. To define a custom error message for a required flag, +set the `RequiredFlagErr` field equal to a `cli.FlagErr` struct with a `Custom` field of `true` +and a `Message` field containing the custom error message. Default and custom error messages +can be mixed with default messages displayed before custom messages. + +For example this: + +```go +package main + +import ( + "log" + "os" + "strings" + + "../cli" +) + +func main() { + app := cli.NewApp() + + app.Flags = []cli.Flag { + cli.StringFlag{ + Name: "lang", + Value: "english", + Usage: "language for the greeting", + Required: true, + RequiredFlagErr: cli.FlagErr{ + Custom: true, + Message: `There's a problem: "lang" is a required flag` , + }, + }, + cli.StringFlag{ + Name: "mood", + Value: "normal", + Usage: "emphasis for the greeting", + Required: true, + }, + } + + app.Action = func(c *cli.Context) error { + name := "Nefertiti" + if c.NArg() > 0 { + name = c.Args().Get(0) + } + var output string + if c.String("lang") == "spanish" { + output = "Hola " + name + } else { + output = "Hello " + name + } + + if c.String("mood") == "excited" { + output = strings.ToUpper(output) + } + + return nil + } + + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } +} +``` + +Creates an app that requires both `lang` and `mood` flags. If an attempt +to run the app is made without either, the end-user will see the following +prompts: + +``` +Required flag "mood" not set +There's a problem: "lang" is a required flag +``` + #### Values from the Environment You can also have the default value set from the environment via `EnvVar`. e.g. From 4d4a5d3b38680b63d09456c40e233e6a4b0f009b Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Mon, 9 Sep 2019 17:01:24 -0400 Subject: [PATCH 09/11] add tests for custom error messages --- app_test.go | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/app_test.go b/app_test.go index bccede4959..cbb6d1e488 100644 --- a/app_test.go +++ b/app_test.go @@ -1084,6 +1084,33 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) { }}, expectedAnError: true, }, + { + testCase: "error_case_empty_input_with_required_flag_and_custom_error_message_on_app", + appRunInput: []string{"myCLI"}, + appFlags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}}, + expectedAnError: true, + }, + { + testCase: "error_case_empty_input_with_required_flag_and_custom_error_message_on_command", + appRunInput: []string{"myCLI", "myCommand"}, + appCommands: []Command{{ + Name: "myCommand", + Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}}, + }}, + expectedAnError: true, + }, + { + testCase: "error_case_empty_input_with_required_flag_and_custom_error_message_on_subcommand", + appRunInput: []string{"myCLI", "myCommand", "mySubCommand"}, + appCommands: []Command{{ + Name: "myCommand", + Subcommands: []Command{{ + Name: "mySubCommand", + Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}}, + }}, + }}, + expectedAnError: true, + }, // assertion: inputing --help, when a required flag is present, does not error { testCase: "valid_case_help_input_with_required_flag_on_app", @@ -1109,6 +1136,30 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) { }}, }}, }, + { + testCase: "valid_case_help_input_with_required_flag_and_custom_error_message_on_app", + appRunInput: []string{"myCLI", "--help"}, + appFlags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}}, + }, + { + testCase: "valid_case_help_input_with_required_flag_and_custom_error_message_on_command", + appRunInput: []string{"myCLI", "myCommand", "--help"}, + appCommands: []Command{{ + Name: "myCommand", + Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}}, + }}, + }, + { + testCase: "valid_case_help_input_with_required_flag_and_custom_error_message_on_subcommand", + appRunInput: []string{"myCLI", "myCommand", "mySubCommand", "--help"}, + appCommands: []Command{{ + Name: "myCommand", + Subcommands: []Command{{ + Name: "mySubCommand", + Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}}, + }}, + }}, + }, // assertion: giving optional input, when a required flag is present, errors { testCase: "error_case_optional_input_with_required_flag_on_app", @@ -1137,6 +1188,33 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) { }}, expectedAnError: true, }, + { + testCase: "error_case_optional_input_with_required_flag_and_custom_error_message_on_app", + appRunInput: []string{"myCLI", "--optional", "cats"}, + appFlags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}, StringFlag{Name: "optional"}}, + expectedAnError: true, + }, + { + testCase: "error_case_optional_input_with_required_flag_and_custom_error_message_on_command", + appRunInput: []string{"myCLI", "myCommand", "--optional", "cats"}, + appCommands: []Command{{ + Name: "myCommand", + Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}, StringFlag{Name: "optional"}}, + }}, + expectedAnError: true, + }, + { + testCase: "error_case_optional_input_with_required_flag_and_custom_error_message_on_subcommand", + appRunInput: []string{"myCLI", "myCommand", "mySubCommand", "--optional", "cats"}, + appCommands: []Command{{ + Name: "myCommand", + Subcommands: []Command{{ + Name: "mySubCommand", + Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}, StringFlag{Name: "optional"}}, + }}, + }}, + expectedAnError: true, + }, // assertion: when a required flag is present, inputting that required flag does not error { testCase: "valid_case_required_flag_input_on_app", @@ -1162,6 +1240,30 @@ func TestRequiredFlagAppRunBehavior(t *testing.T) { }}, }}, }, + { + testCase: "valid_case_required_flag_input_on_app_with_custom_error_message", + appRunInput: []string{"myCLI", "--requiredFlag", "cats"}, + appFlags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}, StringFlag{Name: "optional"}}, + }, + { + testCase: "valid_case_required_flag_input_on_command_with_custom_error_message", + appRunInput: []string{"myCLI", "myCommand", "--requiredFlag", "cats"}, + appCommands: []Command{{ + Name: "myCommand", + Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}, StringFlag{Name: "optional"}}, + }}, + }, + { + testCase: "valid_case_required_flag_input_on_subcommand_with_custom_error_message", + appRunInput: []string{"myCLI", "myCommand", "mySubCommand", "--requiredFlag", "cats"}, + appCommands: []Command{{ + Name: "myCommand", + Subcommands: []Command{{ + Name: "mySubCommand", + Flags: []Flag{StringFlag{Name: "requiredFlag", Required: true, RequiredFlagErr: FlagErr{Custom: true, Message:`custom error`}}, StringFlag{Name: "optional"}}, + }}, + }}, + }, } for _, test := range tdata { t.Run(test.testCase, func(t *testing.T) { From b837236bd53796d1c738282d3ada0f9ca45df3ef Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Tue, 10 Sep 2019 08:50:07 -0400 Subject: [PATCH 10/11] fix import url in README and add flag_generated.go to list of ingored files --- .gitignore | 3 ++- README.md | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 7a7e2d9ef0..a9c49ca3ff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.coverprofile node_modules/ -vendor \ No newline at end of file +vendor +flag_generated.go \ No newline at end of file diff --git a/README.md b/README.md index 60ac106cc0..0cd124fc91 100644 --- a/README.md +++ b/README.md @@ -579,7 +579,7 @@ import ( "os" "strings" - "../cli" + "github.com/urfave/cli" ) func main() { From ee2dd885a6d791f11fbb256e41464eea5df6e15b Mon Sep 17 00:00:00 2001 From: Aaron Berns Date: Tue, 10 Sep 2019 08:53:36 -0400 Subject: [PATCH 11/11] remove flag_generated.go from added files --- altsrc/flag_generated.go | 346 ------------ flag_generated.go | 1100 -------------------------------------- 2 files changed, 1446 deletions(-) delete mode 100644 altsrc/flag_generated.go delete mode 100644 flag_generated.go diff --git a/altsrc/flag_generated.go b/altsrc/flag_generated.go deleted file mode 100644 index e568872143..0000000000 --- a/altsrc/flag_generated.go +++ /dev/null @@ -1,346 +0,0 @@ -// Code generated by fg; DO NOT EDIT. - -package altsrc - -import ( - "flag" - "github.com/urfave/cli" -) - -// BoolFlag is the flag type that wraps cli.BoolFlag to allow -// for other values to be specified -type BoolFlag struct { - cli.BoolFlag - set *flag.FlagSet -} - -// NewBoolFlag creates a new BoolFlag -func NewBoolFlag(fl cli.BoolFlag) *BoolFlag { - return &BoolFlag{ BoolFlag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped BoolFlag.Apply -func (f *BoolFlag) Apply(set *flag.FlagSet) { - f.set = set - f.BoolFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped BoolFlag.ApplyWithError -func (f *BoolFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.BoolFlag.ApplyWithError(set) -} - -// BoolTFlag is the flag type that wraps cli.BoolTFlag to allow -// for other values to be specified -type BoolTFlag struct { - cli.BoolTFlag - set *flag.FlagSet -} - -// NewBoolTFlag creates a new BoolTFlag -func NewBoolTFlag(fl cli.BoolTFlag) *BoolTFlag { - return &BoolTFlag{ BoolTFlag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped BoolTFlag.Apply -func (f *BoolTFlag) Apply(set *flag.FlagSet) { - f.set = set - f.BoolTFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped BoolTFlag.ApplyWithError -func (f *BoolTFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.BoolTFlag.ApplyWithError(set) -} - -// DurationFlag is the flag type that wraps cli.DurationFlag to allow -// for other values to be specified -type DurationFlag struct { - cli.DurationFlag - set *flag.FlagSet -} - -// NewDurationFlag creates a new DurationFlag -func NewDurationFlag(fl cli.DurationFlag) *DurationFlag { - return &DurationFlag{ DurationFlag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped DurationFlag.Apply -func (f *DurationFlag) Apply(set *flag.FlagSet) { - f.set = set - f.DurationFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped DurationFlag.ApplyWithError -func (f *DurationFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.DurationFlag.ApplyWithError(set) -} - -// Float64Flag is the flag type that wraps cli.Float64Flag to allow -// for other values to be specified -type Float64Flag struct { - cli.Float64Flag - set *flag.FlagSet -} - -// NewFloat64Flag creates a new Float64Flag -func NewFloat64Flag(fl cli.Float64Flag) *Float64Flag { - return &Float64Flag{ Float64Flag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped Float64Flag.Apply -func (f *Float64Flag) Apply(set *flag.FlagSet) { - f.set = set - f.Float64Flag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped Float64Flag.ApplyWithError -func (f *Float64Flag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.Float64Flag.ApplyWithError(set) -} - -// GenericFlag is the flag type that wraps cli.GenericFlag to allow -// for other values to be specified -type GenericFlag struct { - cli.GenericFlag - set *flag.FlagSet -} - -// NewGenericFlag creates a new GenericFlag -func NewGenericFlag(fl cli.GenericFlag) *GenericFlag { - return &GenericFlag{ GenericFlag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped GenericFlag.Apply -func (f *GenericFlag) Apply(set *flag.FlagSet) { - f.set = set - f.GenericFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped GenericFlag.ApplyWithError -func (f *GenericFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.GenericFlag.ApplyWithError(set) -} - -// Int64Flag is the flag type that wraps cli.Int64Flag to allow -// for other values to be specified -type Int64Flag struct { - cli.Int64Flag - set *flag.FlagSet -} - -// NewInt64Flag creates a new Int64Flag -func NewInt64Flag(fl cli.Int64Flag) *Int64Flag { - return &Int64Flag{ Int64Flag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped Int64Flag.Apply -func (f *Int64Flag) Apply(set *flag.FlagSet) { - f.set = set - f.Int64Flag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped Int64Flag.ApplyWithError -func (f *Int64Flag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.Int64Flag.ApplyWithError(set) -} - -// IntFlag is the flag type that wraps cli.IntFlag to allow -// for other values to be specified -type IntFlag struct { - cli.IntFlag - set *flag.FlagSet -} - -// NewIntFlag creates a new IntFlag -func NewIntFlag(fl cli.IntFlag) *IntFlag { - return &IntFlag{ IntFlag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped IntFlag.Apply -func (f *IntFlag) Apply(set *flag.FlagSet) { - f.set = set - f.IntFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped IntFlag.ApplyWithError -func (f *IntFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.IntFlag.ApplyWithError(set) -} - -// IntSliceFlag is the flag type that wraps cli.IntSliceFlag to allow -// for other values to be specified -type IntSliceFlag struct { - cli.IntSliceFlag - set *flag.FlagSet -} - -// NewIntSliceFlag creates a new IntSliceFlag -func NewIntSliceFlag(fl cli.IntSliceFlag) *IntSliceFlag { - return &IntSliceFlag{ IntSliceFlag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped IntSliceFlag.Apply -func (f *IntSliceFlag) Apply(set *flag.FlagSet) { - f.set = set - f.IntSliceFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped IntSliceFlag.ApplyWithError -func (f *IntSliceFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.IntSliceFlag.ApplyWithError(set) -} - -// Int64SliceFlag is the flag type that wraps cli.Int64SliceFlag to allow -// for other values to be specified -type Int64SliceFlag struct { - cli.Int64SliceFlag - set *flag.FlagSet -} - -// NewInt64SliceFlag creates a new Int64SliceFlag -func NewInt64SliceFlag(fl cli.Int64SliceFlag) *Int64SliceFlag { - return &Int64SliceFlag{ Int64SliceFlag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped Int64SliceFlag.Apply -func (f *Int64SliceFlag) Apply(set *flag.FlagSet) { - f.set = set - f.Int64SliceFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped Int64SliceFlag.ApplyWithError -func (f *Int64SliceFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.Int64SliceFlag.ApplyWithError(set) -} - -// StringFlag is the flag type that wraps cli.StringFlag to allow -// for other values to be specified -type StringFlag struct { - cli.StringFlag - set *flag.FlagSet -} - -// NewStringFlag creates a new StringFlag -func NewStringFlag(fl cli.StringFlag) *StringFlag { - return &StringFlag{ StringFlag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped StringFlag.Apply -func (f *StringFlag) Apply(set *flag.FlagSet) { - f.set = set - f.StringFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped StringFlag.ApplyWithError -func (f *StringFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.StringFlag.ApplyWithError(set) -} - -// StringSliceFlag is the flag type that wraps cli.StringSliceFlag to allow -// for other values to be specified -type StringSliceFlag struct { - cli.StringSliceFlag - set *flag.FlagSet -} - -// NewStringSliceFlag creates a new StringSliceFlag -func NewStringSliceFlag(fl cli.StringSliceFlag) *StringSliceFlag { - return &StringSliceFlag{ StringSliceFlag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped StringSliceFlag.Apply -func (f *StringSliceFlag) Apply(set *flag.FlagSet) { - f.set = set - f.StringSliceFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped StringSliceFlag.ApplyWithError -func (f *StringSliceFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.StringSliceFlag.ApplyWithError(set) -} - -// Uint64Flag is the flag type that wraps cli.Uint64Flag to allow -// for other values to be specified -type Uint64Flag struct { - cli.Uint64Flag - set *flag.FlagSet -} - -// NewUint64Flag creates a new Uint64Flag -func NewUint64Flag(fl cli.Uint64Flag) *Uint64Flag { - return &Uint64Flag{ Uint64Flag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped Uint64Flag.Apply -func (f *Uint64Flag) Apply(set *flag.FlagSet) { - f.set = set - f.Uint64Flag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped Uint64Flag.ApplyWithError -func (f *Uint64Flag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.Uint64Flag.ApplyWithError(set) -} - -// UintFlag is the flag type that wraps cli.UintFlag to allow -// for other values to be specified -type UintFlag struct { - cli.UintFlag - set *flag.FlagSet -} - -// NewUintFlag creates a new UintFlag -func NewUintFlag(fl cli.UintFlag) *UintFlag { - return &UintFlag{ UintFlag: fl, set: nil } -} - -// Apply saves the flagSet for later usage calls, then calls -// the wrapped UintFlag.Apply -func (f *UintFlag) Apply(set *flag.FlagSet) { - f.set = set - f.UintFlag.Apply(set) -} - -// ApplyWithError saves the flagSet for later usage calls, then calls -// the wrapped UintFlag.ApplyWithError -func (f *UintFlag) ApplyWithError(set *flag.FlagSet) error { - f.set = set - return f.UintFlag.ApplyWithError(set) -} diff --git a/flag_generated.go b/flag_generated.go deleted file mode 100644 index c4d1591b98..0000000000 --- a/flag_generated.go +++ /dev/null @@ -1,1100 +0,0 @@ -// Code generated by fg; DO NOT EDIT. - -package cli - -import ( - "flag" - "fmt" - "strconv" - "time" -) - -type FlagErr struct { - Custom bool - Message string -} - -// BoolFlag is a flag with type bool -type BoolFlag struct { - 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 -// (for usage defaults) -func (f BoolFlag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f BoolFlag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -func (f BoolFlag) IsRequired() bool { - return f.Required -} - -// 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 -func (f BoolFlag) TakesValue() bool { - return false -} - -// GetUsage returns the usage string for the flag -func (f BoolFlag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f BoolFlag) GetValue() string { - return "" -} - -// Bool looks up the value of a local BoolFlag, returns -// false if not found -func (c *Context) Bool(name string) bool { - return lookupBool(name, c.flagSet) -} - -// GlobalBool looks up the value of a global BoolFlag, returns -// false if not found -func (c *Context) GlobalBool(name string) bool { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupBool(name, fs) - } - return false -} - -func lookupBool(name string, set *flag.FlagSet) bool { - f := set.Lookup(name) - if f != nil { - parsed, err := strconv.ParseBool(f.Value.String()) - if err != nil { - return false - } - return parsed - } - return false -} - -// 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 - RequiredFlagErr FlagErr - Hidden bool - TakesFile bool - Destination *bool -} - -// String returns a readable representation of this value -// (for usage defaults) -func (f BoolTFlag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f BoolTFlag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -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 -} - -// TakesValue returns true of the flag takes a value, otherwise false -func (f BoolTFlag) TakesValue() bool { - return false -} - -// GetUsage returns the usage string for the flag -func (f BoolTFlag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f BoolTFlag) GetValue() string { - return "" -} - -// BoolT looks up the value of a local BoolTFlag, returns -// false if not found -func (c *Context) BoolT(name string) bool { - return lookupBoolT(name, c.flagSet) -} - -// GlobalBoolT looks up the value of a global BoolTFlag, returns -// false if not found -func (c *Context) GlobalBoolT(name string) bool { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupBoolT(name, fs) - } - return false -} - -func lookupBoolT(name string, set *flag.FlagSet) bool { - f := set.Lookup(name) - if f != nil { - parsed, err := strconv.ParseBool(f.Value.String()) - if err != nil { - return false - } - return parsed - } - return false -} - -// 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 - RequiredFlagErr FlagErr - Hidden bool - TakesFile bool - Value time.Duration - Destination *time.Duration -} - -// String returns a readable representation of this value -// (for usage defaults) -func (f DurationFlag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f DurationFlag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -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 -} - -// TakesValue returns true of the flag takes a value, otherwise false -func (f DurationFlag) TakesValue() bool { - return true -} - -// GetUsage returns the usage string for the flag -func (f DurationFlag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f DurationFlag) GetValue() string { - return f.Value.String() -} - -// Duration looks up the value of a local DurationFlag, returns -// 0 if not found -func (c *Context) Duration(name string) time.Duration { - return lookupDuration(name, c.flagSet) -} - -// GlobalDuration looks up the value of a global DurationFlag, returns -// 0 if not found -func (c *Context) GlobalDuration(name string) time.Duration { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupDuration(name, fs) - } - return 0 -} - -func lookupDuration(name string, set *flag.FlagSet) time.Duration { - f := set.Lookup(name) - if f != nil { - parsed, err := time.ParseDuration(f.Value.String()) - if err != nil { - return 0 - } - return parsed - } - return 0 -} - -// Float64Flag is a flag with type float64 -type Float64Flag struct { - 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 -// (for usage defaults) -func (f Float64Flag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f Float64Flag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -func (f Float64Flag) IsRequired() bool { - return f.Required -} - -// 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 -func (f Float64Flag) TakesValue() bool { - return true -} - -// GetUsage returns the usage string for the flag -func (f Float64Flag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f Float64Flag) GetValue() string { - return fmt.Sprintf("%f", f.Value) -} - -// Float64 looks up the value of a local Float64Flag, returns -// 0 if not found -func (c *Context) Float64(name string) float64 { - return lookupFloat64(name, c.flagSet) -} - -// GlobalFloat64 looks up the value of a global Float64Flag, returns -// 0 if not found -func (c *Context) GlobalFloat64(name string) float64 { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupFloat64(name, fs) - } - return 0 -} - -func lookupFloat64(name string, set *flag.FlagSet) float64 { - f := set.Lookup(name) - if f != nil { - parsed, err := strconv.ParseFloat(f.Value.String(), 64) - if err != nil { - return 0 - } - return parsed - } - return 0 -} - -// GenericFlag is a flag with type Generic -type GenericFlag struct { - 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 -// (for usage defaults) -func (f GenericFlag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f GenericFlag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -func (f GenericFlag) IsRequired() bool { - return f.Required -} - -// 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 -func (f GenericFlag) TakesValue() bool { - return true -} - -// GetUsage returns the usage string for the flag -func (f GenericFlag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f GenericFlag) GetValue() string { - if f.Value != nil { - return f.Value.String() - } - return "" -} - -// Generic looks up the value of a local GenericFlag, returns -// nil if not found -func (c *Context) Generic(name string) interface{} { - return lookupGeneric(name, c.flagSet) -} - -// GlobalGeneric looks up the value of a global GenericFlag, returns -// nil if not found -func (c *Context) GlobalGeneric(name string) interface{} { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupGeneric(name, fs) - } - return nil -} - -func lookupGeneric(name string, set *flag.FlagSet) interface{} { - f := set.Lookup(name) - if f != nil { - parsed, err := f.Value, error(nil) - if err != nil { - return nil - } - return parsed - } - return nil -} - -// Int64Flag is a flag with type int64 -type Int64Flag struct { - 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 -// (for usage defaults) -func (f Int64Flag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f Int64Flag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -func (f Int64Flag) IsRequired() bool { - return f.Required -} - -// 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 -func (f Int64Flag) TakesValue() bool { - return true -} - -// GetUsage returns the usage string for the flag -func (f Int64Flag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f Int64Flag) GetValue() string { - return fmt.Sprintf("%d", f.Value) -} - -// Int64 looks up the value of a local Int64Flag, returns -// 0 if not found -func (c *Context) Int64(name string) int64 { - return lookupInt64(name, c.flagSet) -} - -// GlobalInt64 looks up the value of a global Int64Flag, returns -// 0 if not found -func (c *Context) GlobalInt64(name string) int64 { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupInt64(name, fs) - } - return 0 -} - -func lookupInt64(name string, set *flag.FlagSet) int64 { - f := set.Lookup(name) - if f != nil { - parsed, err := strconv.ParseInt(f.Value.String(), 0, 64) - if err != nil { - return 0 - } - return parsed - } - return 0 -} - -// IntFlag is a flag with type int -type IntFlag struct { - 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 -// (for usage defaults) -func (f IntFlag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f IntFlag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -func (f IntFlag) IsRequired() bool { - return f.Required -} - -// 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 -func (f IntFlag) TakesValue() bool { - return true -} - -// GetUsage returns the usage string for the flag -func (f IntFlag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f IntFlag) GetValue() string { - return fmt.Sprintf("%d", f.Value) -} - -// Int looks up the value of a local IntFlag, returns -// 0 if not found -func (c *Context) Int(name string) int { - return lookupInt(name, c.flagSet) -} - -// GlobalInt looks up the value of a global IntFlag, returns -// 0 if not found -func (c *Context) GlobalInt(name string) int { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupInt(name, fs) - } - return 0 -} - -func lookupInt(name string, set *flag.FlagSet) int { - f := set.Lookup(name) - if f != nil { - parsed, err := strconv.ParseInt(f.Value.String(), 0, 64) - if err != nil { - return 0 - } - return int(parsed) - } - return 0 -} - -// IntSliceFlag is a flag with type *IntSlice -type IntSliceFlag struct { - 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 -// (for usage defaults) -func (f IntSliceFlag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f IntSliceFlag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -func (f IntSliceFlag) IsRequired() bool { - return f.Required -} - -// 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 -func (f IntSliceFlag) TakesValue() bool { - return true -} - -// GetUsage returns the usage string for the flag -func (f IntSliceFlag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f IntSliceFlag) GetValue() string { - if f.Value != nil { - return f.Value.String() - } - return "" -} - -// IntSlice looks up the value of a local IntSliceFlag, returns -// nil if not found -func (c *Context) IntSlice(name string) []int { - return lookupIntSlice(name, c.flagSet) -} - -// GlobalIntSlice looks up the value of a global IntSliceFlag, returns -// nil if not found -func (c *Context) GlobalIntSlice(name string) []int { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupIntSlice(name, fs) - } - return nil -} - -func lookupIntSlice(name string, set *flag.FlagSet) []int { - f := set.Lookup(name) - if f != nil { - parsed, err := (f.Value.(*IntSlice)).Value(), error(nil) - if err != nil { - return nil - } - return parsed - } - return nil -} - -// Int64SliceFlag is a flag with type *Int64Slice -type Int64SliceFlag struct { - 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 -// (for usage defaults) -func (f Int64SliceFlag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f Int64SliceFlag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -func (f Int64SliceFlag) IsRequired() bool { - return f.Required -} - -// 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 -func (f Int64SliceFlag) TakesValue() bool { - return true -} - -// GetUsage returns the usage string for the flag -func (f Int64SliceFlag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f Int64SliceFlag) GetValue() string { - if f.Value != nil { - return f.Value.String() - } - return "" -} - -// Int64Slice looks up the value of a local Int64SliceFlag, returns -// nil if not found -func (c *Context) Int64Slice(name string) []int64 { - return lookupInt64Slice(name, c.flagSet) -} - -// GlobalInt64Slice looks up the value of a global Int64SliceFlag, returns -// nil if not found -func (c *Context) GlobalInt64Slice(name string) []int64 { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupInt64Slice(name, fs) - } - return nil -} - -func lookupInt64Slice(name string, set *flag.FlagSet) []int64 { - f := set.Lookup(name) - if f != nil { - parsed, err := (f.Value.(*Int64Slice)).Value(), error(nil) - if err != nil { - return nil - } - return parsed - } - return nil -} - -// StringFlag is a flag with type string -type StringFlag struct { - 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 -// (for usage defaults) -func (f StringFlag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f StringFlag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -func (f StringFlag) IsRequired() bool { - return f.Required -} - -// 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 -func (f StringFlag) TakesValue() bool { - return true -} - -// GetUsage returns the usage string for the flag -func (f StringFlag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f StringFlag) GetValue() string { - return f.Value -} - -// String looks up the value of a local StringFlag, returns -// "" if not found -func (c *Context) String(name string) string { - return lookupString(name, c.flagSet) -} - -// GlobalString looks up the value of a global StringFlag, returns -// "" if not found -func (c *Context) GlobalString(name string) string { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupString(name, fs) - } - return "" -} - -func lookupString(name string, set *flag.FlagSet) string { - f := set.Lookup(name) - if f != nil { - parsed, err := f.Value.String(), error(nil) - if err != nil { - return "" - } - return parsed - } - return "" -} - -// StringSliceFlag is a flag with type *StringSlice -type StringSliceFlag struct { - 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 -// (for usage defaults) -func (f StringSliceFlag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f StringSliceFlag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -func (f StringSliceFlag) IsRequired() bool { - return f.Required -} - -// 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 -func (f StringSliceFlag) TakesValue() bool { - return true -} - -// GetUsage returns the usage string for the flag -func (f StringSliceFlag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f StringSliceFlag) GetValue() string { - if f.Value != nil { - return f.Value.String() - } - return "" -} - -// StringSlice looks up the value of a local StringSliceFlag, returns -// nil if not found -func (c *Context) StringSlice(name string) []string { - return lookupStringSlice(name, c.flagSet) -} - -// GlobalStringSlice looks up the value of a global StringSliceFlag, returns -// nil if not found -func (c *Context) GlobalStringSlice(name string) []string { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupStringSlice(name, fs) - } - return nil -} - -func lookupStringSlice(name string, set *flag.FlagSet) []string { - f := set.Lookup(name) - if f != nil { - parsed, err := (f.Value.(*StringSlice)).Value(), error(nil) - if err != nil { - return nil - } - return parsed - } - return nil -} - -// Uint64Flag is a flag with type uint64 -type Uint64Flag struct { - 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 -// (for usage defaults) -func (f Uint64Flag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f Uint64Flag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -func (f Uint64Flag) IsRequired() bool { - return f.Required -} - -// 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 -func (f Uint64Flag) TakesValue() bool { - return true -} - -// GetUsage returns the usage string for the flag -func (f Uint64Flag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f Uint64Flag) GetValue() string { - return fmt.Sprintf("%d", f.Value) -} - -// Uint64 looks up the value of a local Uint64Flag, returns -// 0 if not found -func (c *Context) Uint64(name string) uint64 { - return lookupUint64(name, c.flagSet) -} - -// GlobalUint64 looks up the value of a global Uint64Flag, returns -// 0 if not found -func (c *Context) GlobalUint64(name string) uint64 { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupUint64(name, fs) - } - return 0 -} - -func lookupUint64(name string, set *flag.FlagSet) uint64 { - f := set.Lookup(name) - if f != nil { - parsed, err := strconv.ParseUint(f.Value.String(), 0, 64) - if err != nil { - return 0 - } - return parsed - } - return 0 -} - -// UintFlag is a flag with type uint -type UintFlag struct { - 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 -// (for usage defaults) -func (f UintFlag) String() string { - return FlagStringer(f) -} - -// GetName returns the name of the flag -func (f UintFlag) GetName() string { - return f.Name -} - -// IsRequired returns whether or not the flag is required -func (f UintFlag) IsRequired() bool { - return f.Required -} - -// 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 -func (f UintFlag) TakesValue() bool { - return true -} - -// GetUsage returns the usage string for the flag -func (f UintFlag) GetUsage() string { - return f.Usage -} - -// GetValue returns the flags value as string representation and an empty -// string if the flag takes no value at all. -func (f UintFlag) GetValue() string { - return fmt.Sprintf("%d", f.Value) -} - -// Uint looks up the value of a local UintFlag, returns -// 0 if not found -func (c *Context) Uint(name string) uint { - return lookupUint(name, c.flagSet) -} - -// GlobalUint looks up the value of a global UintFlag, returns -// 0 if not found -func (c *Context) GlobalUint(name string) uint { - if fs := lookupGlobalFlagSet(name, c); fs != nil { - return lookupUint(name, fs) - } - return 0 -} - -func lookupUint(name string, set *flag.FlagSet) uint { - f := set.Lookup(name) - if f != nil { - parsed, err := strconv.ParseUint(f.Value.String(), 0, 64) - if err != nil { - return 0 - } - return uint(parsed) - } - return 0 -}