-
-
Notifications
You must be signed in to change notification settings - Fork 29.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
argparse: arg groups and mutually exclusive groups behave inconsitently #90216
Comments
I tried to add arguments to process DB-related settings, either from typing import Optional # 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:
What is expected:
|
Checking the code the add_mutually_exclusive_group has only kwargs, so one part can be fixed (POC, breaks tests): --- Lib/argparse.py class _MutuallyExclusiveGroup(_ArgumentGroup): - def __init__(self, container, required=False):
- super(_MutuallyExclusiveGroup, self).__init__(container)
+ def __init__(self, container, required=False, title=None, description=None):
+ super(_MutuallyExclusiveGroup, self).__init__(container, title, description)
self.required = required
self._container = container @@ -2529,6 +2529,14 @@ def format_help(self): + for mutual_group in self._mutually_exclusive_groups: |
Nesting argument groups and mutually exclusive groups is now deprecated (see bpo-22047). Thank you for the bug report. Note Paul's comment about why nesting mutually exclusive groups does not give you anything in terms of semantics. |
At least until these latest deprecations, the only nesting that made sense was to put a mutually_exclusive_group inside an argument_group. This was a way of providing a title and description for the exclusive_group. (And not documented.) I don't know if that's still possible. 'required' only makes sense for the exclusive_group. I don't know what happens when one tries to give it to an argument_group. If it doesn't raise an error, I expect it to be ignored. argument_groups are only used for help formatting; they have no role in parsing. exclusive_groups are primarily a parsing checking tool. Usage formatting tries to display exclusive groups, but is easily broken. While mutually_exclusive_group is a subclass of argument_group (and that in turn a subclass of argument_container), very little usage or behavior is inherited. Don't expect any consistency. A key point, that is easily lost, is that all groups share the _actions list with the parser. When an argument is added a group (either kind), it is, in effect, added to the parser's _actions list. So when parsing, there's only one list of Actions. A group will also keep the Action in its own _group_actions list, which is used for formatting or for exclusive checking. But otherwise the _group_actions list not used for parsing. Another point, is that there are 2 default argument_groups. Thus every Action is in an _group_actions list. If an exclusive_group is not nested in an argument_group, its Actions will be added to one of the defaults (optionals or positionals). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: