Lines Matching full:action

57     - Action -- The base class for parser actions. Typically actions are
59 the action= argument of add_argument(). However, for greater
60 customization of ArgumentParser actions, subclasses of Action may
61 be defined and passed as the action= argument.
83 "Action",
131 action="ignore",
288 def add_argument(self, action): argument
289 if action.help is not SUPPRESS:
293 invocations = [get_invocation(action)]
294 for subaction in self._iter_indented_subactions(action):
303 self._add_item(self._format_action, [action])
306 for action in actions:
307 self.add_argument(action)
341 for action in actions:
342 if action.option_strings:
343 optionals.append(action)
345 positionals.append(action)
426 for action in group._group_actions:
427 group_actions.add(action)
439 for i, action in enumerate(actions):
443 if action.help is SUPPRESS:
451 elif not action.option_strings:
452 part = self._format_args(action, action.dest)
455 if action in group_actions:
459 # add the action string to the list
464 option_string = action.option_strings[0]
468 if action.nargs == 0:
474 default = action.dest.upper()
475 args_string = self._format_args(action, default)
479 if not action.required and action not in group_actions:
482 # add the action string to the list
489 # join all the action items with spaces
511 def _format_action(self, action): argument
516 action_header = self._format_action_invocation(action)
519 if not action.help:
523 # short action name; start on the same line and pad two spaces
529 # long action name; start on the next line
535 # collect the pieces of the action help
538 # if there was help for the action, add lines of help text
539 if action.help:
540 help_text = self._expand_help(action)
551 for subaction in self._iter_indented_subactions(action):
557 def _format_action_invocation(self, action): argument
558 if not action.option_strings:
559 (metavar,) = self._metavar_formatter(action, action.dest)(1)
567 if action.nargs == 0:
568 parts.extend(action.option_strings)
573 default = action.dest.upper()
574 args_string = self._format_args(action, default)
575 for option_string in action.option_strings:
580 def _metavar_formatter(self, action, default_metavar): argument
581 if action.metavar is not None:
582 result = action.metavar
583 elif action.choices is not None:
584 choice_strs = [str(choice) for choice in action.choices]
597 def _format_args(self, action, default_metavar): argument
598 get_metavar = self._metavar_formatter(action, default_metavar)
599 if action.nargs is None:
601 elif action.nargs == OPTIONAL:
603 elif action.nargs == ZERO_OR_MORE:
605 elif action.nargs == ONE_OR_MORE:
607 elif action.nargs == REMAINDER:
609 elif action.nargs == PARSER:
612 formats = ["%s" for _ in range(action.nargs)]
613 result = " ".join(formats) % get_metavar(action.nargs)
616 def _expand_help(self, action): argument
617 params = dict(vars(action), prog=self._prog)
627 return self._get_help_string(action) % params
629 def _iter_indented_subactions(self, action): argument
631 get_subactions = action._get_subactions
650 def _get_help_string(self, action): argument
651 return action.help
683 def _get_help_string(self, action): argument
684 help = action.help
685 if "%(default)" not in action.help:
686 if action.default is not SUPPRESS:
688 if action.option_strings or action.nargs in defaulting_nargs:
737 # Action classes
741 class Action(_AttributeHolder): class
744 Action objects are used by an ArgumentParser to represent the information
746 command line. The keyword arguments to the Action constructor are also
747 all attributes of Action instances.
752 should be associated with this action.
768 option uses an action that takes no values.
782 - required -- True if the action must always be specified at the
834 class _StoreAction(Action):
873 class _StoreConstAction(Action):
922 class _AppendAction(Action):
940 "the append const action may be more appropriate"
963 class _AppendConstAction(Action):
991 class _CountAction(Action):
1007 class _HelpAction(Action):
1022 class _VersionAction(Action):
1044 class _SubParsersAction(Action):
1045 class _ChoicesPseudoAction(Action):
1073 # create a pseudo-action to hold the choice help
1191 self.register("action", None, _StoreAction)
1192 self.register("action", "store", _StoreAction)
1193 self.register("action", "store_const", _StoreConstAction)
1194 self.register("action", "store_true", _StoreTrueAction)
1195 self.register("action", "store_false", _StoreFalseAction)
1196 self.register("action", "append", _AppendAction)
1197 self.register("action", "append_const", _AppendConstAction)
1198 self.register("action", "count", _CountAction)
1199 self.register("action", "help", _HelpAction)
1200 self.register("action", "version", _VersionAction)
1201 self.register("action", "parsers", _SubParsersAction)
1206 # action storage
1242 for action in self._actions:
1243 if action.dest in kwargs:
1244 action.default = kwargs[action.dest]
1247 for action in self._actions:
1248 if action.dest == dest and action.default is not None:
1249 return action.default
1282 # create the action object, and add it to the parser
1285 raise ValueError('unknown action "%s"' % action_class)
1286 action = action_class(**kwargs)
1288 # raise an error if the action type is not callable
1289 type_func = self._registry_get("type", action.type, action.type)
1293 return self._add_action(action)
1305 def _add_action(self, action): argument
1307 self._check_conflict(action)
1310 self._actions.append(action)
1311 action.container = self
1313 # index the action by any option strings it has
1314 for option_string in action.option_strings:
1315 self._option_string_actions[option_string] = action
1318 for option_string in action.option_strings:
1323 # return the created action
1324 return action
1326 def _remove_action(self, action): argument
1327 self._actions.remove(action)
1338 # map each action to its group
1352 for action in group._group_actions:
1353 group_map[action] = title_group_map[group.title]
1362 for action in group._group_actions:
1363 group_map[action] = mutex_group
1366 for action in container._actions:
1367 group_map.get(action, self)._add_action(action)
1420 action = kwargs.pop("action", default)
1421 return self._registry_get("action", action, action)
1432 def _check_conflict(self, action): argument
1436 for option_string in action.option_strings:
1444 conflict_handler(action, confl_optionals)
1446 def _handle_conflict_error(self, action, conflicting_actions): argument
1449 [option_string for option_string, action in conflicting_actions]
1451 raise ArgumentError(action, message % conflict_string)
1453 def _handle_conflict_resolve(self, action, conflicting_actions): argument
1456 for option_string, action in conflicting_actions:
1459 action.option_strings.remove(option_string)
1464 if not action.option_strings:
1465 action.container._remove_action(action)
1489 def _add_action(self, action): argument
1490 action = super(_ArgumentGroup, self)._add_action(action)
1491 self._group_actions.append(action)
1492 return action
1494 def _remove_action(self, action): argument
1495 super(_ArgumentGroup, self)._remove_action(action)
1496 self._group_actions.remove(action)
1505 def _add_action(self, action): argument
1506 if action.required:
1509 action = self._container._add_action(action)
1510 self._group_actions.append(action)
1511 return action
1513 def _remove_action(self, action): argument
1514 self._container._remove_action(action)
1515 self._group_actions.remove(action)
1558 """"add_argument(..., action='version', version="N", ...)" """
1600 action="help",
1608 action="version",
1665 # create the parsers action and add it to the positionals list
1667 action = parsers_class(option_strings=[], **kwargs)
1668 self._subparsers._add_action(action)
1670 # return the created parsers action
1671 return action
1673 def _add_action(self, action): argument
1674 if action.option_strings:
1675 self._optionals._add_action(action)
1677 self._positionals._add_action(action)
1678 return action
1681 return [action for action in self._actions if action.option_strings]
1684 return [action for action in self._actions if not action.option_strings]
1705 # add any action defaults that aren't present
1706 for action in self._actions:
1707 if action.dest is not SUPPRESS:
1708 if not hasattr(namespace, action.dest):
1709 if action.default is not SUPPRESS:
1710 default = action.default
1711 if isinstance(action.default, _basestring):
1712 default = self._get_value(action, default)
1713 setattr(namespace, action.dest, default)
1770 # converts arg strings to the appropriate and then takes the action
1774 def take_action(action, argument_strings, option_string=None): argument
1775 seen_actions.add(action)
1776 argument_values = self._get_values(action, argument_strings)
1781 if argument_values is not action.default:
1782 seen_non_default_actions.add(action)
1783 for conflict_action in action_conflicts.get(action, []):
1787 raise ArgumentError(action, msg % action_name)
1789 # take the action if we didn't receive a SUPPRESS value
1792 action(self, namespace, argument_values, option_string)
1794 # function to convert arg_strings into an optional action
1799 action, option_string, explicit_arg = option_tuple
1807 # if we found no optional action, skip it
1808 if action is None:
1815 arg_count = match_argument(action, "A")
1817 # if the action is a single-dash option and takes no
1822 action_tuples.append((action, [], option_string))
1828 action = optionals_map[option_string]
1832 raise ArgumentError(action, msg % explicit_arg)
1834 # if the action expect exactly one argument, we've
1839 action_tuples.append((action, args, option_string))
1846 raise ArgumentError(action, msg % explicit_arg)
1854 arg_count = match_argument(action, selected_patterns)
1857 action_tuples.append((action, args, option_string))
1863 for action, args, option_string in action_tuples:
1864 take_action(action, args, option_string)
1880 for action, arg_count in zip(positionals, arg_counts):
1883 take_action(action, args)
1937 for action in self._actions:
1938 if action.required:
1939 if action not in seen_actions:
1940 name = _get_action_name(action)
1946 for action in group._group_actions:
1947 if action in seen_non_default_actions:
1953 _get_action_name(action)
1954 for action in group._group_actions
1955 if action.help is not SUPPRESS
1995 def _match_argument(self, action, arg_strings_pattern): argument
1996 # match the pattern for this action to the arg strings
1997 nargs_pattern = self._get_nargs_pattern(action)
2007 default = _("expected %s argument(s)") % action.nargs
2008 msg = nargs_errors.get(action.nargs, default)
2009 raise ArgumentError(action, msg)
2021 [self._get_nargs_pattern(action) for action in actions_slice]
2040 # if the option string is present in the parser, return the action
2042 action = self._option_string_actions[arg_string]
2043 return action, arg_string, None
2049 # if the option string before the "=" is present, return the action
2053 action = self._option_string_actions[option_string]
2054 return action, option_string, explicit_arg
2063 [option_string for action, option_string, explicit_arg in option_tuples]
2068 # if exactly one action matched, this segmentation is good,
2069 # so return the parsed action
2103 action = self._option_string_actions[option_string]
2104 tup = action, option_string, explicit_arg
2118 action = self._option_string_actions[option_string]
2119 tup = action, option_string, short_explicit_arg
2122 action = self._option_string_actions[option_string]
2123 tup = action, option_string, explicit_arg
2133 def _get_nargs_pattern(self, action): argument
2136 nargs = action.nargs
2166 # if this is an optional action, -- is not allowed
2167 if action.option_strings:
2177 def _get_values(self, action, arg_strings): argument
2179 if action.nargs not in [PARSER, REMAINDER]:
2183 if not arg_strings and action.nargs == OPTIONAL:
2184 if action.option_strings:
2185 value = action.const
2187 value = action.default
2189 value = self._get_value(action, value)
2190 self._check_value(action, value)
2196 and action.nargs == ZERO_OR_MORE
2197 and not action.option_strings
2199 if action.default is not None:
2200 value = action.default
2203 self._check_value(action, value)
2206 elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
2208 value = self._get_value(action, arg_string)
2209 self._check_value(action, value)
2212 elif action.nargs == REMAINDER:
2213 value = [self._get_value(action, v) for v in arg_strings]
2216 elif action.nargs == PARSER:
2217 value = [self._get_value(action, v) for v in arg_strings]
2218 self._check_value(action, value[0])
2222 value = [self._get_value(action, v) for v in arg_strings]
2224 self._check_value(action, v)
2229 def _get_value(self, action, arg_string): argument
2230 type_func = self._registry_get("type", action.type, action.type)
2233 raise ArgumentError(action, msg % type_func)
2241 name = getattr(action.type, "__name__", repr(action.type))
2243 raise ArgumentError(action, msg)
2247 name = getattr(action.type, "__name__", repr(action.type))
2249 raise ArgumentError(action, msg % (name, arg_string))
2254 def _check_value(self, action, value): argument
2256 if action.choices is not None and value not in action.choices:
2257 tup = value, ", ".join(map(repr, action.choices))
2259 raise ArgumentError(action, msg)