Lines Matching full:action
44 - Action -- The base class for parser actions. Typically actions are
46 the action= argument of add_argument(). However, for greater
47 customization of ArgumentParser actions, subclasses of Action may
48 be defined and passed as the action= argument.
78 'Action',
260 def add_argument(self, action): argument
261 if action.help is not SUPPRESS:
265 invocations = [get_invocation(action)]
266 for subaction in self._iter_indented_subactions(action):
276 self._add_item(self._format_action, [action])
279 for action in actions:
280 self.add_argument(action)
316 for action in actions:
317 if action.option_strings:
318 optionals.append(action)
320 positionals.append(action)
411 for action in group._group_actions:
412 group_actions.add(action)
413 if action.help is SUPPRESS:
441 for i, action in enumerate(actions):
445 if action.help is SUPPRESS:
453 elif not action.option_strings:
454 default = self._get_default_metavar_for_positional(action)
455 part = self._format_args(action, default)
458 if action in group_actions:
462 # add the action string to the list
467 option_string = action.option_strings[0]
471 if action.nargs == 0:
472 part = action.format_usage()
477 default = self._get_default_metavar_for_optional(action)
478 args_string = self._format_args(action, default)
482 if not action.required and action not in group_actions:
485 # add the action string to the list
492 # join all the action items with spaces
513 def _format_action(self, action): argument
519 action_header = self._format_action_invocation(action)
522 if not action.help:
526 # short action name; start on the same line and pad two spaces
532 # long action name; start on the next line
538 # collect the pieces of the action help
541 # if there was help for the action, add lines of help text
542 if action.help and action.help.strip():
543 help_text = self._expand_help(action)
555 for subaction in self._iter_indented_subactions(action):
561 def _format_action_invocation(self, action): argument
562 if not action.option_strings:
563 default = self._get_default_metavar_for_positional(action)
564 metavar, = self._metavar_formatter(action, default)(1)
572 if action.nargs == 0:
573 parts.extend(action.option_strings)
578 default = self._get_default_metavar_for_optional(action)
579 args_string = self._format_args(action, default)
580 for option_string in action.option_strings:
585 def _metavar_formatter(self, action, default_metavar): argument
586 if action.metavar is not None:
587 result = action.metavar
588 elif action.choices is not None:
589 choice_strs = [str(choice) for choice in action.choices]
601 def _format_args(self, action, default_metavar): argument
602 get_metavar = self._metavar_formatter(action, default_metavar)
603 if action.nargs is None:
605 elif action.nargs == OPTIONAL:
607 elif action.nargs == ZERO_OR_MORE:
613 elif action.nargs == ONE_OR_MORE:
615 elif action.nargs == REMAINDER:
617 elif action.nargs == PARSER:
619 elif action.nargs == SUPPRESS:
623 formats = ['%s' for _ in range(action.nargs)]
626 result = ' '.join(formats) % get_metavar(action.nargs)
629 def _expand_help(self, action): argument
630 params = dict(vars(action), prog=self._prog)
640 return self._get_help_string(action) % params
642 def _iter_indented_subactions(self, action): argument
644 get_subactions = action._get_subactions
666 def _get_help_string(self, action): argument
667 return action.help
669 def _get_default_metavar_for_optional(self, action): argument
670 return action.dest.upper()
672 def _get_default_metavar_for_positional(self, action): argument
673 return action.dest
705 def _get_help_string(self, action): argument
714 help = action.help
719 if action.default is not SUPPRESS:
721 if action.option_strings or action.nargs in defaulting_nargs:
735 def _get_default_metavar_for_optional(self, action): argument
736 return action.type.__name__
738 def _get_default_metavar_for_positional(self, action): argument
739 return action.type.__name__
787 # Action classes
790 class Action(_AttributeHolder): class
793 Action objects are used by an ArgumentParser to represent the information
795 command line. The keyword arguments to the Action constructor are also
796 all attributes of Action instances.
801 should be associated with this action.
817 option uses an action that takes no values.
831 - required -- True if the action must always be specified at the
885 class BooleanOptionalAction(Action):
924 class _StoreAction(Action):
959 class _StoreConstAction(Action):
1016 class _AppendAction(Action):
1032 'the append const action may be more appropriate')
1054 class _AppendConstAction(Action):
1081 class _CountAction(Action):
1104 class _HelpAction(Action):
1123 class _VersionAction(Action):
1149 class _SubParsersAction(Action):
1151 class _ChoicesPseudoAction(Action):
1198 # create a pseudo-action to hold the choice help
1353 self.register('action', None, _StoreAction)
1354 self.register('action', 'store', _StoreAction)
1355 self.register('action', 'store_const', _StoreConstAction)
1356 self.register('action', 'store_true', _StoreTrueAction)
1357 self.register('action', 'store_false', _StoreFalseAction)
1358 self.register('action', 'append', _AppendAction)
1359 self.register('action', 'append_const', _AppendConstAction)
1360 self.register('action', 'count', _CountAction)
1361 self.register('action', 'help', _HelpAction)
1362 self.register('action', 'version', _VersionAction)
1363 self.register('action', 'parsers', _SubParsersAction)
1364 self.register('action', 'extend', _ExtendAction)
1369 # action storage
1405 for action in self._actions:
1406 if action.dest in kwargs:
1407 action.default = kwargs[action.dest]
1410 for action in self._actions:
1411 if action.dest == dest and action.default is not None:
1412 return action.default
1446 # create the action object, and add it to the parser
1449 raise ValueError('unknown action "%s"' % (action_class,))
1450 action = action_class(**kwargs)
1452 # raise an error if the action type is not callable
1453 type_func = self._registry_get('type', action.type, action.type)
1464 self._get_formatter()._format_args(action, None)
1468 return self._add_action(action)
1480 def _add_action(self, action): argument
1482 self._check_conflict(action)
1485 self._actions.append(action)
1486 action.container = self
1488 # index the action by any option strings it has
1489 for option_string in action.option_strings:
1490 self._option_string_actions[option_string] = action
1493 for option_string in action.option_strings:
1498 # return the created action
1499 return action
1501 def _remove_action(self, action): argument
1502 self._actions.remove(action)
1513 # map each action to its group
1526 for action in group._group_actions:
1527 group_map[action] = title_group_map[group.title]
1537 for action in group._group_actions:
1538 group_map[action] = mutex_group
1541 for action in container._actions:
1542 group_map.get(action, self)._add_action(action)
1595 action = kwargs.pop('action', default)
1596 return self._registry_get('action', action, action)
1607 def _check_conflict(self, action): argument
1611 for option_string in action.option_strings:
1619 conflict_handler(action, confl_optionals)
1621 def _handle_conflict_error(self, action, conflicting_actions): argument
1626 for option_string, action
1628 raise ArgumentError(action, message % conflict_string)
1630 def _handle_conflict_resolve(self, action, conflicting_actions): argument
1633 for option_string, action in conflicting_actions:
1636 action.option_strings.remove(option_string)
1641 if not action.option_strings:
1642 action.container._remove_action(action)
1669 def _add_action(self, action): argument
1670 action = super(_ArgumentGroup, self)._add_action(action)
1671 self._group_actions.append(action)
1672 return action
1674 def _remove_action(self, action): argument
1675 super(_ArgumentGroup, self)._remove_action(action)
1676 self._group_actions.remove(action)
1694 def _add_action(self, action): argument
1695 if action.required:
1698 action = self._container._add_action(action)
1699 self._group_actions.append(action)
1700 return action
1702 def _remove_action(self, action): argument
1703 self._container._remove_action(action)
1704 self._group_actions.remove(action)
1787 action='help', default=SUPPRESS,
1840 # create the parsers action and add it to the positionals list
1842 action = parsers_class(option_strings=[], **kwargs)
1843 self._subparsers._add_action(action)
1845 # return the created parsers action
1846 return action
1848 def _add_action(self, action): argument
1849 if action.option_strings:
1850 self._optionals._add_action(action)
1852 self._positionals._add_action(action)
1853 return action
1856 return [action
1857 for action in self._actions
1858 if action.option_strings]
1861 return [action
1862 for action in self._actions
1863 if not action.option_strings]
1887 # add any action defaults that aren't present
1888 for action in self._actions:
1889 if action.dest is not SUPPRESS:
1890 if not hasattr(namespace, action.dest):
1891 if action.default is not SUPPRESS:
1892 setattr(namespace, action.dest, action.default)
1956 # converts arg strings to the appropriate and then takes the action
1960 def take_action(action, argument_strings, option_string=None): argument
1961 seen_actions.add(action)
1962 argument_values = self._get_values(action, argument_strings)
1967 if argument_values is not action.default:
1968 seen_non_default_actions.add(action)
1969 for conflict_action in action_conflicts.get(action, []):
1973 raise ArgumentError(action, msg % action_name)
1975 # take the action if we didn't receive a SUPPRESS value
1978 action(self, namespace, argument_values, option_string)
1980 # function to convert arg_strings into an optional action
1985 action, option_string, explicit_arg = option_tuple
1993 # if we found no optional action, skip it
1994 if action is None:
2001 arg_count = match_argument(action, 'A')
2003 # if the action is a single-dash option and takes no
2012 action_tuples.append((action, [], option_string))
2018 action = optionals_map[option_string]
2022 raise ArgumentError(action, msg % explicit_arg)
2024 # if the action expect exactly one argument, we've
2029 action_tuples.append((action, args, option_string))
2036 raise ArgumentError(action, msg % explicit_arg)
2044 arg_count = match_argument(action, selected_patterns)
2047 action_tuples.append((action, args, option_string))
2053 for action, args, option_string in action_tuples:
2054 take_action(action, args, option_string)
2070 for action, arg_count in zip(positionals, arg_counts):
2073 take_action(action, args)
2123 # action defaults which were not given as arguments
2125 for action in self._actions:
2126 if action not in seen_actions:
2127 if action.required:
2128 required_actions.append(_get_action_name(action))
2130 # Convert action default now instead of doing it before
2134 if (action.default is not None and
2135 isinstance(action.default, str) and
2136 hasattr(namespace, action.dest) and
2137 action.default is getattr(namespace, action.dest)):
2138 setattr(namespace, action.dest,
2139 self._get_value(action, action.default))
2148 for action in group._group_actions:
2149 if action in seen_non_default_actions:
2154 names = [_get_action_name(action)
2155 for action in group._group_actions
2156 if action.help is not SUPPRESS]
2191 def _match_argument(self, action, arg_strings_pattern): argument
2192 # match the pattern for this action to the arg strings
2193 nargs_pattern = self._get_nargs_pattern(action)
2203 msg = nargs_errors.get(action.nargs)
2207 action.nargs) % action.nargs
2208 raise ArgumentError(action, msg)
2219 pattern = ''.join([self._get_nargs_pattern(action)
2220 for action in actions_slice])
2238 # if the option string is present in the parser, return the action
2240 action = self._option_string_actions[arg_string]
2241 return action, arg_string, None
2247 # if the option string before the "=" is present, return the action
2251 action = self._option_string_actions[option_string]
2252 return action, option_string, explicit_arg
2261 for action, option_string, explicit_arg in option_tuples])
2266 # if exactly one action matched, this segmentation is good,
2267 # so return the parsed action
2302 action = self._option_string_actions[option_string]
2303 tup = action, option_string, explicit_arg
2317 action = self._option_string_actions[option_string]
2318 tup = action, option_string, short_explicit_arg
2321 action = self._option_string_actions[option_string]
2322 tup = action, option_string, explicit_arg
2332 def _get_nargs_pattern(self, action): argument
2335 nargs = action.nargs
2361 # suppress action, like nargs=0
2369 # if this is an optional action, -- is not allowed
2370 if action.option_strings:
2402 a = [action for action in positionals
2403 if action.nargs in [PARSER, REMAINDER]]
2408 if [action.dest for group in self._mutually_exclusive_groups
2409 for action in group._group_actions if action in positionals]:
2419 for action in positionals:
2421 action.save_nargs = action.nargs
2422 # action.nargs = 0
2423 action.nargs = SUPPRESS
2424 action.save_default = action.default
2425 action.default = SUPPRESS
2428 for action in positionals:
2430 if (hasattr(namespace, action.dest)
2431 and getattr(namespace, action.dest)==[]):
2433 warn('Do not expect %s in %s' % (action.dest, namespace))
2434 delattr(namespace, action.dest)
2437 for action in positionals:
2438 action.nargs = action.save_nargs
2439 action.default = action.save_default
2444 for action in optionals:
2445 action.save_required = action.required
2446 action.required = False
2454 for action in optionals:
2455 action.required = action.save_required
2465 def _get_values(self, action, arg_strings): argument
2467 if action.nargs not in [PARSER, REMAINDER]:
2474 if not arg_strings and action.nargs == OPTIONAL:
2475 if action.option_strings:
2476 value = action.const
2478 value = action.default
2480 value = self._get_value(action, value)
2481 self._check_value(action, value)
2485 elif (not arg_strings and action.nargs == ZERO_OR_MORE and
2486 not action.option_strings):
2487 if action.default is not None:
2488 value = action.default
2491 self._check_value(action, value)
2494 elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
2496 value = self._get_value(action, arg_string)
2497 self._check_value(action, value)
2500 elif action.nargs == REMAINDER:
2501 value = [self._get_value(action, v) for v in arg_strings]
2504 elif action.nargs == PARSER:
2505 value = [self._get_value(action, v) for v in arg_strings]
2506 self._check_value(action, value[0])
2509 elif action.nargs == SUPPRESS:
2514 value = [self._get_value(action, v) for v in arg_strings]
2516 self._check_value(action, v)
2521 def _get_value(self, action, arg_string): argument
2522 type_func = self._registry_get('type', action.type, action.type)
2525 raise ArgumentError(action, msg % type_func)
2533 name = getattr(action.type, '__name__', repr(action.type))
2535 raise ArgumentError(action, msg)
2539 name = getattr(action.type, '__name__', repr(action.type))
2542 raise ArgumentError(action, msg % args)
2547 def _check_value(self, action, value): argument
2549 if action.choices is not None and value not in action.choices:
2551 'choices': ', '.join(map(repr, action.choices))}
2553 raise ArgumentError(action, msg % args)