forked from ogier/pflag
-
Notifications
You must be signed in to change notification settings - Fork 365
Extend UnknownFlags to UnknownFlagsHandling and introduce UnknownFlagsHandlingPassUnknownToArgs (#337)
#440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ikedam
wants to merge
1
commit into
spf13:master
Choose a base branch
from
ikedam:feature/337_PreserveUnknownFlagsEnum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -523,6 +523,111 @@ func testParseWithUnknownFlags(f *FlagSet, t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func testParseWithUnknownFlagsAndPassToArgs(f *FlagSet, t *testing.T) { | ||
| if f.Parsed() { | ||
| t.Fatal("f.Parse() = true before Parse") | ||
| } | ||
| f.ParseErrorsAllowlist.UnknownFlagsHandling = UnknownFlagsHandlingPassUnknownToArgs | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test code is same to https://github.com/spf13/pflag/pull/338/files#diff-7a7385a10188cdaa9ac18effc375607e65672f55084b8049b6d3f44a6ab70c51 except this line. |
||
| f.SetInterspersed(true) | ||
|
|
||
| f.BoolP("boola", "a", false, "bool value") | ||
| f.BoolP("boolb", "b", false, "bool2 value") | ||
| f.BoolP("boolc", "c", false, "bool3 value") | ||
| f.BoolP("boold", "d", false, "bool4 value") | ||
| f.BoolP("boole", "e", false, "bool4 value") | ||
| f.StringP("stringa", "s", "0", "string value") | ||
| f.StringP("stringz", "z", "0", "string value") | ||
| f.StringP("stringx", "x", "0", "string value") | ||
| f.StringP("stringy", "y", "0", "string value") | ||
| f.StringP("stringo", "o", "0", "string value") | ||
| f.Lookup("stringx").NoOptDefVal = "1" | ||
| args := []string{ | ||
| "-ab", | ||
| // -f and -g is unknown | ||
| "-fcgs=xx", | ||
| "--stringz=something", | ||
| "--unknown1", | ||
| "unknown1Value", | ||
| "-d=true", | ||
| "-x", | ||
| "--unknown2=unknown2Value", | ||
| "-u=unknown3Value", | ||
| "-p", | ||
| "unknown4Value", | ||
| "-q", //another unknown with bool value | ||
| "-y", | ||
| "ee", | ||
| "--unknown7=unknown7value", | ||
| "--stringo=ovalue", | ||
| "--unknown8=unknown8value", | ||
| "--boole", | ||
| "--unknown6", | ||
| "", | ||
| "-uuuuu", | ||
| "", | ||
| "--unknown10", | ||
| "--unknown11", | ||
| "arg0", | ||
| "arg1", | ||
| } | ||
| want := []string{ | ||
| "boola", "true", | ||
| "boolb", "true", | ||
| "boolc", "true", | ||
| "stringa", "xx", | ||
| "stringz", "something", | ||
| "boold", "true", | ||
| "stringx", "1", | ||
| "stringy", "ee", | ||
| "stringo", "ovalue", | ||
| "boole", "true", | ||
| } | ||
| wantArgs := []string{ | ||
| "-fg", | ||
| "--unknown1", | ||
| "unknown1Value", | ||
| "--unknown2=unknown2Value", | ||
| "-u=unknown3Value", | ||
| "-p", | ||
| "unknown4Value", | ||
| "-q", //another unknown with bool value | ||
| "--unknown7=unknown7value", | ||
| "--unknown8=unknown8value", | ||
| "--unknown6", | ||
| "", | ||
| "-uuuuu", | ||
| "", | ||
| "--unknown10", | ||
| "--unknown11", | ||
| "arg0", | ||
| "arg1", | ||
| } | ||
| got := []string{} | ||
| store := func(flag *Flag, value string) error { | ||
| got = append(got, flag.Name) | ||
| if len(value) > 0 { | ||
| got = append(got, value) | ||
| } | ||
| return nil | ||
| } | ||
| if err := f.ParseAll(args, store); err != nil { | ||
| t.Errorf("expected no error, got %s", err) | ||
| } | ||
| if !f.Parsed() { | ||
| t.Errorf("f.Parse() = false after Parse") | ||
| } | ||
| if !reflect.DeepEqual(got, want) { | ||
| t.Errorf("f.ParseAll() fail to restore the args") | ||
| t.Errorf("Got: %v", got) | ||
| t.Errorf("Want: %v", want) | ||
| } | ||
| if !reflect.DeepEqual(f.Args(), wantArgs) { | ||
| t.Errorf("f.ParseAll() fail to restore the args") | ||
| t.Errorf("Got: %v", f.Args()) | ||
| t.Errorf("Want: %v", wantArgs) | ||
| } | ||
| } | ||
|
|
||
| func TestShorthand(t *testing.T) { | ||
| f := NewFlagSet("shorthand", ContinueOnError) | ||
| if f.Parsed() { | ||
|
|
@@ -652,6 +757,10 @@ func TestIgnoreUnknownFlags(t *testing.T) { | |
| testParseWithUnknownFlags(GetCommandLine(), t) | ||
| } | ||
|
|
||
| func TestIgnoreUnknownFlagsAndPassToArgs(t *testing.T) { | ||
| ResetForTesting(func() { t.Error("bad parse") }) | ||
| testParseWithUnknownFlagsAndPassToArgs(GetCommandLine(), t) | ||
| } | ||
| func TestFlagSetParse(t *testing.T) { | ||
| testParse(NewFlagSet("test", ContinueOnError), t) | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
ErrorOnUnknownwould cause name conflict.