Repository navigation
DefaultParser rejects a numerical argument in every position but where an option value is pending - #452
Open
simpleqt wants to merge 1 commit into
Open
DefaultParser rejects a numerical argument in every position but where an option value is pending#452simpleqt wants to merge 1 commit into
simpleqt wants to merge 1 commit into
Conversation
DefaultParser recognizes a numerical argument, but only consults that check when
an option is waiting for its value (isArgument() is called from handleToken()
for the currentOption). A negative number in any other position therefore falls
through to handleUnknownToken(), which throws for any token that starts with a
dash:
new DefaultParser().parse(new Options(), new String[] {"-1"});
// UnrecognizedOptionException: Unrecognized option: -1
The same happens after a complete option and its value (-b value -1), after a
flag (-a -1) and for the second of two negative numbers (-b -1 -1.5). The
changelog entry for CLI-184 states that "negative numerical arguments take
precedence over numerical options", so a standalone negative number is expected
to be an argument.
Route a token that is a negative number and is not a registered option to the
arguments; tokens that match an option are still parsed as options, and the --
end-of-options marker is unchanged.
Assisted-By: DeepSeek dsv41
Signed-off-by: simpleqt <2015900818@qq.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
DefaultParserrecognizes a numerical argument, but only consults that test when an option is waiting for its value:isArgument()is called fromhandleToken()for the pendingcurrentOption(DefaultParser.java:609). A negative number in any other position falls through tohandleShortAndLongOption()and then tohandleUnknownToken(), which throws for any token that starts with a dash (DefaultParser.java:632-635):The same happens after a complete option and its value (
-b value -1), after a flag (-a -1), at the start of the line (-1 -a) and for the second of two negative numbers (-b -1 -1.5, where the first is correctly consumed as the value of-b).This contradicts the behaviour recorded in the release notes: CLI-184, "Negative numerical arguments take precedence over numerical options (only supported by the new DefaultParser)", and CLI-150, "Negative numbers mistaken for options". Today the precedence only exists while an option value is pending, so a program invoked as
tool -1always aborts withUnrecognizedOptionExceptionunless the caller writestool -- -1.Fix: in
handleToken(), route a token that is a negative number and does not match a registered option to the arguments. Everything else is unchanged:-1,parse(opts, "-1", "-2")sets-1and puts-2in the arguments;-b -1still consumes-1as the value of-b(the value path is untouched);--still sends every following token to the arguments;NonOptionActionmodes keep their meaning (IGNORE/SKIP/STOPare not reached for a negative number).Validation:
DefaultParserTest#testNegativeNumberArgument,#testNegativeNumberOptionand#testNegativeNumberAfterEndOfOptions.UnrecognizedOptionException: Unrecognized option: -1/-2; with the fix all pass.mvn testpasses (1005 tests, 0 failures) and checkstyle reports 0 violations.No
src/changes/changes.xmlentry is included because the action format requires a JIRA issue key and I do not have a JIRA account. Happy to add one, e.g.:AI assistance used to diagnose and fix this defect; the analysis and the change have been reviewed and verified as described above.