forked from ogier/pflag
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
add ability to ignore unknown flags #160
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bbf05e3
add ability to ignore unknown flags
rajatjindal 56e2774
add testcases
rajatjindal eabdfa9
add 2 more patterns to the testcase
rajatjindal c8d7929
handle --unknownflag=val arg and -u=val arg scenario
rajatjindal 7245619
add ParseErrorsWhiteList to extend error handling during parsing
rajatjindal File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -389,7 +389,78 @@ func testParseAll(f *FlagSet, t *testing.T) { | |
} | ||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("f.ParseAll() fail to restore the args") | ||
t.Errorf("Got: %v", got) | ||
t.Errorf("Got: %v", got) | ||
t.Errorf("Want: %v", want) | ||
} | ||
} | ||
|
||
func testParseWithUnknownFlags(f *FlagSet, t *testing.T) { | ||
if f.Parsed() { | ||
t.Error("f.Parse() = true before Parse") | ||
} | ||
f.ParseErrorsWhitelist.UnknownFlags = 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", | ||
"-cs=xx", | ||
"--stringz=something", | ||
"--unknown1", | ||
"unknown1Value", | ||
"-d=true", | ||
"-x", | ||
"--unknown2=unknown2Value", | ||
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. I think this test need to cover the case where 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. added the testcase, thanks |
||
"-u=unknown3Value", | ||
"-p", | ||
"unknown4Value", | ||
"-q", //another unknown with bool value | ||
"-y", | ||
"ee", | ||
"--unknown7=unknown7value", | ||
"--stringo=ovalue", | ||
"--unknown8=unknown8value", | ||
"--boole", | ||
"--unknown6", | ||
} | ||
want := []string{ | ||
"boola", "true", | ||
"boolb", "true", | ||
"boolc", "true", | ||
"stringa", "xx", | ||
"stringz", "something", | ||
"boold", "true", | ||
"stringx", "1", | ||
"stringy", "ee", | ||
"stringo", "ovalue", | ||
"boole", "true", | ||
} | ||
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) | ||
} | ||
} | ||
|
@@ -500,6 +571,11 @@ func TestParseAll(t *testing.T) { | |
testParseAll(GetCommandLine(), t) | ||
} | ||
|
||
func TestIgnoreUnknownFlags(t *testing.T) { | ||
ResetForTesting(func() { t.Error("bad parse") }) | ||
testParseWithUnknownFlags(GetCommandLine(), t) | ||
} | ||
|
||
func TestFlagSetParse(t *testing.T) { | ||
testParse(NewFlagSet("test", ContinueOnError), t) | ||
} | ||
|
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.
What if I passed
--unknown=bob --next-flag
. I think this will strip--next-flag
, but I don't think it should.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.
this function just strips value for unknown flag, the flag itself is not included in args.
for
--unknown=value --next-flag ...
args will be--next-flag ...
added the testcase to test this explicitly
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.
I really think you need to know
len(split)
(fromparseLongArg
) and to use that. What if it was--unknown=value subcmd
. You would stripsubcmd
which is really not what the user wants.Clearly it is a heuristic to know what to strip and what not to strip for an 'unknown' but I think we want to be as careful as we can...
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.
oh yes, thats right. i would consider this a bug :)
fixed. thank you for pointing out.