| 1316 | metavar=metavar) |
| 1317 | |
| 1318 | def add_parser(self, name, *, deprecated=False, **kwargs): |
| 1319 | # set prog from the existing prefix |
| 1320 | if kwargs.get('prog') is None: |
| 1321 | kwargs['prog'] = '%s %s' % (self._prog_prefix, name) |
| 1322 | |
| 1323 | # set color |
| 1324 | if kwargs.get('color') is None: |
| 1325 | kwargs['color'] = self._color |
| 1326 | |
| 1327 | aliases = kwargs.pop('aliases', ()) |
| 1328 | |
| 1329 | if name in self._name_parser_map: |
| 1330 | raise ValueError(f'conflicting subparser: {name}') |
| 1331 | for alias in aliases: |
| 1332 | if alias in self._name_parser_map: |
| 1333 | raise ValueError(f'conflicting subparser alias: {alias}') |
| 1334 | |
| 1335 | # create a pseudo-action to hold the choice help |
| 1336 | if 'help' in kwargs: |
| 1337 | help = kwargs.pop('help') |
| 1338 | choice_action = self._ChoicesPseudoAction(name, aliases, help) |
| 1339 | self._choices_actions.append(choice_action) |
| 1340 | else: |
| 1341 | choice_action = None |
| 1342 | |
| 1343 | # create the parser and add it to the map |
| 1344 | parser = self._parser_class(**kwargs) |
| 1345 | if choice_action is not None: |
| 1346 | parser._check_help(choice_action) |
| 1347 | self._name_parser_map[name] = parser |
| 1348 | |
| 1349 | # make parser available under aliases also |
| 1350 | for alias in aliases: |
| 1351 | self._name_parser_map[alias] = parser |
| 1352 | |
| 1353 | if deprecated: |
| 1354 | self._deprecated.add(name) |
| 1355 | self._deprecated.update(aliases) |
| 1356 | |
| 1357 | return parser |
| 1358 | |
| 1359 | def _get_subactions(self): |
| 1360 | return self._choices_actions |