@@ -29,6 +29,13 @@ const (
2929 PanicOnError
3030)
3131
32+ // ParseErrorsWhitelist defines the parsing errors that can be ignored
33+ type ParseErrorsWhitelist struct {
34+ // UnknownFlags will ignore unknown flags errors and continue parsing the rest of the flags.
35+ // See VisitUnknowns or GetUnknownFlags for collected unknowns.
36+ UnknownFlags bool
37+ }
38+
3239// ParseErrorsAllowlist defines the parsing errors that can be ignored
3340type ParseErrorsAllowlist struct {
3441 // UnknownFlags will ignore unknown flags errors and continue parsing the rest of the flags.
@@ -51,7 +58,10 @@ type FlagSet struct {
5158 // help/usage messages.
5259 SortFlags bool
5360
54- // ParseErrorsAllowlist is used to configure a whitelist of errors
61+ // ParseErrorsWhitelist is used to configure a whitelist of errors
62+ ParseErrorsWhitelist ParseErrorsWhitelist
63+
64+ // ParseErrorsAllowlist is used to configure an allowlist of errors
5565 ParseErrorsAllowlist ParseErrorsAllowlist
5666
5767 // DisableBuiltinHelp toggles the built-in convention of handling -h and --help
@@ -289,7 +299,8 @@ func (f *FlagSet) VisitUnknowns(fn func(*UnknownFlag)) {
289299}
290300
291301// GetUnknownFlags returns unknown flags found during Parse.
292- // This requires ParseErrorsAllowlist.UnknownFlags to be set so that
302+ // This requires ParseErrorsWhitelist.UnknownFlags or
303+ // ParseErrorsAllowlist.UnknownFlags to be set so that
293304// parsing does not abort on the first unknown flag.
294305func (f * FlagSet ) GetUnknownFlags () []* UnknownFlag {
295306 return f .unknownFlags
@@ -305,7 +316,8 @@ func VisitUnknowns(fn func(*UnknownFlag)) {
305316}
306317
307318// GetUnknownFlags returns unknown flags found during Parse.
308- // This requires ParseErrorsAllowlist.UnknownFlags to be set so that
319+ // This requires ParseErrorsWhitelist.UnknownFlags or
320+ // ParseErrorsAllowlist.UnknownFlags to be set so that
309321// parsing does not abort on the first unknown flag.
310322func GetUnknownFlags () []* UnknownFlag {
311323 return CommandLine .GetUnknownFlags ()
@@ -980,7 +992,7 @@ func (f *FlagSet) parseLongArg(s string, args []string, fn parseFunc) (a []strin
980992 err = ErrHelp
981993 return
982994 }
983- if ! f .ParseErrorsAllowlist .UnknownFlags {
995+ if ! f .ParseErrorsWhitelist . UnknownFlags && ! f . ParseErrorsAllowlist .UnknownFlags {
984996 err = f .failf ("unknown flag: --%s" , name )
985997 return
986998 }
@@ -1001,11 +1013,11 @@ func (f *FlagSet) parseLongArg(s string, args []string, fn parseFunc) (a []strin
10011013 value = a [0 ]
10021014 a = a [1 :]
10031015 }
1004- } else if f .ParseErrorsAllowlist .UnknownFlags {
1016+ } else if f .ParseErrorsWhitelist . UnknownFlags || f . ParseErrorsAllowlist .UnknownFlags {
10051017 value = ""
10061018 }
10071019
1008- if ! exists && f . ParseErrorsAllowlist .UnknownFlags {
1020+ if ! exists && ( f . ParseErrorsWhitelist . UnknownFlags || f . ParseErrorsAllowlist .UnknownFlags ) {
10091021 f .addUnknownFlag (name , value )
10101022 return
10111023 }
@@ -1030,7 +1042,7 @@ func (f *FlagSet) parseSingleShortArg(shorthands string, args []string, fn parse
10301042 err = ErrHelp
10311043 return
10321044 }
1033- if ! f .ParseErrorsAllowlist .UnknownFlags {
1045+ if ! f .ParseErrorsWhitelist . UnknownFlags && ! f . ParseErrorsAllowlist .UnknownFlags {
10341046 err = f .failf ("unknown shorthand flag: %q in -%s" , c , shorthands )
10351047 return
10361048 }
@@ -1058,11 +1070,11 @@ func (f *FlagSet) parseSingleShortArg(shorthands string, args []string, fn parse
10581070 value = args [0 ]
10591071 outArgs = args [1 :]
10601072 }
1061- } else if f .ParseErrorsAllowlist .UnknownFlags {
1073+ } else if f .ParseErrorsWhitelist . UnknownFlags || f . ParseErrorsAllowlist .UnknownFlags {
10621074 value = ""
10631075 }
10641076
1065- if ! exists && f . ParseErrorsAllowlist .UnknownFlags {
1077+ if ! exists && ( f . ParseErrorsWhitelist . UnknownFlags || f . ParseErrorsAllowlist .UnknownFlags ) {
10661078 f .addUnknownFlag (string (c ), value )
10671079 return
10681080 }
0 commit comments