This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author kkarbowiak
Recipients kkarbowiak
Date 2020-07-21.11:46:47
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1595332007.75.0.283126282834.issue41359@roundup.psfhosted.org>
In-reply-to
Content
The documentation for `ArgumentParser.add_mutually_exclusive_group` states: "argparse will make sure that only one of the arguments in the mutually exclusive group was present on the command line".

This is not the case in certain circumstances:

```python
import argparse

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-a')
group.add_argument('-b', nargs='?')

parser.parse_args('-a a -b'.split())
```

The above code does not produce any error, even though both exclusive arguments are present.

My guess is that the check for mutual exclusion is not done during processing of each command line argument, but rather afterwards. It seems the check only ensures at most one argument from group is not `None`.

The issue exists at least on Python 2.7.13, 3.6, 3.7.5, 3.8, and 3.10.
History
Date User Action Args
2020-07-21 11:46:47kkarbowiaksetrecipients: + kkarbowiak
2020-07-21 11:46:47kkarbowiaksetmessageid: <1595332007.75.0.283126282834.issue41359@roundup.psfhosted.org>
2020-07-21 11:46:47kkarbowiaklinkissue41359 messages
2020-07-21 11:46:47kkarbowiakcreate