[issue15271] argparse: repeatedly specifying the same argument ignores the previous ones
Steven Bethard steven.beth...@gmail.com added the comment: I don't think this is a bug. You've specified two arguments with the same destination, foo. This means that argparse will parse the first one, assign it to the attribute foo and then parse the second one and assign it to the attribute foo again, overwriting the previous one. So you only see the second one, but the first one wasn't ignored. If you didn't want them to overwrite the same attribute, you either need to declare them as action=append so that they both add to the same attribute, or you need to declare them with different destinations (and use metavar=foo if you want them to display the same way). -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15271 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15271] argparse: repeatedly specifying the same argument ignores the previous ones
Steven Bethard steven.beth...@gmail.com added the comment: I'm going to close this issue, since argparse is doing what you've asked it to, even if that wasn't what you expected. But I think the documentation for this kind of thing could be improved. If you'd like to help document the workaround for your problem that I described, please contribute to Issue 15428. -- resolution: - invalid status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15271 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15271] argparse: repeatedly specifying the same argument ignores the previous ones
New submission from Ionuț Arțăriși io...@artarisi.eu: To reproduce: import argparse [74536 refs] parser = argparse.ArgumentParser() [74809 refs] parser.add_argument(foo) parser.add_argument(foo) parser.parse_args([bar]) usage: ipython [-h] foo foo ipython: error: too few arguments An exception has occurred, use %tb to see the full traceback. SystemExit: 2 parser.parse_args([bar, baz]) Namespace(foo='baz') So it actually makes you provide two arguments, but it loses/ignores the first one and there's no way to get it back. -- messages: 164791 nosy: bethard, mapleoin priority: normal severity: normal status: open title: argparse: repeatedly specifying the same argument ignores the previous ones type: behavior ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15271 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15271] argparse: repeatedly specifying the same argument ignores the previous ones
Florent Xicluna florent.xicl...@gmail.com added the comment: Confirmed. It should probably raise an ArgumentError like this one. http://docs.python.org/library/argparse.html#conflict-handler -- components: +Library (Lib) nosy: +flox stage: - needs patch versions: +Python 3.3 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15271 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15271] argparse: repeatedly specifying the same argument ignores the previous ones
Ionuț Arțăriși io...@artarisi.eu added the comment: So I was looking into this and it seems that there are (at least) two contradicting test cases. When inheriting a parser from two parents, there are two different behaviours for positionals and for options. In the case of positionals, there is this test: def test_same_argument_name_parents(self): parents = [self.wxyz_parent, self.z_parent] parser = ErrorRaisingArgumentParser(parents=parents) self.assertEqual(parser.parse_args('1 2'.split()), NS(w=None, y=None, z='2')) and this is the context from higher up: self.wxyz_parent.add_argument('z') ... self.z_parent.add_argument('z') So the tests don't expect an error when two parents provide the same argument. Instead, the eating up of the first argument (in this case '1') seems to be condoned? When I tried to change the conflict_handler during parent action merging to 'resolve' I got another test failing: def test_conflicting_parents(self): self.assertRaises( argparse.ArgumentError, argparse.ArgumentParser, parents=[self.w_parent, self.wxyz_parent]) context: self.wxyz_parent.add_argument('--w') ... self.w_parent.add_argument('--w') This tests that two parents which provide the exact same option will raise an error. So I think the first test is wrong and should be modified to expect an error in the case where two arguments with the same name are provided. Or is there some use case where this behaviour makes sense? -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15271 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue15271] argparse: repeatedly specifying the same argument ignores the previous ones
Ionuț Arțăriși io...@artarisi.eu added the comment: Here's a stab at a patch to consider conflicts between positionals. Right now conflict resolution is handled the same as in the case of options. -- keywords: +patch Added file: http://bugs.python.org/file26303/disallow_duplicate_positionals.diff ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue15271 ___ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com