[issue37510] argparse removing more "--" than it should
New submission from Jorge L. Martinez : $ python -c ' import argparse p = argparse.ArgumentParser() p.add_argument("first_arg") p.add_argument("args", nargs="*") r = p.parse_args(["foo", "--", "bar", "--", "baz", "--", "zap"]) print(r.first_arg + " " + " ".join(r.args)) ' returns: foo bar baz -- zap when I think it should return: foo bar -- baz -- zap -- components: Library (Lib) messages: 347378 nosy: jol priority: normal severity: normal status: open title: argparse removing more "--" than it should type: behavior versions: Python 3.7 ___ Python tracker <https://bugs.python.org/issue37510> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37510] argparse removing more "--" than it should
Jorge L. Martinez added the comment: Sorry, I forgot to add details on my machine. Python: 3.7.3 OS: Archlinux -- ___ Python tracker <https://bugs.python.org/issue37510> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37510] argparse removing more "--" than it should
Jorge L. Martinez added the comment: To be clear, my opinion is that a single call of parse_args() should only ever remove the first "--". Right now, it seems that it removes the first of each argument group, as determined by nargs, I guess. -- ___ Python tracker <https://bugs.python.org/issue37510> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37510] argparse removing more "--" than it should
Jorge L. Martinez added the comment: > There are earlier bug/issues about the '--'. Yes, there are: https://bugs.python.org/issue9571 https://bugs.python.org/issue3 https://bugs.python.org/issue14364 But this one seems separate. Though they're related, they don't seem like duplicates, so that's why I thought I'd make this one. > Also look at the parser code itself. Keep in mind that parsing is done in > two passes - once to identify flags versus arguments ('O/A') and then to > allocate strings to arguments. I don't recall when '--' is being handled, > possibly in both. I'm not sure what your point is here. I did take a quick look at the code, yesterday. I think the identification part you mention is done right. It marks 'O/A' as you mention. Then, when it sees "--", it marks it "-", and the rest of the arguments are marked as "A". I didn't look at the start of the code of the second pass or how it is concretely linked to the first pass. However, I did see that in the example I gave, _get_values() in argparse.py gets called twice (apparently once per argument group as determined by nargs, I guess) to remove the "--" present in arg_strings. The first time, arg_strings is ["foo", "--"] and the second time it's ["bar", "--", "baz", "--", "zap"]. So, that's what happens, and where part of the fix should probably be. I don't think the removal of "--" should happen in a function that gets called multiple times. Though I didn't spend the time to see where the code should be positioned, I can only imagine the correct behavior would be to remove the argument marked as "-" by the first pass mentioned. I didn't mention this yesterday, because I figured there wouldn't be much value in sharing incomplete research like this, as opposed to a patch. I didn't want to influence the work of whoever chose to invest time in this for a proper fix. -- ___ Python tracker <https://bugs.python.org/issue37510> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37510] argparse removing more "--" than it should
Jorge L. Martinez added the comment: > to remove the "--" present in arg_strings *to remove the first "--" present... -- ___ Python tracker <https://bugs.python.org/issue37510> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue37510] argparse removing more "--" than it should
Jorge L. Martinez added the comment: Maybe I can find the time to make a patch this weekend (either today or tomorrow). I hope I'm not underestimating this somehow, but I don't think this would take too long. The only issue I can foresee is in disagreement of what the correct behavior should be, which is why I gave my opinion that a single call of parse_args() should only ever remove a single "--". If I don't submit a patch by Monday (PDT), everyone should assume I decided not to tackle this. By the way, does this issue tracking platform support submitting to the issue thread by email? Maybe, I'll try that. -- ___ Python tracker <https://bugs.python.org/issue37510> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com