This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: argparse.ArgumentParser fails on arguments with leading dash and comma
Type: behavior Stage: resolved
Components: Versions: Python 3.7, Python 3.6
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: lene, paul.j3
Priority: normal Keywords:

Created on 2018-10-23 10:26 by lene, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg328299 - (view) Author: Lene Preuss (lene) Date: 2018-10-23 10:26
Obligatory apologies if this has been reported before, I could not find it. It is similar to but different from https://bugs.python.org/issue9334.

>>> from argparse import ArgumentParser
>>> p = ArgumentParser()
>>> p.add_argument('-d')
_StoreAction(option_strings=['-d'], dest='d', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)
>>> p.parse_args(['-d', '12'])
Namespace(d='12')
>>> p.parse_args(['-d', '1,2'])
Namespace(d='1,2')
>>> p.parse_args(['-d', '-12'])
Namespace(d='-12')
>>> p.parse_args(['-d', '-1,2'])
usage: [-h] [-d D]
: error: argument -d: expected one argument

As suggested in issue 9334, passing the argument with an equals sign works:
>>> p.parse_args(['-d=-1,2'])
Namespace(d='-1,2')

But I don't think that this is the intended behavior.
msg328389 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2018-10-24 19:54
I think it's the same issue. A dash argument that is not clearly a number is interpreted as an optional's flag.  With few exceptions, the parser does not examine the contents of the string

It tests the initial character, it tests for space, it tests for numbers (I'm not sure scientific notation is accepted).  That's about it.

Argparse has a minimum of constrains on what is a valid flag string.  For example, the following is ok:

parser.add_argument('-1,2')
# appearing the namespace as: Namespace(**{'1,2': None})
History
Date User Action Args
2022-04-11 14:59:07adminsetgithub: 79230
2018-10-24 19:54:37paul.j3setstatus: open -> closed
resolution: duplicate
stage: resolved
2018-10-24 19:54:02paul.j3setnosy: + paul.j3
messages: + msg328389
2018-10-23 10:29:51lenesetversions: + Python 3.7
2018-10-23 10:26:52lenecreate