Repository navigation
Fix DefaultParser keeping property values across parses - #450
Conversation
|
Hello @oscerd This PR can break existing applications by throwing Is this intentional? Whether it is or not , this needs to be documented and tested. Rebase on git master and see TY! |
When parse() gets a Properties object, handleProperties() called processValue() on the Option registered in Options instead of on the copy that goes into the CommandLine, and only when that Option had no values yet. Nothing ever clears it, so once you've parsed with properties, reusing the same Options gives stale results: a new property value is ignored, a value given on the command line is replaced by the old one and ends up in the argument list, and pairs from a "-D" style option carry over into the next parse. Add the value to the copy instead, like values from the command line, so parsing doesn't change the Options anymore. A value the Option already has still wins over the property, as before, and the Javadoc now says so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Andrea Cosentino <ancosen@gmail.com>
2eedfd0 to
d19562c
Compare
|
Sorry @garydgregory, that wasn't intentional. The I put the check back, on the copy this time, so an existing value still wins and Thanks for catching it! |
Parsing still updates the option group selections, so saying that options itself isn't modified went too far. Say what actually holds: property values go into the returned CommandLine without changing the values of the registered options. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Andrea Cosentino <ancosen@gmail.com>
If you reuse an
Optionsinstance and passPropertiestoDefaultParser.parse(), the first parse leaves the property value behind in theOptionregistered inOptions:handleProperties()calledprocessValue()on that registered option rather than on the copy that goes into theCommandLine, and nothing ever clears it. From then on the sameOptionsgives wrong answers:-o valueon the command line returns the old property value, andvalueends up in the argument list;-Dstyle option (hasArgs().valueSeparator('=')), pairs from the earlier properties show up in the next parse'sgetOptionProperties().The fix adds the value to the copy instead, the same way values from the command line are handled, so parsing no longer changes the values of the registered options. A value the option already has still wins over the property, as
testPropertiesPreserveExistingOptionValue()expects, and theparse(Options, Properties, NonOptionAction, String...)Javadoc now says so. The three new tests inDefaultParserTestfail on master and pass with the change.mvn; that'smvnon the command line by itself.🤖 Generated with Claude Code