Skip to content
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

Closed
Pythass mannequin opened this issue Mar 13, 2022 · 6 comments
Closed

argparse - "expected one argument" when used -: in argument #91158

Pythass mannequin opened this issue Mar 13, 2022 · 6 comments
Labels
3.10 only security fixes build The build process and cross-build stdlib Python modules in the Lib dir

Comments

@Pythass
Copy link
Mannequin

Pythass mannequin commented Mar 13, 2022

BPO 47002
Nosy @rhettinger, @ericvsmith, @tirkarthi
Files
  • test.py: Test file for triggering the argparse issue
  • 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:

    assignee = None
    closed_at = None
    created_at = <Date 2022-03-13.08:29:17.944>
    labels = ['build', 'library', '3.10']
    title = 'argparse - "expected one argument" when used -: in argument'
    updated_at = <Date 2022-03-13.20:39:33.905>
    user = 'https://bugs.python.org/Pythass'

    bugs.python.org fields:

    activity = <Date 2022-03-13.20:39:33.905>
    actor = 'paul.j3'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2022-03-13.08:29:17.944>
    creator = 'Pythass'
    dependencies = []
    files = ['50668']
    hgrepos = []
    issue_num = 47002
    keywords = []
    message_count = 5.0
    messages = ['415031', '415032', '415036', '415068', '415097']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'eric.smith', 'paul.j3', 'xtreak', 'Pythass']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'compile error'
    url = 'https://bugs.python.org/issue47002'
    versions = ['Python 3.10']

    @Pythass
    Copy link
    Mannequin Author

    Pythass mannequin commented Mar 13, 2022

    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

    @Pythass Pythass mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) 3.10 only security fixes build The build process and cross-build labels Mar 13, 2022
    @ericvsmith
    Copy link
    Member

    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.

    @ericvsmith ericvsmith added stdlib Python modules in the Lib dir and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Mar 13, 2022
    @tirkarthi
    Copy link
    Member

    Seems related to https://bugs.python.org/issue9334

    @Pythass
    Copy link
    Mannequin Author

    Pythass mannequin commented Mar 13, 2022

    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".

    @paulj3
    Copy link
    Mannequin

    paulj3 mannequin commented Mar 13, 2022

    '-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)

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @rhettinger
    Copy link
    Contributor

    Closing as a duplicate.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.10 only security fixes build The build process and cross-build stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants