[issue43220] Explicit default required arguments with add_mutually_exclusive_group are rejected

Keith Smiley report at bugs.python.org
Sun Feb 14 01:34:45 EST 2021


New submission from Keith Smiley <keithbsmiley at gmail.com>:

With this code:

```
import argparse

parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--foo", default="1")
group.add_argument("--bar")
args = parser.parse_args()
print(args)
```

When you explicitly pass `--foo 1`, it is treated as if no argument was passed:

```
% python3 /tmp/bug.py --foo 1
usage: bug.py [-h] (--foo FOO | --bar BAR)
bug.py: error: one of the arguments --foo --bar is required
```

I can't tell if this behavior is intentional, but it was surprising to me. It also seems to be somewhat based on the length of the default string. For example on my macOS machine if I change the default to `longerstring` it does not have this issue.

----------
components: Library (Lib)
messages: 386934
nosy: keith
priority: normal
severity: normal
status: open
title: Explicit default required arguments with add_mutually_exclusive_group are rejected
versions: Python 3.10, Python 3.6, Python 3.7, Python 3.8, Python 3.9

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


More information about the Python-bugs-list mailing list