Author paul.j3
Recipients Christophe.Guillon, abacabadabacaba, amcnabb, andersk, bethard, danielsh, davidben, drm, eric.araujo, eric.smith, gdb, gfxmonk, nelhage, paul.j3, r.david.murray, skilletaudio
Date 2013-03-22.17:16:16
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1363972578.15.0.956483056028.issue9334@psf.upfronthosting.co.za>
In-reply-to
Content
This patch makes two changes to argparse.py ArgumentParser._parse_optional()

- accept negative scientific and complex numbers

- add the args_default_to_positional parser option

_negative_number_matcher only matches integers and simple floats.  This
is fine for detecting number-like options like '-1'.  But as used in
_parse_optional() it prevents strings like '-1e4' and '-1-4j' from being
classed as positionals (msg184174).  In this patch it is replaced with

    try:
        complex(arg_string)
        return None
    except ValueError:
        pass

Immediately before this number test I added

    if self.args_default_to_positional:
        return None

to implement the idea suggested in msg169978.

I added the args_default_to_positional parser option to the documentation, along with some notes on its implications in the `Arguments containing -` section.  A few of the examples that I added use scientific or complex numbers.

I tested test_argparse.py with args_default_to_positional=True default.  A number of the 'failures' no longer failed.
class TestDefaultToPositionalWithOptionLike illustrates this in the
Option-Like situation.

The only 'successes' to fail were in the TestAddSubparsers case.  There
an argument string  '0.5 -p 1 b -w 7' produced 'wrong choice' error,
since the '-p' was assumed to be a commands choice, rather than an unknown optional.

I translated the TestStandard cases from the optparse test file.  argparse ran most of these without problem.  The value of args_default_to_positional makes no difference.  There a few optparse tests that use '--'  or a valid optional as positional that argparse does not handle.
History
Date User Action Args
2013-03-22 17:16:18paul.j3setrecipients: + paul.j3, amcnabb, bethard, eric.smith, eric.araujo, r.david.murray, gfxmonk, andersk, abacabadabacaba, gdb, nelhage, drm, davidben, skilletaudio, Christophe.Guillon, danielsh
2013-03-22 17:16:18paul.j3setmessageid: <1363972578.15.0.956483056028.issue9334@psf.upfronthosting.co.za>
2013-03-22 17:16:18paul.j3linkissue9334 messages
2013-03-22 17:16:17paul.j3create