New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
argparse - "expected one argument" when used -: in argument #91158
Comments
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: Examples of NON triggering "expected one argument" error: |
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 I suspect this can't be fixed without breaking other usages of argparse. |
Seems related to https://bugs.python.org/issue9334 |
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: 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: 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". |
'-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
you are likely to get:
This can probably be closed as a duplicate of: https://bugs.python.org/issue9334 |
Closing as a duplicate. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: