Skip to content

Conversation

@ikedam
Copy link

@ikedam ikedam commented Aug 9, 2025

Close #337

Alternative to #338.

FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling=UnknownFlagsHandlingPassUnknownToArgs 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 = UnknownFlagsHandlingErrorOnUnknown (Default)
    • Same to FlagSet.ParseErrorsAllowlist.UnknownFlags = false
    • Treats unknown flags error.
  • FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling = UnknownFlagsHandlingIgnoreUnknown
    • Same to FlagSet.ParseErrorsAllowlist.UnknownFlags = true
    • Ignore unknown flags.
  • FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling=UnknownFlagsHandlingPassUnknownToArgs:
    • Pass unknown flags to Args() (described above)

Backward compatibilities:

  • FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling is not set (UnknownFlagsHandlingErrorOnUnknown) and FlagSet.ParseErrorsAllowlist.UnknownFlags = true:
    • Same to FlagSet.ParseErrorsAllowlist.UnknownFlagsHandling = UnknownFlagsHandlingIgnoreUnknown
    • Ignore unknown flags.

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.

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.

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