[issue9334] argparse does not accept options taking arguments beginning with dash (regression from optparse)

paul j3 report at bugs.python.org
Fri Mar 22 18:16:18 CET 2013


paul j3 added the comment:

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.

----------
keywords: +patch
Added file: http://bugs.python.org/file29548/final.patch

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue9334>
_______________________________________


More information about the Python-bugs-list mailing list