Skip to content

Commit 387ae5f

Browse files
authored
Merge pull request #11 from cornfeedhobo/fix-allowlist
add whitelist back and make allowlist reverse compatible
2 parents b612a34 + 5c8cec5 commit 387ae5f

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

flag.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3340
type 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.
294305
func (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.
310322
func 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
}

flag_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@ func testParseWithUnknownFlags(f *FlagSet, t *testing.T) {
408408
if f.Parsed() {
409409
t.Error("f.Parse() = true before Parse")
410410
}
411-
f.ParseErrorsAllowlist.UnknownFlags = true
412411

413412
f.BoolP("boola", "a", false, "bool value")
414413
f.BoolP("boolb", "b", false, "bool2 value")
@@ -609,6 +608,11 @@ func TestParseAll(t *testing.T) {
609608

610609
func TestIgnoreUnknownFlags(t *testing.T) {
611610
ResetForTesting(func() { t.Error("bad parse") })
611+
CommandLine.ParseErrorsWhitelist.UnknownFlags = true
612+
testParseWithUnknownFlags(CommandLine, t)
613+
614+
ResetForTesting(func() { t.Error("bad parse") })
615+
CommandLine.ParseErrorsAllowlist.UnknownFlags = true
612616
testParseWithUnknownFlags(CommandLine, t)
613617
}
614618

0 commit comments

Comments
 (0)