add_argument(dest, ..., name=value, ...) add_argument(option_string, option_string, ..., name=value, ...)
(self, *args, **kwargs)
| 1270 | # Adding argument actions |
| 1271 | # ======================= |
| 1272 | def add_argument(self, *args, **kwargs): |
| 1273 | """ |
| 1274 | add_argument(dest, ..., name=value, ...) |
| 1275 | add_argument(option_string, option_string, ..., name=value, ...) |
| 1276 | """ |
| 1277 | |
| 1278 | # if no positional args are supplied or only one is supplied and |
| 1279 | # it doesn't look like an option string, parse a positional |
| 1280 | # argument |
| 1281 | chars = self.prefix_chars |
| 1282 | if not args or len(args) == 1 and args[0][0] not in chars: |
| 1283 | if args and 'dest' in kwargs: |
| 1284 | raise ValueError('dest supplied twice for positional argument') |
| 1285 | kwargs = self._get_positional_kwargs(*args, **kwargs) |
| 1286 | |
| 1287 | # otherwise, we're adding an optional argument |
| 1288 | else: |
| 1289 | kwargs = self._get_optional_kwargs(*args, **kwargs) |
| 1290 | |
| 1291 | # if no default was supplied, use the parser-level default |
| 1292 | if 'default' not in kwargs: |
| 1293 | dest = kwargs['dest'] |
| 1294 | if dest in self._defaults: |
| 1295 | kwargs['default'] = self._defaults[dest] |
| 1296 | elif self.argument_default is not None: |
| 1297 | kwargs['default'] = self.argument_default |
| 1298 | |
| 1299 | # create the action object, and add it to the parser |
| 1300 | action_class = self._pop_action_class(kwargs) |
| 1301 | if not _callable(action_class): |
| 1302 | raise ValueError('unknown action "%s"' % (action_class,)) |
| 1303 | action = action_class(**kwargs) |
| 1304 | |
| 1305 | # raise an error if the action type is not callable |
| 1306 | type_func = self._registry_get('type', action.type, action.type) |
| 1307 | if not _callable(type_func): |
| 1308 | raise ValueError('%r is not callable' % (type_func,)) |
| 1309 | |
| 1310 | # raise an error if the metavar does not match the type |
| 1311 | if hasattr(self, "_get_formatter"): |
| 1312 | try: |
| 1313 | self._get_formatter()._format_args(action, None) |
| 1314 | except TypeError: |
| 1315 | raise ValueError("length of metavar tuple does not match nargs") |
| 1316 | |
| 1317 | return self._add_action(action) |
| 1318 | |
| 1319 | def add_argument_group(self, *args, **kwargs): |
| 1320 | group = _ArgumentGroup(self, *args, **kwargs) |
nothing calls this directly
no test coverage detected