Skip to content

error mixing positional and non-positional arguments with argparse #76217

Closed

Description

@stefanseefeld
BPO 32036
Nosy @stefanseefeld, @bitdancer
Files
  • test_argparse.py: script demonstrating inconsistent command-line parsing behaviour
  • 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 2017-11-15.23:07:01.834>
    created_at = <Date 2017-11-15.16:36:45.147>
    labels = ['type-bug', 'library']
    title = 'error mixing positional and non-positional arguments with `argparse`'
    updated_at = <Date 2017-11-15.23:07:01.833>
    user = 'https://github.com/stefanseefeld'

    bugs.python.org fields:

    activity = <Date 2017-11-15.23:07:01.833>
    actor = 'r.david.murray'
    assignee = 'none'
    closed = True
    closed_date = <Date 2017-11-15.23:07:01.834>
    closer = 'r.david.murray'
    components = ['Library (Lib)']
    creation = <Date 2017-11-15.16:36:45.147>
    creator = 'stefan'
    dependencies = []
    files = ['47268']
    hgrepos = []
    issue_num = 32036
    keywords = []
    message_count = 5.0
    messages = ['306283', '306291', '306294', '306302', '306317']
    nosy_count = 2.0
    nosy_names = ['stefan', 'r.david.murray']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32036'
    versions = []

    Activity

    1. stefanseefeld commented on Nov 15, 2017

      stefanseefeldmannequin
      MannequinAuthor

      I'm trying to mix positional and non-positional arguments with a script using argparse, but I observe inconsistent behaviour.
      The attached test runs fine when invoked with

      test_argparse.py --info a a=b
      test_argparse.py a a=b --info

      but produces the error error: unrecognized arguments: a=b when invoked as

      test_argparse.py a --info a=b

      Is this intended behaviour ? If yes, is this documented ? If not, is there a way to make this work with existing argparse versions ?

    2. added
      stdlib
      type-bug
      on Nov 15, 2017
    3. bitdancer commented on Nov 15, 2017

      @bitdancer
      Member

      Can you reproduce this without your PosArgsParser?

    4. stefanseefeld commented on Nov 15, 2017

      stefanseefeldmannequin
      MannequinAuthor

      On 15.11.2017 12:54, R. David Murray wrote:

      Can you reproduce this without your PosArgsParser?
      I can indeed (by simply commenting out the action argument to the
      add_argument() calls).
      That obviously results in all positional arguments being accumulated in
      the goal member, as there is no logic to distinguish a from a=b
      semantically.

    5. stefanseefeld commented on Nov 15, 2017

      stefanseefeldmannequin
      MannequinAuthor

      It looks like https://bugs.python.org/issue14191 is a conversation about the same inconsistent behaviour. It is set to "fixed". Can you comment on this ? Should I follow the advice mentioned there about how to work around the issue ?

    6. bitdancer commented on Nov 15, 2017

      @bitdancer
      Member

      Ah, yes, I'd managed to forget about that issue, even though I was the one that finally committed the fix.

      So, the alternate parser will be in 3.7. In the meantime you can grab the code from the commit and put it in a local file, I think. You'll doubtless have to tweak a few things to use it that way, but it should be fairly straightforward.

    7. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      stdlibtype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions