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 - "expected one argument" when used -: in argument
Type: compile error Stage:
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Pythass, eric.smith, paul.j3, rhettinger, xtreak
Priority: normal Keywords:

Created on 2022-03-13 08:29 by Pythass, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
test.py Pythass, 2022-03-13 08:29 Test file for triggering the argparse issue
Messages (5)
msg415031 - (view) Author: Pythass (Pythass) Date: 2022-03-13 08:29
By using argparse, and by passing a string as argument that has both a dash "-" and colon ":", python returns "expected one argument" error, despite it works when I use only "-" without ":".

An example of code is attached at the ticket.

Examples of triggering "expected one argument" error:
python test.py -u -1:00
python test.py -u -1:

Examples of NON triggering "expected one argument" error:
python test.py -u -1
python test.py -u=-1:00
msg415032 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2022-03-13 10:09
Here's a simplified reproducer:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--utc", choices=["-1:00"])
args = parser.parse_args()

I assume this is related to argparse guessing if an argument is a negative number. See https://stackoverflow.com/questions/9025204/python-argparse-issue-with-optional-arguments-which-are-negative-numbers

From a suggestion there, note that
python test.py -u ' -1:00'
doesn't give the "expected one argument" error. I realize that doesn't solve your problem, but it does shed some light on the issue.

I suspect this can't be fixed without breaking other usages of argparse.
msg415036 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2022-03-13 12:57
Seems related to https://bugs.python.org/issue9334
msg415068 - (view) Author: Pythass (Pythass) Date: 2022-03-13 18:06
The curious aspect is that for:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--utc", choices=["-1"])
args = parser.parse_args()

it works. But if we use the colon (:) character as:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--utc", choices=["-1:"])
args = parser.parse_args()

it does not work anymore... I could think maybe the issue could be related to the presence of ":" character together with "-" character.

For example for:
choices=["-:"] does not work
choices=["-"] works
choices=[":"] works
choices=[":-"] works

So, we get the error if the option start with "-" and has inside also ":" character. At this point I don't think it is related strictly to "negative numbers".
msg415097 - (view) Author: paul j3 (paul.j3) * (Python triager) Date: 2022-03-13 20:39
'-1' and '-1.23' are recognized as numbers, and treated as arguments.  '-1' requires some special handling because it is allowed as a flag, as in

    parser.add_argument('-1','--one')

'-1:00' on the other hand is no different from a string like '-foo'.  Default is to parse it as a flag.  If you don't get this error

    argument -f: expected one argument

you are likely to get:

    error: unrecognized arguments: -1:23

This can probably be closed as a duplicate of:

https://bugs.python.org/issue9334
argparse does not accept options taking arguments beginning with dash (regression from optparse)
History
Date User Action Args
2022-04-11 14:59:57adminsetgithub: 91158
2022-03-13 20:39:33paul.j3setmessages: + msg415097
2022-03-13 18:06:30Pythasssetmessages: + msg415068
2022-03-13 12:57:09xtreaksetnosy: + xtreak
messages: + msg415036
2022-03-13 12:53:33xtreaksetnosy: + rhettinger
2022-03-13 10:09:13eric.smithsetnosy: + eric.smith, paul.j3, - lys.nikolaou, pablogsal
messages: + msg415032
components: + Library (Lib), - Parser
2022-03-13 08:29:17Pythasscreate