Skip to content

argparse not including '--' arguments in previous optional REMAINDER argument #66419

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

Open
JurkoGospodneti mannequin opened this issue Aug 18, 2014 · 3 comments · May be fixed by #124509
Open

argparse not including '--' arguments in previous optional REMAINDER argument #66419

JurkoGospodneti mannequin opened this issue Aug 18, 2014 · 3 comments · May be fixed by #124509
Assignees
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@JurkoGospodneti
Copy link
Mannequin

JurkoGospodneti mannequin commented Aug 18, 2014

BPO 22223

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 2014-08-18.09:46:02.121>
labels = ['type-bug', 'library']
title = "argparse not including '--' arguments in previous optional REMAINDER argument"
updated_at = <Date 2014-08-20.13:47:19.982>
user = 'https://bugs.python.org/JurkoGospodneti'

bugs.python.org fields:

activity = <Date 2014-08-20.13:47:19.982>
actor = 'Juraj.Ivancic'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2014-08-18.09:46:02.121>
creator = 'Jurko.Gospodneti\xc4\x87'
dependencies = []
files = []
hgrepos = []
issue_num = 22223
keywords = []
message_count = 2.0
messages = ['225484', '225485']
nosy_count = 2.0
nosy_names = ['Jurko.Gospodneti\xc4\x87', 'Juraj.Ivancic']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue22223'
versions = ['Python 2.7', 'Python 3.4']

Linked PRs

@JurkoGospodneti
Copy link
Mannequin Author

JurkoGospodneti mannequin commented Aug 18, 2014

If you have an optional nargs=argparse.REMAINDER argument defined
then, if specified, its value should contain all the remaining command-line content. However, it seems that value does not include later '--' arguments.

For example, the following works as expected:

import argparse
parser = argparse.ArgumentParser(prog="PROG")
parser.add_argument("--args", nargs=argparse.REMAINDER)
args = parser.parse_args("--args cmd --arg1 XX ZZ".split())
assert args.args == ["cmd", "--arg1", "XX", "ZZ"]

But the following fails with
'PROG: error: unrecognized arguments: -- ZZ':

import argparse
parser = argparse.ArgumentParser(prog="PROG")
parser.add_argument("--args", nargs=argparse.REMAINDER)
args = parser.parse_args("--args cmd --arg1 XX -- ZZ".split())
assert args.args == ["cmd", "--arg1", "XX", "--", "ZZ"]

Note that the same code works as expected when using a
positional nargs=argparse.REMAINDER arguments:

import argparse
parser = argparse.ArgumentParser(prog="PROG")
parser.add_argument("args", nargs=argparse.REMAINDER)
args = parser.parse_args("args cmd --arg1 XX ZZ".split())
assert args.args == ["cmd", "--arg1", "XX", "ZZ"]
args = parser.parse_args("args cmd --arg1 XX -- ZZ".split())
assert args.args == ["cmd", "--arg1", "XX", "--", "ZZ"]

But that, of course, does not allow us to have the args value
start with something like "--blah" that looks like an optional
argument.

Hope this helps.

Best regards,
Jurko Gospodnetić

@JurkoGospodneti JurkoGospodneti mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 18, 2014
@JurkoGospodneti
Copy link
Mannequin Author

JurkoGospodneti mannequin commented Aug 18, 2014

Might be related to the following issues:

http://bugs.python.org/issue9571
http://bugs.python.org/issue13922

@serhiy-storchaka
Copy link
Member

REMAINDER is currently undocumented (see bpo-17050/gh-61252), but the proposed change looks right. This would allow to use REMAINDER to simulate a subparser with option as the command (currently the subparser machinery does not support this). I believe it is just an omission that this was not supported before.

serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Sep 25, 2024
…all arguments

It no longer stops at the first '--'.
@serhiy-storchaka serhiy-storchaka self-assigned this Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
Status: Bugs
2 participants