paul j3 added the comment:

This patch implements, I think, the ideas bethard proposed.  It is test patch, 
not intended for production.

Most of work is in ArgumentParser._get_alt_length() which
- generates a pattern along the lines bethard proposed
- generates a string like arg_strings_pattern, but with optionals strings 
('-x') instead of 'O'.
- runs a match
- from groups like '-xAAA', creates dict entries like: 
    alt_opt_length['x'] = 3

Later, in consume_optionals(), this alternative count replaces arg_count if it 
is lower.  The next consume_positionals() then takes care of consuming the 
unconsumed arguments.

If _get_alt_length() has any problems, it logs an error, and returns an 
otherwise empty dict.  So it 'fails' quietly without affecting regular parsing.

Reasons for failing include (for now) the use of subparsers, optionals with 
explicit args, and special prefix_chars.  With exclusions like this, 
test_argparse.py runs without errors or failures.

Since this is still a testing vehicle, it writes an issue9338.log file with 
debugging entries.

This version works, but is both not sufficiently general and too general.  As 
bethard notes, the testing pattern could get very large if there are many 
optionals.  Ideally the pattern will allow the optionals in any order and 
combination between positionals.  The ambiguities that I discussed in the 
previous 2 posts disappear if the patching pattern is sufficiently general.

But I also suspect it is too general.  It does not need to match every case, 
just those where an optional is consuming arguments that should go to a 
positional.  But if we come up with something more specific, this could still 
be a useful testing tool.

----------
Added file: http://bugs.python.org/file30296/issue9338_3.patch

_______________________________________
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