Skip to content

Extend UnknownFlags to UnknownFlagsHandling and introduce PassUnknownFlagToArgs (#337)#440

Open
ikedam wants to merge 3 commits intospf13:masterfrom
ikedam:feature/337_PreserveUnknownFlagsEnum
Open

Extend UnknownFlags to UnknownFlagsHandling and introduce PassUnknownFlagToArgs (#337)#440
ikedam wants to merge 3 commits intospf13:masterfrom
ikedam:feature/337_PreserveUnknownFlagsEnum

Conversation

@ikedam
Copy link

@ikedam ikedam commented Aug 9, 2025

Close #337

Alternative to #338.

FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling=PassUnknownFlagToArgs behaves like this:

  • Input:
    -xayb -c -z --known-flag known-flag-value --unknown-flag arg0 arg1 arg2
    
    • -x, -y, -z and --unknown-flag are unknown
  • Args() results:
    -xy -z --unknown-flag arg0 arg1 arg2
    

This request deprecates FlagSet.ParseErrorsAllowlist.UnknownFlags and extends it to FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling.

  • FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling = ErrorOnUnknownFlag (Default)
    • Same to FlagSet.ParseErrorsAllowlist.UnknownFlags = false
    • Treats unknown flags error.
  • FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling = IgnoreUnknownFlag
    • Same to FlagSet.ParseErrorsAllowlist.UnknownFlags = true
    • Ignore unknown flags.
  • FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling=PassUnknownFlagToArgs:
    • Pass unknown flags to Args() (described above)

Backward compatibilities:

  • FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling is not set (ErrorOnUnknownFlag) and FlagSet.ParseErrorsAllowlist.UnknownFlags = true:
    • Same to FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling = ErrorOnUnknownFlag
    • Ignore unknown flags.
  • Also same for ParseErrorsWhitelist

flag_test.go Outdated
if f.Parsed() {
t.Fatal("f.Parse() = true before Parse")
}
f.ParseErrorsAllowlist.UnknownFlagsHandling = UnknownFlagsHandlingPassUnknownToArgs
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flag.go Outdated
Comment on lines +144 to +152
// UnknownFlagsHandlingErrorOnUnknown will return an error if an unknown flag is found
UnknownFlagsHandlingErrorOnUnknown UnknownFlagsHandling = iota
// UnknownFlagsHandlingIgnoreUnknown will ignore unknown flags and continue parsing rest of the flags
UnknownFlagsHandlingIgnoreUnknown
// UnknownFlagsHandlingPassUnknownToArgs will treat unknown flags as non-flag arguments.
// Combined shorthand flags mixed with known ones and unknown ones results
// combined flags only with unknown ones.
// E.g. -fghi results -gh if only `f` and `i` are known.
UnknownFlagsHandlingPassUnknownToArgs
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reeeally long names...
I worry names like ErrorOnUnknown would cause name conflict.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ErrorOnUnknownFlag? Along with IgnoreUnknownFlag and PassUnknownFlagToArgs. I don't think they can be made shorter than that without losing meaning, but I don't think we need to be overly cautious about naming conflicts here.

If you really want to guard against it, another variant is to namespace these somehow - either with a struct value where the enum values are fields on the struct, or by just moving them to an appropriately named subpackage.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed in c0bdd63

Copy link
Collaborator

@tomasaschan tomasaschan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this version a lot better than #338.

(The passing-error-as-control-flow thing smells a little, but the parseLongArg method was a mess before this change, so that's not your fault... Something to look out for in 2.0, I guess.)

Needs a rebase (I haven't looked into why) and probably shouldn't be merged until we've decided we don't have anything else we want to get into 1.0 before cutting 1.1.

flag.go Outdated
Comment on lines +144 to +152
// UnknownFlagsHandlingErrorOnUnknown will return an error if an unknown flag is found
UnknownFlagsHandlingErrorOnUnknown UnknownFlagsHandling = iota
// UnknownFlagsHandlingIgnoreUnknown will ignore unknown flags and continue parsing rest of the flags
UnknownFlagsHandlingIgnoreUnknown
// UnknownFlagsHandlingPassUnknownToArgs will treat unknown flags as non-flag arguments.
// Combined shorthand flags mixed with known ones and unknown ones results
// combined flags only with unknown ones.
// E.g. -fghi results -gh if only `f` and `i` are known.
UnknownFlagsHandlingPassUnknownToArgs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ErrorOnUnknownFlag? Along with IgnoreUnknownFlag and PassUnknownFlagToArgs. I don't think they can be made shorter than that without losing meaning, but I don't think we need to be overly cautious about naming conflicts here.

If you really want to guard against it, another variant is to namespace these somehow - either with a struct value where the enum values are fields on the struct, or by just moving them to an appropriately named subpackage.

flag.go Outdated
Comment on lines +166 to +175
func (a *ParseErrorsAllowlist) getUnknownFlagsHandling() UnknownFlagsHandling {
// if UnknownFlagsHandling is set, use it
if a.UnknownFlagsHandling != UnknownFlagsHandlingErrorOnUnknown {
return a.UnknownFlagsHandling
}

if a.UnknownFlags {
return UnknownFlagsHandlingIgnoreUnknown
}
return UnknownFlagsHandlingErrorOnUnknown
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@ikedam ikedam force-pushed the feature/337_PreserveUnknownFlagsEnum branch from f16d4d7 to d1d7aee Compare March 6, 2026 16:56
@ikedam
Copy link
Author

ikedam commented Mar 6, 2026

First, rebased and resolved the conflict.
Conflicted with #446 .

Needs to support both ParseErrorsAllowlist and ParseErrorsWhitelist, and resolved as d1d7aee .
The conflict was left unresolved in the previous commit and fixed in this one so reviewers can see exactly how it was resolved.

* `UnknownFlagsHandlingErrorOnUnknown` to `ErrorOnUnknownFlag` and so on
@ikedam ikedam changed the title Extend UnknownFlags to UnknownFlagsHandling and introduce UnknownFlagsHandlingPassUnknownToArgs (#337) Extend UnknownFlags to UnknownFlagsHandling and introduce PassUnknownFlagToArgs (#337) Mar 6, 2026
@ikedam ikedam requested a review from tomasaschan March 6, 2026 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Preserve unknown flags

2 participants