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
Comments
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"])) |
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) |
Thanks for the note, Martin. I agree that it's a duplicate. (I had done a Bob On Sun, May 17, 2015 at 8:29 PM, Martin Panter <report@bugs.python.org> >
> 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)
>
>
|
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: