@@ -69,23 +69,24 @@ type NoConfig struct{}
69
69
// C specifies the configuration required(if any for that flag type)
70
70
// VC specifies the value creator which creates the flag.Value emulation
71
71
type FlagBase [T any , C any , VC ValueCreator [T , C ]] struct {
72
- Name string `json:"name"` // name of the flag
73
- Category string `json:"category"` // category of the flag, if any
74
- DefaultText string `json:"defaultText"` // default text of the flag for usage purposes
75
- HideDefault bool `json:"hideDefault"` // whether to hide the default value in output
76
- Usage string `json:"usage"` // usage string for help output
77
- Sources ValueSourceChain `json:"-"` // sources to load flag value from
78
- Required bool `json:"required"` // whether the flag is required or not
79
- Hidden bool `json:"hidden"` // whether to hide the flag in help output
80
- Persistent bool `json:"persistent"` // whether the flag needs to be applied to subcommands as well
81
- Value T `json:"defaultValue"` // default value for this flag if not set by from any source
82
- Destination * T `json:"-"` // destination pointer for value when set
83
- Aliases []string `json:"aliases"` // Aliases that are allowed for this flag
84
- TakesFile bool `json:"takesFileArg"` // whether this flag takes a file argument, mainly for shell completion purposes
85
- Action func (context.Context , * Command , T ) error `json:"-"` // Action callback to be called when flag is set
86
- Config C `json:"config"` // Additional/Custom configuration associated with this flag type
87
- OnlyOnce bool `json:"onlyOnce"` // whether this flag can be duplicated on the command line
88
- Validator func (T ) error `json:"-"` // custom function to validate this flag value
72
+ Name string `json:"name"` // name of the flag
73
+ Category string `json:"category"` // category of the flag, if any
74
+ DefaultText string `json:"defaultText"` // default text of the flag for usage purposes
75
+ HideDefault bool `json:"hideDefault"` // whether to hide the default value in output
76
+ Usage string `json:"usage"` // usage string for help output
77
+ Sources ValueSourceChain `json:"-"` // sources to load flag value from
78
+ Required bool `json:"required"` // whether the flag is required or not
79
+ Hidden bool `json:"hidden"` // whether to hide the flag in help output
80
+ Persistent bool `json:"persistent"` // whether the flag needs to be applied to subcommands as well
81
+ Value T `json:"defaultValue"` // default value for this flag if not set by from any source
82
+ Destination * T `json:"-"` // destination pointer for value when set
83
+ Aliases []string `json:"aliases"` // Aliases that are allowed for this flag
84
+ TakesFile bool `json:"takesFileArg"` // whether this flag takes a file argument, mainly for shell completion purposes
85
+ Action func (context.Context , * Command , T ) error `json:"-"` // Action callback to be called when flag is set
86
+ Config C `json:"config"` // Additional/Custom configuration associated with this flag type
87
+ OnlyOnce bool `json:"onlyOnce"` // whether this flag can be duplicated on the command line
88
+ Validator func (T ) error `json:"-"` // custom function to validate this flag value
89
+ ValidateDefaults bool `json:"validateDefaults"` // whether to validate defaults or not
89
90
90
91
// unexported fields for internal use
91
92
count int // number of times the flag has been set
@@ -145,7 +146,7 @@ func (f *FlagBase[T, C, V]) Apply(set *flag.FlagSet) error {
145
146
}
146
147
147
148
// Validate the given default or values set from external sources as well
148
- if f .Validator != nil {
149
+ if f .Validator != nil && f . ValidateDefaults {
149
150
if v , ok := f .value .Get ().(T ); ! ok {
150
151
return & typeError [T ]{
151
152
other : f .value .Get (),
0 commit comments