[go: nahoru, domu]

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 mis-handles a store_true option in between two nargs="?" positionals #121282

Open
zackw opened this issue Jul 2, 2024 · 0 comments
Open
Labels
type-bug An unexpected behavior, bug, or error

Comments

@zackw
Copy link
zackw commented Jul 2, 2024

Bug report

Bug description:

Consider the following cut-down script:

import argparse
import pprint

ap = argparse.ArgumentParser()
ap.add_argument("p", nargs="?")
ap.add_argument("q", nargs="?")
ap.add_argument("--test", action="store_true")

args = ap.parse_args()
pprint.pprint(args.__dict__)

When invoked with two positional arguments and --test on either side of them, the behavior is as expected:

$ python3 test.py --test a b
{'p': 'a', 'q': 'b', 'test': True}
$ python3 test.py a b --test
{'p': 'a', 'q': 'b', 'test': True}

However, if you put --test in between the two positionals, argparse misinterprets the argument list:

$ python3 test.py a --test b
usage: test.py [-h] [--test] [p] [q]
test.py: error: unrecognized arguments: b

This argument list should produce the same Namespace object as the first two examples.

CPython versions tested on:

3.9, 3.11, 3.12

Operating systems tested on:

Linux

@zackw zackw added the type-bug An unexpected behavior, bug, or error label Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant