Message189867
Here's another approach to the problem, using an iterative localized search. For simple cases it produces the same thing, but in complex cases it is more robust.
It is based on two ideas:
- if the action in consume_optional() is being 'greedy', use
slots = self._match_arguments_partial([action]+positionals, selected_patterns)
to determine if this action can share arguments with any of the remaining positionals. This is similar to how consume_positionals() allocates arguments to the set of positionals.
- try this 'sharing' with the last optional. If that is not enough, try the penultimate optional as well, and continue working toward the start as needed.
Since the normal parsing is from left to right, figuring out when to start 'sharing' requires some sort of search strategy. I have moved the start_index loop into a consume_loop() function, and added a switch so it can parse the arguments without invoking take_action() (so arguments are not evaluated, and namespace is not changed).
If there is a suspected 'greed' problem, consume_loop() is called (in test mode) one or more times to determine the right-most optionals to use, and once more (with take_action) to parse and evaluate the arguments.
As in the previous patch this writes a log file for debugging purposes. test_argparse.py now has a number of tests for this issue.
It is more robust than the previous patch, and does not need special handling for things like subparsers and explicit arguments. |
|
Date |
User |
Action |
Args |
2013-05-23 16:20:44 | paul.j3 | set | recipients:
+ paul.j3, bethard, wrobell, eric.araujo, catherine, elsdoerfer, Kotan |
2013-05-23 16:20:42 | paul.j3 | set | messageid: <1369326042.2.0.778468251586.issue9338@psf.upfronthosting.co.za> |
2013-05-23 16:20:42 | paul.j3 | link | issue9338 messages |
2013-05-23 16:20:41 | paul.j3 | create | |
|