diff --git Lib/argparse.py Lib/argparse.py index b44fa4f0f6..53310b95be 100644 --- Lib/argparse.py +++ Lib/argparse.py @@ -1437,7 +1437,7 @@ def add_argument(self, *args, **kwargs): return self._add_action(action) - def add_argument_group(self, *args, **kwargs): + def _add_argument_group(self, *args, **kwargs): group = _ArgumentGroup(self, *args, **kwargs) self._action_groups.append(group) return group @@ -1635,6 +1635,7 @@ def __init__(self, container, title=None, description=None, **kwargs): self._has_negative_number_optionals = \ container._has_negative_number_optionals self._mutually_exclusive_groups = container._mutually_exclusive_groups + self._container = container def _add_action(self, action): action = super(_ArgumentGroup, self)._add_action(action) @@ -1645,13 +1646,21 @@ def _remove_action(self, action): super(_ArgumentGroup, self)._remove_action(action) self._group_actions.remove(action) + def add_argument_group(self, *args, **kwargs): + if isinstance(self, _ArgumentGroup) and not isinstance(self, _MutuallyExclusiveGroup): + from warnings import warn + warn('Adding an argument group to another group is deprecated, and not supported', DeprecationWarning, stacklevel=2) + # actions are added to the correct object with properly printed help + return self + else: + return super()._add_argument_group(self, *args, **kwargs) + class _MutuallyExclusiveGroup(_ArgumentGroup): def __init__(self, container, required=False): super(_MutuallyExclusiveGroup, self).__init__(container) self.required = required - self._container = container def _add_action(self, action): if action.required: @@ -1665,6 +1674,15 @@ def _remove_action(self, action): self._container._remove_action(action) self._group_actions.remove(action) + add_argument_group = _ArgumentGroup._add_argument_group + + def add_mutually_exclusive_group(self, *args, **kwargs): + if isinstance(self._container, _MutuallyExclusiveGroup): + from warnings import warn + warn('Adding a mutually exclusive group to another mutually exclusive group is deprecated, and not supported', + DeprecationWarning, stacklevel=2) + + return super()._add_argument_group(self, *args, **kwargs) class ArgumentParser(_AttributeHolder, _ActionsContainer): """Object for parsing command line strings into Python objects. @@ -1750,6 +1768,8 @@ def identity(string): else: self._defaults.update(defaults) + add_argument_group = _ActionsContainer._add_argument_group + # ======================= # Pretty __repr__ methods # =======================