python / cpython

The Python programming language
https://www.python.org
Other
63.53k stars 30.44k forks source link

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

Open 335075a5-9147-4ab8-9a54-c8e4934a7d22 opened 10 years ago

335075a5-9147-4ab8-9a54-c8e4934a7d22 commented 10 years ago
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: ```python assignee = None closed_at = None created_at = labels = ['type-bug', 'library'] title = "argparse not including '--' arguments in previous optional REMAINDER argument" updated_at = user = 'https://bugs.python.org/JurkoGospodneti' ``` bugs.python.org fields: ```python activity = actor = 'Juraj.Ivancic' assignee = 'none' closed = False closed_date = None closer = None components = ['Library (Lib)'] creation = 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

335075a5-9147-4ab8-9a54-c8e4934a7d22 commented 10 years ago

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ć

335075a5-9147-4ab8-9a54-c8e4934a7d22 commented 10 years ago

Might be related to the following issues:

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

serhiy-storchaka commented 1 month ago

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.