Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ type ParseErrorsAllowlist struct {
UnknownFlags bool
}

// DEPRECATED: please use ParseErrorsAllowlist instead
// This type will be removed in a future release
type ParseErrorsWhitelist = ParseErrorsAllowlist

// NormalizedName is a flag name that has been normalized according to rules
// for the FlagSet (e.g. making '-' and '_' equivalent).
type NormalizedName string
Expand All @@ -161,6 +165,10 @@ type FlagSet struct {
// ParseErrorsAllowlist is used to configure an allowlist of errors
ParseErrorsAllowlist ParseErrorsAllowlist

// DEPRECATED: please use ParseErrorsAllowlist instead
// This field will be removed in a future release
ParseErrorsWhitelist ParseErrorsAllowlist

name string
parsed bool
actual map[NormalizedName]*Flag
Expand Down Expand Up @@ -984,6 +992,8 @@ func (f *FlagSet) parseLongArg(s string, args []string, fn parseFunc) (a []strin
case name == "help":
f.usage()
return a, ErrHelp
case f.ParseErrorsWhitelist.UnknownFlags:
fallthrough
case f.ParseErrorsAllowlist.UnknownFlags:
// --unknown=unknownval arg ...
// we do not want to lose arg in this case
Expand Down Expand Up @@ -1042,6 +1052,8 @@ func (f *FlagSet) parseSingleShortArg(shorthands string, args []string, fn parse
f.usage()
err = ErrHelp
return
case f.ParseErrorsWhitelist.UnknownFlags:
fallthrough
case f.ParseErrorsAllowlist.UnknownFlags:
// '-f=arg arg ...'
// we do not want to lose arg in this case
Expand Down
11 changes: 8 additions & 3 deletions flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -447,11 +447,11 @@ func testParseAll(f *FlagSet, t *testing.T) {
}
}

func testParseWithUnknownFlags(f *FlagSet, t *testing.T) {
func testParseWithUnknownFlags(f *FlagSet, t *testing.T, setUnknownFlags func(f *FlagSet)) {
if f.Parsed() {
t.Error("f.Parse() = true before Parse")
}
f.ParseErrorsAllowlist.UnknownFlags = true
setUnknownFlags(f)

f.BoolP("boola", "a", false, "bool value")
f.BoolP("boolb", "b", false, "bool2 value")
Expand Down Expand Up @@ -649,7 +649,12 @@ func TestParseAll(t *testing.T) {

func TestIgnoreUnknownFlags(t *testing.T) {
ResetForTesting(func() { t.Error("bad parse") })
testParseWithUnknownFlags(GetCommandLine(), t)
testParseWithUnknownFlags(GetCommandLine(), t, func(f *FlagSet) { f.ParseErrorsAllowlist.UnknownFlags = true })
}

func TestIgnoreUnknownFlagsBackwardsCompat(t *testing.T) {
ResetForTesting(func() { t.Error("bad parse") })
testParseWithUnknownFlags(GetCommandLine(), t, func(f *FlagSet) { f.ParseErrorsWhitelist.UnknownFlags = true })
}

func TestFlagSetParse(t *testing.T) {
Expand Down
Loading