| 1240 | metavar=metavar) |
| 1241 | |
| 1242 | def add_parser(self, name, *, deprecated=False, **kwargs): |
| 1243 | # set prog from the existing prefix |
| 1244 | if kwargs.get('prog') is None: |
| 1245 | kwargs['prog'] = '%s %s' % (self._prog_prefix, name) |
| 1246 | |
| 1247 | # set color |
| 1248 | if kwargs.get('color') is None: |
| 1249 | kwargs['color'] = self._color |
| 1250 | |
| 1251 | aliases = kwargs.pop('aliases', ()) |
| 1252 | |
| 1253 | if name in self._name_parser_map: |
| 1254 | raise ValueError(f'conflicting subparser: {name}') |
| 1255 | for alias in aliases: |
| 1256 | if alias in self._name_parser_map: |
| 1257 | raise ValueError(f'conflicting subparser alias: {alias}') |
| 1258 | |
| 1259 | # create a pseudo-action to hold the choice help |
| 1260 | if 'help' in kwargs: |
| 1261 | help = kwargs.pop('help') |
| 1262 | choice_action = self._ChoicesPseudoAction(name, aliases, help) |
| 1263 | self._choices_actions.append(choice_action) |
| 1264 | else: |
| 1265 | choice_action = None |
| 1266 | |
| 1267 | # create the parser and add it to the map |
| 1268 | parser = self._parser_class(**kwargs) |
| 1269 | if choice_action is not None: |
| 1270 | parser._check_help(choice_action) |
| 1271 | self._name_parser_map[name] = parser |
| 1272 | |
| 1273 | # make parser available under aliases also |
| 1274 | for alias in aliases: |
| 1275 | self._name_parser_map[alias] = parser |
| 1276 | |
| 1277 | if deprecated: |
| 1278 | self._deprecated.add(name) |
| 1279 | self._deprecated.update(aliases) |
| 1280 | |
| 1281 | return parser |
| 1282 | |
| 1283 | def _get_subactions(self): |
| 1284 | return self._choices_actions |