[New-bugs-announce] [issue46058] argparse: arg groups and mutually exclusive groups behave inconsitently
László Attila Tóth
report at bugs.python.org
Sun Dec 12 14:01:14 EST 2021
New submission from László Attila Tóth <laszlo.attila.toth at gmail.com>:
I tried to add arguments to process DB-related settings, either from typing import Optional
from a file
or explicitly specifying them. In theory with nested groups (by add_argument_group
and add_mutually_exlusive_group) this can be implemented in almost straightforward way:
# test.py
import argparse
parser = argparse.ArgumentParser()
dbsettings = parser.add_argument_group('Database settings')
xdbgrp = dbsettings.add_mutually_exclusive_group(required=True)
xdbgrp.add_argument('--db-config')
grp = xdbgrp.add_argument_group(required=True)
grp.add_argument('--db-host')
grp.add_argument('--db-user')
xgrp = grp.add_mutually_exclusive_group()
xgrp.add_argument('--db-password')
xgrp.add_argument('--db-password-file')
parser.parse_args()
But there are issues:
1) the add_mutually_exclusive_group has only one optional parameter, required=False by default,
so I cannot provide a title, I have to create yet another group (xdbgrp in the example)
2) I would expect the parser do the complete argument parsing and validation, so I don't
need to implement certain steps. In this example I expect to have a --db-host arg
if the --db-config is not specified. But if I add ``required=True``, the argparse
module expects that with --db-config the --db-host is also specified.
In other words the xdbgrp mutually exclusive group fails to be mutually exclusive.
3) While xgrp behaves correctly, I cannot specify both --db-password and --db-password-file,
I still can specify them with --db-config (see #2)
4) If I run it as: python3 test.py --db-host x
the command fails:
usage: test.py [-h] --db-config DB_CONFIG --db-host DB_HOST [--db-user DB_USER]
[--db-password DB_PASSWORD | --db-password-file DB_PASSWORD_FILE]
test.py: error: one of the arguments --db-config is required
So both --db-config and --db-host are required, the embedded group, grp fails to work,
or prehaps again the xdbgrp fails (depends on the view)
5) Removing all required=True options the following is accepted:
python3 test.py --db-host x --db-config y
so the xdbgrp mutually exclusive group again doesn't work.
6) Only xdbgrp is required, --db-host is not:
python3 test.py --db-host x
usage: test.py [-h] --db-config DB_CONFIG [--db-host DB_HOST] [--db-user DB_USER]
[--db-password DB_PASSWORD | --db-password-file DB_PASSWORD_FILE]
test.py: error: one of the arguments --db-config is required
Again, the group embedded into a mutually exclusive group is not handled correctly
What is expected:
1) add_mutually_exclusive_group can have title/description, but unfortunately it is not
backward compatible
2) If I add a mutally exclusive group, it has XOR relation between its arguments and
argument groups.
3) An argument group is handled as a single entity similar to an argument.
Basically this is the same as #2.
4) A required argument affects only its argument group and the parent group
and so on till the parser, but this chain stops at a mutually exclusive group,
based on #2 and #3 .
----------
components: Library (Lib)
messages: 408405
nosy: Laszlo.Attila.Toth
priority: normal
severity: normal
status: open
title: argparse: arg groups and mutually exclusive groups behave inconsitently
type: behavior
versions: Python 3.10, Python 3.11, Python 3.6, Python 3.7, Python 3.8, Python 3.9
_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46058>
_______________________________________
More information about the New-bugs-announce
mailing list