[New-bugs-announce] [issue41359] argparse mutually exclusive group does not exclude in some cases

Krzysiek report at bugs.python.org
Tue Jul 21 07:46:47 EDT 2020


New submission from Krzysiek <krzychdid at interia.pl>:

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.

----------
components: Library (Lib)
messages: 374065
nosy: kkarbowiak
priority: normal
severity: normal
status: open
title: argparse mutually exclusive group  does not exclude in some cases
type: behavior
versions: Python 3.10

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41359>
_______________________________________


More information about the New-bugs-announce mailing list