import argparse # argparse help error: arguments created by add_mutually_exclusive_group() are shown outside their parent group created by add_argument_group() # prelimiary fix to the function that copies a parent parser to child def new_container_add(self, container): # collect groups by titles title_group_map = {} for group in self._action_groups: if group.title in title_group_map: msg = _('cannot merge actions - two groups are named %r') raise ValueError(msg % (group.title)) title_group_map[group.title] = group # map each action to its group group_map = {} for group in container._action_groups: # if a group with the title exists, use that, otherwise # create a new group matching the container's group if group.title not in title_group_map: title_group_map[group.title] = self.add_argument_group( title=group.title, description=group.description, conflict_handler=group.conflict_handler) # map the actions to their new group for action in group._group_actions: group_map[action] = title_group_map[group.title] # add container's mutually exclusive groups # NOTE: if add_mutually_exclusive_group ever gains title= and # description= then this code will need to be expanded as above for group in container._mutually_exclusive_groups: #print('container title',group._container.title) mutex_group = self.add_mutually_exclusive_group( required=group.required) # correct the _container attribute of the copied group mx_container = title_group_map[group._container.title] ##### mutex_group._container = mx_container ##### # map the actions to their new mutex group for action in group._group_actions: group_map[action] = mutex_group # add all actions to this container or their group for action in container._actions: group_map.get(action, self)._add_action(action) argparse._ActionsContainer._add_container_actions = new_container_add # create parent parser parent_parser = argparse.ArgumentParser(add_help = False) # create group for its arguments global_group = parent_parser.add_argument_group("global arguments") global_group.add_argument("--global-arg1") global_group.add_argument("--global-arg2") mutex_group = global_group.add_mutually_exclusive_group() mutex_group.add_argument("--mutex-arg1") mutex_group.add_argument("--mutex-arg2") # create child parser child_parser = argparse.ArgumentParser(parents = [parent_parser]) child_parser.add_argument("--child-arg1") child_parser.add_argument("--child-arg2") print("="*100) parent_parser.print_help() print("="*100) child_parser.print_help()