Skip to content

Commit e71eab6

Browse files
authored
Merge branch 'main' into issue_1775
2 parents 84b36b4 + cd7d34a commit e71eab6

File tree

6 files changed

+85
-68
lines changed

6 files changed

+85
-68
lines changed

command_test.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3810,7 +3810,7 @@ func TestCommandReadArgsFromStdIn(t *testing.T) {
38103810
args: []string{"foo"},
38113811
expectedInt: 100,
38123812
expectedFloat: 100.1,
3813-
expectedSlice: []string{"hello", "hello\t\n 123\n44"},
3813+
expectedSlice: []string{"hello", "hello\n 123\n44"},
38143814
},
38153815
{
38163816
name: "end-args",
@@ -3981,7 +3981,8 @@ func TestJSONExportCommand(t *testing.T) {
39813981
"config": {
39823982
"TrimSpace": false
39833983
},
3984-
"onlyOnce": false
3984+
"onlyOnce": false,
3985+
"validateDefaults" : false
39853986
},
39863987
{
39873988
"name": "sub-command-flag",
@@ -4000,7 +4001,8 @@ func TestJSONExportCommand(t *testing.T) {
40004001
"config": {
40014002
"Count": null
40024003
},
4003-
"onlyOnce": false
4004+
"onlyOnce": false,
4005+
"validateDefaults" : false
40044006
}
40054007
],
40064008
"hideHelp": false,
@@ -4041,7 +4043,8 @@ func TestJSONExportCommand(t *testing.T) {
40414043
"config": {
40424044
"TrimSpace": false
40434045
},
4044-
"onlyOnce": false
4046+
"onlyOnce": false,
4047+
"validateDefaults" : false
40454048
},
40464049
{
40474050
"name": "another-flag",
@@ -4060,7 +4063,8 @@ func TestJSONExportCommand(t *testing.T) {
40604063
"config": {
40614064
"Count": null
40624065
},
4063-
"onlyOnce": false
4066+
"onlyOnce": false,
4067+
"validateDefaults" : false
40644068
}
40654069
],
40664070
"hideHelp": false,
@@ -4218,7 +4222,8 @@ func TestJSONExportCommand(t *testing.T) {
42184222
"config": {
42194223
"Count": null
42204224
},
4221-
"onlyOnce": false
4225+
"onlyOnce": false,
4226+
"validateDefaults" : false
42224227
}
42234228
],
42244229
"hideHelp": false,
@@ -4259,7 +4264,8 @@ func TestJSONExportCommand(t *testing.T) {
42594264
"config": {
42604265
"TrimSpace": false
42614266
},
4262-
"onlyOnce": false
4267+
"onlyOnce": false,
4268+
"validateDefaults" : false
42634269
},
42644270
{
42654271
"name": "another-flag",
@@ -4278,7 +4284,8 @@ func TestJSONExportCommand(t *testing.T) {
42784284
"config": {
42794285
"Count": null
42804286
},
4281-
"onlyOnce": false
4287+
"onlyOnce": false,
4288+
"validateDefaults" : false
42824289
}
42834290
],
42844291
"hideHelp": false,
@@ -4318,7 +4325,8 @@ func TestJSONExportCommand(t *testing.T) {
43184325
"config": {
43194326
"TrimSpace": false
43204327
},
4321-
"onlyOnce": false
4328+
"onlyOnce": false,
4329+
"validateDefaults" : false
43224330
},
43234331
{
43244332
"name": "flag",
@@ -4338,7 +4346,8 @@ func TestJSONExportCommand(t *testing.T) {
43384346
"config": {
43394347
"TrimSpace": false
43404348
},
4341-
"onlyOnce": false
4349+
"onlyOnce": false,
4350+
"validateDefaults" : false
43424351
},
43434352
{
43444353
"name": "another-flag",
@@ -4357,7 +4366,8 @@ func TestJSONExportCommand(t *testing.T) {
43574366
"config": {
43584367
"Count": null
43594368
},
4360-
"onlyOnce": false
4369+
"onlyOnce": false,
4370+
"validateDefaults" : false
43614371
},
43624372
{
43634373
"name": "hidden-flag",
@@ -4374,7 +4384,8 @@ func TestJSONExportCommand(t *testing.T) {
43744384
"config": {
43754385
"Count": null
43764386
},
4377-
"onlyOnce": false
4387+
"onlyOnce": false,
4388+
"validateDefaults" : false
43784389
}
43794390
],
43804391
"hideHelp": false,

docs/v2/examples/full-api-example.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,11 @@ func main() {
107107
Action: wopAction,
108108
},
109109
},
110-
SkipFlagParsing: false,
111-
HideHelp: false,
112-
Hidden: false,
113-
HelpName: "doo!",
110+
SkipFlagParsing: false,
111+
HideHelp: false,
112+
HideHelpCommands: false,
113+
Hidden: false,
114+
HelpName: "doo!",
114115
BashComplete: func(cCtx *cli.Context) {
115116
fmt.Fprintf(cCtx.App.Writer, "--better\n")
116117
},
@@ -156,6 +157,7 @@ func main() {
156157
},
157158
EnableBashCompletion: true,
158159
HideHelp: false,
160+
HideHelpCommands: false,
159161
HideVersion: false,
160162
BashComplete: func(cCtx *cli.Context) {
161163
fmt.Fprintf(cCtx.App.Writer, "lipstick\nkiss\nme\nlipstick\nringo\n")

flag_impl.go

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,24 @@ type NoConfig struct{}
6969
// C specifies the configuration required(if any for that flag type)
7070
// VC specifies the value creator which creates the flag.Value emulation
7171
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
8990

9091
// unexported fields for internal use
9192
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 {
145146
}
146147

147148
// Validate the given default or values set from external sources as well
148-
if f.Validator != nil {
149+
if f.Validator != nil && f.ValidateDefaults {
149150
if v, ok := f.value.Get().(T); !ok {
150151
return &typeError[T]{
151152
other: f.value.Get(),

flag_validation_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ func TestFlagDefaultValidation(t *testing.T) {
2020
}
2121
return fmt.Errorf("Value %d not in range [3,10] or [20,24]", i)
2222
},
23+
ValidateDefaults: true,
2324
},
2425
},
2526
}

godoc-current.txt

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -647,23 +647,24 @@ var VersionFlag Flag = &BoolFlag{
647647
VersionFlag prints the version for the application
648648

649649
type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {
650-
Name string `json:"name"` // name of the flag
651-
Category string `json:"category"` // category of the flag, if any
652-
DefaultText string `json:"defaultText"` // default text of the flag for usage purposes
653-
HideDefault bool `json:"hideDefault"` // whether to hide the default value in output
654-
Usage string `json:"usage"` // usage string for help output
655-
Sources ValueSourceChain `json:"-"` // sources to load flag value from
656-
Required bool `json:"required"` // whether the flag is required or not
657-
Hidden bool `json:"hidden"` // whether to hide the flag in help output
658-
Persistent bool `json:"persistent"` // whether the flag needs to be applied to subcommands as well
659-
Value T `json:"defaultValue"` // default value for this flag if not set by from any source
660-
Destination *T `json:"-"` // destination pointer for value when set
661-
Aliases []string `json:"aliases"` // Aliases that are allowed for this flag
662-
TakesFile bool `json:"takesFileArg"` // whether this flag takes a file argument, mainly for shell completion purposes
663-
Action func(context.Context, *Command, T) error `json:"-"` // Action callback to be called when flag is set
664-
Config C `json:"config"` // Additional/Custom configuration associated with this flag type
665-
OnlyOnce bool `json:"onlyOnce"` // whether this flag can be duplicated on the command line
666-
Validator func(T) error `json:"-"` // custom function to validate this flag value
650+
Name string `json:"name"` // name of the flag
651+
Category string `json:"category"` // category of the flag, if any
652+
DefaultText string `json:"defaultText"` // default text of the flag for usage purposes
653+
HideDefault bool `json:"hideDefault"` // whether to hide the default value in output
654+
Usage string `json:"usage"` // usage string for help output
655+
Sources ValueSourceChain `json:"-"` // sources to load flag value from
656+
Required bool `json:"required"` // whether the flag is required or not
657+
Hidden bool `json:"hidden"` // whether to hide the flag in help output
658+
Persistent bool `json:"persistent"` // whether the flag needs to be applied to subcommands as well
659+
Value T `json:"defaultValue"` // default value for this flag if not set by from any source
660+
Destination *T `json:"-"` // destination pointer for value when set
661+
Aliases []string `json:"aliases"` // Aliases that are allowed for this flag
662+
TakesFile bool `json:"takesFileArg"` // whether this flag takes a file argument, mainly for shell completion purposes
663+
Action func(context.Context, *Command, T) error `json:"-"` // Action callback to be called when flag is set
664+
Config C `json:"config"` // Additional/Custom configuration associated with this flag type
665+
OnlyOnce bool `json:"onlyOnce"` // whether this flag can be duplicated on the command line
666+
Validator func(T) error `json:"-"` // custom function to validate this flag value
667+
ValidateDefaults bool `json:"validateDefaults"` // whether to validate defaults or not
667668

668669
// Has unexported fields.
669670
}

testdata/godoc-v3.x.txt

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -647,23 +647,24 @@ var VersionFlag Flag = &BoolFlag{
647647
VersionFlag prints the version for the application
648648

649649
type FlagBase[T any, C any, VC ValueCreator[T, C]] struct {
650-
Name string `json:"name"` // name of the flag
651-
Category string `json:"category"` // category of the flag, if any
652-
DefaultText string `json:"defaultText"` // default text of the flag for usage purposes
653-
HideDefault bool `json:"hideDefault"` // whether to hide the default value in output
654-
Usage string `json:"usage"` // usage string for help output
655-
Sources ValueSourceChain `json:"-"` // sources to load flag value from
656-
Required bool `json:"required"` // whether the flag is required or not
657-
Hidden bool `json:"hidden"` // whether to hide the flag in help output
658-
Persistent bool `json:"persistent"` // whether the flag needs to be applied to subcommands as well
659-
Value T `json:"defaultValue"` // default value for this flag if not set by from any source
660-
Destination *T `json:"-"` // destination pointer for value when set
661-
Aliases []string `json:"aliases"` // Aliases that are allowed for this flag
662-
TakesFile bool `json:"takesFileArg"` // whether this flag takes a file argument, mainly for shell completion purposes
663-
Action func(context.Context, *Command, T) error `json:"-"` // Action callback to be called when flag is set
664-
Config C `json:"config"` // Additional/Custom configuration associated with this flag type
665-
OnlyOnce bool `json:"onlyOnce"` // whether this flag can be duplicated on the command line
666-
Validator func(T) error `json:"-"` // custom function to validate this flag value
650+
Name string `json:"name"` // name of the flag
651+
Category string `json:"category"` // category of the flag, if any
652+
DefaultText string `json:"defaultText"` // default text of the flag for usage purposes
653+
HideDefault bool `json:"hideDefault"` // whether to hide the default value in output
654+
Usage string `json:"usage"` // usage string for help output
655+
Sources ValueSourceChain `json:"-"` // sources to load flag value from
656+
Required bool `json:"required"` // whether the flag is required or not
657+
Hidden bool `json:"hidden"` // whether to hide the flag in help output
658+
Persistent bool `json:"persistent"` // whether the flag needs to be applied to subcommands as well
659+
Value T `json:"defaultValue"` // default value for this flag if not set by from any source
660+
Destination *T `json:"-"` // destination pointer for value when set
661+
Aliases []string `json:"aliases"` // Aliases that are allowed for this flag
662+
TakesFile bool `json:"takesFileArg"` // whether this flag takes a file argument, mainly for shell completion purposes
663+
Action func(context.Context, *Command, T) error `json:"-"` // Action callback to be called when flag is set
664+
Config C `json:"config"` // Additional/Custom configuration associated with this flag type
665+
OnlyOnce bool `json:"onlyOnce"` // whether this flag can be duplicated on the command line
666+
Validator func(T) error `json:"-"` // custom function to validate this flag value
667+
ValidateDefaults bool `json:"validateDefaults"` // whether to validate defaults or not
667668

668669
// Has unexported fields.
669670
}

0 commit comments

Comments
 (0)