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 parsing (mingling --option and optional positional argument) #68411

Closed
bobjalex mannequin opened this issue May 18, 2015 · 3 comments
Closed

argparse parsing (mingling --option and optional positional argument) #68411

bobjalex mannequin opened this issue May 18, 2015 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@bobjalex
Copy link
Mannequin

bobjalex mannequin commented May 18, 2015

BPO 24223
Nosy @vadmium
Superseder
  • bpo-15112: argparse: nargs='*' positional argument doesn't accept any items if preceded by an option and another positional
  • 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 = <Date 2015-05-18.03:29:16.860>
    created_at = <Date 2015-05-18.01:22:12.448>
    labels = ['type-bug', 'library']
    title = 'argparse parsing (mingling --option and optional positional argument)'
    updated_at = <Date 2015-05-21.16:46:53.110>
    user = 'https://bugs.python.org/bobjalex'

    bugs.python.org fields:

    activity = <Date 2015-05-21.16:46:53.110>
    actor = 'bobjalex'
    assignee = 'none'
    closed = True
    closed_date = <Date 2015-05-18.03:29:16.860>
    closer = 'martin.panter'
    components = ['Library (Lib)']
    creation = <Date 2015-05-18.01:22:12.448>
    creator = 'bobjalex'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 24223
    keywords = []
    message_count = 3.0
    messages = ['243447', '243452', '243763']
    nosy_count = 2.0
    nosy_names = ['martin.panter', 'bobjalex']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '15112'
    type = 'behavior'
    url = 'https://bugs.python.org/issue24223'
    versions = ['Python 3.4']

    @bobjalex
    Copy link
    Mannequin Author

    bobjalex mannequin commented May 18, 2015

    Here is simple example of failure to parse arguments that should parse OK. In the following little program, the second from last line contains an aargument sequence that parses OK, but the last line should but doesn't.

    import argparse
    ap = argparse.ArgumentParser()
    ap.add_argument("--option", action="store_true")
    ap.add_argument("arg_1")
    ap.add_argument("arg_2", nargs="?")
    print("test 1:", ap.parse_args(["abc", "mmm", "--option"]))
    print("test 2:", ap.parse_args(["abc", "--option", "mmm"]))

    @bobjalex bobjalex mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 18, 2015
    @vadmium
    Copy link
    Member

    vadmium commented May 18, 2015

    I suggest this is a duplicate of bpo-15112. The same problem also happens with nargs="*", and that issue apparently has a patch to handle both cases.

    For the record, this is the resulting error, and a demo that it works if the option comes before the positional arguments:

    >>> print("test 2:", ap.parse_args(["abc", "--option", "mmm"]))
    usage: [-h] [--option] arg_1 [arg_2]
    : error: unrecognized arguments: mmm
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/home/proj/python/cpython/Lib/argparse.py", line 1729, in parse_args
        self.error(msg % ' '.join(argv))
      File "/home/proj/python/cpython/Lib/argparse.py", line 2385, in error
        self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
      File "/home/proj/python/cpython/Lib/argparse.py", line 2372, in exit
        _sys.exit(status)
      File "/home/pythonstartup.py", line 345, in exit
        raise SystemExit(code)
    __main__.SystemExit: 2
    >>> ap.parse_args(["--option", "abc", "mmm"])
    Namespace(arg_1='abc', arg_2='mmm', option=True)

    @vadmium vadmium closed this as completed May 18, 2015
    @vadmium vadmium changed the title argparse parsing bug argparse parsing (mingling --option and optional positional argument) May 18, 2015
    @bobjalex
    Copy link
    Mannequin Author

    bobjalex mannequin commented May 21, 2015

    Thanks for the note, Martin. I agree that it's a duplicate. (I had done a
    brief search for possible dups, but didn't find that one!)

    Bob

    On Sun, May 17, 2015 at 8:29 PM, Martin Panter <report@bugs.python.org>
    wrote:

    >
    > Martin Panter added the comment:
    >
    > I suggest this is a duplicate of Issue 15112. The same problem also
    > happens with nargs="*", and that issue apparently has a patch to handle
    > both cases.
    >
    > For the record, this is the resulting error, and a demo that it works if
    > the option comes before the positional arguments:
    >
    > >>> print("test 2:", ap.parse_args(["abc", "--option", "mmm"]))
    > usage: [-h] [--option] arg_1 [arg_2]
    > : error: unrecognized arguments: mmm
    > Traceback (most recent call last):
    >   File "<stdin>", line 1, in <module>
    >   File "/home/proj/python/cpython/Lib/argparse.py", line 1729, in
    > parse_args
    >     self.error(msg % ' '.join(argv))
    >   File "/home/proj/python/cpython/Lib/argparse.py", line 2385, in error
    >     self.exit(2, _('%(prog)s: error: %(message)s\n') % args)
    >   File "/home/proj/python/cpython/Lib/argparse.py", line 2372, in exit
    >     _sys.exit(status)
    >   File "/home/pythonstartup.py", line 345, in exit
    >     raise SystemExit(code)
    > __main__.SystemExit: 2
    > >>> ap.parse_args(["--option", "abc", "mmm"])
    > Namespace(arg_1='abc', arg_2='mmm', option=True)
    >
    > 

    nosy: +vadmium
    resolution: -> duplicate
    stage: -> resolved
    status: open -> closed
    superseder: -> argparse: nargs='*' positional argument doesn't accept any
    items if preceded by an option and another positional
    title: argparse parsing bug -> argparse parsing (mingling --option and
    optional positional argument)


    Python tracker <report@bugs.python.org>
    <http://bugs.python.org/issue24223\>


    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant