paul j3 added the comment:

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.

----------
Added file: http://bugs.python.org/file30349/argparse_7.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue9338>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to