| 2024 | # ================================== |
| 2025 | |
| 2026 | def add_subparsers(self, **kwargs): |
| 2027 | if self._subparsers is not None: |
| 2028 | raise ValueError('cannot have multiple subparser arguments') |
| 2029 | |
| 2030 | # add the parser class to the arguments if it's not present |
| 2031 | kwargs.setdefault('parser_class', type(self)) |
| 2032 | |
| 2033 | if 'title' in kwargs or 'description' in kwargs: |
| 2034 | title = kwargs.pop('title', _('subcommands')) |
| 2035 | description = kwargs.pop('description', None) |
| 2036 | self._subparsers = self.add_argument_group(title, description) |
| 2037 | else: |
| 2038 | self._subparsers = self._positionals |
| 2039 | |
| 2040 | # prog defaults to the usage message of this parser, skipping |
| 2041 | # non-required optional arguments and with no "usage:" prefix |
| 2042 | if kwargs.get('prog') is None: |
| 2043 | # Create formatter without color to avoid storing ANSI codes in prog |
| 2044 | formatter = self.formatter_class(prog=self.prog) |
| 2045 | formatter._set_color(False) |
| 2046 | positionals = self._get_positional_actions() |
| 2047 | required_optionals = [action for action in self._get_optional_actions() |
| 2048 | if action.required] |
| 2049 | groups = self._mutually_exclusive_groups |
| 2050 | formatter.add_usage(None, required_optionals + positionals, groups, '') |
| 2051 | kwargs['prog'] = formatter.format_help().strip() |
| 2052 | |
| 2053 | # create the parsers action and add it to the positionals list |
| 2054 | parsers_class = self._pop_action_class(kwargs, 'parsers') |
| 2055 | action = parsers_class(option_strings=[], **kwargs) |
| 2056 | action._color = self.color |
| 2057 | self._check_help(action) |
| 2058 | self._subparsers._add_action(action) |
| 2059 | |
| 2060 | # return the created parsers action |
| 2061 | return action |
| 2062 | |
| 2063 | def _add_action(self, action): |
| 2064 | if action.option_strings: |