Home | History | Annotate | Download | only in python2.7

Lines Matching refs:action

43     - Action -- The base class for parser actions. Typically actions are
45 the action= argument of add_argument(). However, for greater
46 customization of ArgumentParser actions, subclasses of Action may
47 be defined and passed as the action= argument.
75 'Action',
253 def add_argument(self, action):
254 if action.help is not SUPPRESS:
258 invocations = [get_invocation(action)]
259 for subaction in self._iter_indented_subactions(action):
269 self._add_item(self._format_action, [action])
272 for action in actions:
273 self.add_argument(action)
309 for action in actions:
310 if action.option_strings:
311 optionals.append(action)
313 positionals.append(action)
394 for action in group._group_actions:
395 group_actions.add(action)
413 for i, action in enumerate(actions):
417 if action.help is SUPPRESS:
425 elif not action.option_strings:
426 part = self._format_args(action, action.dest)
429 if action in group_actions:
433 # add the action string to the list
438 option_string = action.option_strings[0]
442 if action.nargs == 0:
448 default = action.dest.upper()
449 args_string = self._format_args(action, default)
453 if not action.required and action
456 # add the action string to the list
463 # join all the action items with spaces
485 def _format_action(self, action):
491 action_header = self._format_action_invocation(action)
494 if not action.help:
498 # short action name; start on the same line and pad two spaces
504 # long action name; start on the next line
510 # collect the pieces of the action help
513 # if there was help for the action, add lines of help text
514 if action.help:
515 help_text = self._expand_help(action)
526 for subaction in self._iter_indented_subactions(action):
532 def _format_action_invocation(self, action):
533 if not action.option_strings:
534 metavar, = self._metavar_formatter(action, action.dest)(1)
542 if action.nargs == 0:
543 parts.extend(action.option_strings)
548 default = action.dest.upper()
549 args_string = self._format_args(action, default)
550 for option_string in action.option_strings:
555 def _metavar_formatter(self, action, default_metavar):
556 if action.metavar is not None:
557 result = action.metavar
558 elif action.choices is not None:
559 choice_strs = [str(choice) for choice in action.choices]
571 def _format_args(self, action, default_metavar):
572 get_metavar = self._metavar_formatter(action, default_metavar)
573 if action.nargs is None:
575 elif action.nargs == OPTIONAL:
577 elif action.nargs == ZERO_OR_MORE:
579 elif action.nargs == ONE_OR_MORE:
581 elif action.nargs == REMAINDER:
583 elif action.nargs == PARSER:
586 formats = ['%s' for _ in range(action.nargs)]
587 result = ' '.join(formats) % get_metavar(action.nargs)
590 def _expand_help(self, action):
591 params = dict(vars(action), prog=self._prog)
601 return self._get_help_string(action) % params
603 def _iter_indented_subactions(self, action):
605 get_subactions = action._get_subactions
623 def _get_help_string(self, action):
624 return action.help
656 def _get_help_string(self, action):
657 help = action.help
658 if '%(default)' not in action.help:
659 if action.default is not SUPPRESS:
661 if action.option_strings or action.nargs in defaulting_nargs:
709 # Action classes
712 class Action(_AttributeHolder):
715 Action objects are used by an ArgumentParser to represent the information
717 command line. The keyword arguments to the Action constructor are also
718 all attributes of Action instances.
723 should be associated with this action.
739 option uses an action that takes no values.
753 - required -- True if the action must always be specified at the
803 class _StoreAction(Action):
838 class _StoreConstAction(Action):
895 class _AppendAction(Action):
911 'the append const action may be more appropriate')
932 class _AppendConstAction(Action):
958 class _CountAction(Action):
979 class _HelpAction(Action):
998 class _VersionAction(Action):
1023 class _SubParsersAction(Action):
1025 class _ChoicesPseudoAction(Action):
1057 # create a pseudo-action to hold the choice help
1185 self.register('action', None, _StoreAction)
1186 self.register('action', 'store', _StoreAction)
1187 self.register('action', 'store_const', _StoreConstAction)
1188 self.register('action', 'store_true', _StoreTrueAction)
1189 self.register('action', 'store_false', _StoreFalseAction)
1190 self.register('action', 'append', _AppendAction)
1191 self.register('action', 'append_const', _AppendConstAction)
1192 self.register('action', 'count', _CountAction)
1193 self.register('action', 'help', _HelpAction)
1194 self.register('action', 'version', _VersionAction)
1195 self.register('action', 'parsers', _SubParsersAction)
1200 # action storage
1236 for action in self._actions:
1237 if action.dest in kwargs:
1238 action.default = kwargs[action.dest]
1241 for action in self._actions:
1242 if action.dest == dest and action.default is not None:
1243 return action.default
1277 # create the action object, and add it to the parser
1280 raise ValueError('unknown action "%s"' % (action_class,))
1281 action = action_class(**kwargs)
1283 # raise an error if the action type is not callable
1284 type_func = self._registry_get('type', action.type, action.type)
1291 self._get_formatter()._format_args(action, None)
1295 return self._add_action(action)
1307 def _add_action(self, action):
1309 self._check_conflict(action)
1312 self._actions.append(action)
1313 action.container = self
1315 # index the action by any option strings it has
1316 for option_string in action.option_strings:
1317 self._option_string_actions[option_string] = action
1320 for option_string in action.option_strings:
1325 # return the created action
1326 return action
1328 def _remove_action(self, action):
1329 self._actions.remove(action)
1340 # map each action to its group
1353 for action in group._group_actions:
1354 group_map[action] = title_group_map[group.title]
1364 for action in group._group_actions:
1365 group_map[action] = mutex_group
1368 for action in container._actions:
1369 group_map.get(action, self)._add_action(action)
1423 action = kwargs.pop('action', default)
1424 return self._registry_get('action', action, action)
1435 def _check_conflict(self, action):
1439 for option_string in action.option_strings:
1447 conflict_handler(action, confl_optionals)
1449 def _handle_conflict_error(self, action, conflicting_actions):
1452 for option_string, action
1454 raise ArgumentError(action, message % conflict_string)
1456 def _handle_conflict_resolve(self, action, conflicting_actions):
1459 for option_string, action in conflicting_actions:
1462 action.option_strings.remove(option_string)
1467 if not action.option_strings:
1468 action.container._remove_action(action)
1495 def _add_action(self, action):
1496 action = super(_ArgumentGroup, self)._add_action(action)
1497 self._group_actions.append(action)
1498 return action
1500 def _remove_action(self, action):
1501 super(_ArgumentGroup, self)._remove_action(action)
1502 self._group_actions.remove(action)
1512 def _add_action(self, action):
1513 if action.required:
1516 action = self._container._add_action(action)
1517 self._group_actions.append(action)
1518 return action
1520 def _remove_action(self, action):
1521 self._container._remove_action(action)
1522 self._group_actions.remove(action)
1562 """"add_argument(..., action='version', version="N", ...)" """
1599 action='help', default=SUPPRESS,
1604 action='version', default=SUPPRESS,
1659 # create the parsers action and add it to the positionals list
1661 action = parsers_class(option_strings=[], **kwargs)
1662 self._subparsers._add_action(action)
1664 # return the created parsers action
1665 return action
1667 def _add_action(self, action):
1668 if action.option_strings:
1669 self._optionals._add_action(action)
1671 self._positionals._add_action(action)
1672 return action
1675 return [action
1676 for action in self._actions
1677 if action.option_strings]
1680 return [action
1681 for action in self._actions
1682 if not action.option_strings]
1706 # add any action defaults that aren't present
1707 for action in self._actions:
1708 if action.dest is not SUPPRESS:
1709 if not hasattr(namespace, action.dest):
1710 if action.default is not SUPPRESS:
1711 setattr(namespace, action.dest, action.default)
1772 # converts arg strings to the appropriate and then takes the action
1776 def take_action(action, argument_strings, option_string=None):
1777 seen_actions.add(action)
1778 argument_values = self._get_values(action, argument_strings)
1783 if argument_values is not action.default:
1784 seen_non_default_actions.add(action)
1785 for conflict_action in action_conflicts.get(action, []):
1789 raise ArgumentError(action, msg % action_name)
1791 # take the action if we didn't receive a SUPPRESS value
1794 action(self, namespace, argument_values, option_string)
1796 # function to convert arg_strings into an optional action
1801 action
1809 # if we found no optional action, skip it
1810 if action is None:
1817 arg_count = match_argument(action, 'A')
1819 # if the action is a single-dash option and takes no
1824 action_tuples.append((action, [], option_string))
1830 action = optionals_map[option_string]
1834 raise ArgumentError(action, msg % explicit_arg)
1836 # if the action expect exactly one argument, we've
1841 action_tuples.append((action, args, option_string))
1848 raise ArgumentError(action, msg % explicit_arg)
1856 arg_count = match_argument(action, selected_patterns)
1859 action_tuples.append((action, args, option_string))
1865 for action, args, option_string in action_tuples:
1866 take_action(action, args, option_string)
1882 for action, arg_count in zip(positionals, arg_counts):
1885 take_action(action, args)
1940 for action in self._actions:
1941 if action not in seen_actions:
1942 if action.required:
1943 name = _get_action_name(action)
1946 # Convert action default now instead of doing it before
1950 if (action.default is not None and
1951 isinstance(action.default, basestring) and
1952 hasattr(namespace, action.dest) and
1953 action.default is getattr(namespace, action.dest)):
1954 setattr(namespace, action.dest,
1955 self._get_value(action, action.default))
1960 for action in group._group_actions:
1961 if action in seen_non_default_actions:
1966 names = [_get_action_name(action)
1967 for action in group._group_actions
1968 if action.help is not SUPPRESS]
2007 def _match_argument(self, action, arg_strings_pattern):
2008 # match the pattern for this action to the arg strings
2009 nargs_pattern = self._get_nargs_pattern(action)
2019 default = _('expected %s argument(s)') % action.nargs
2020 msg = nargs_errors.get(action.nargs, default)
2021 raise ArgumentError(action, msg)
2032 pattern = ''.join([self._get_nargs_pattern(action)
2033 for action in actions_slice])
2051 # if the option string is present in the parser, return the action
2053 action = self._option_string_actions[arg_string]
2054 return action, arg_string, None
2060 # if the option string before the "=" is present, return the action
2064 action = self._option_string_actions[option_string]
2065 return action, option_string, explicit_arg
2074 for action, option_string, explicit_arg in option_tuples])
2078 # if exactly one action matched, this segmentation is good,
2079 # so return the parsed action
2113 action = self._option_string_actions[option_string]
2114 tup = action, option_string, explicit_arg
2128 action = self._option_string_actions[option_string]
2129 tup = action, option_string, short_explicit_arg
2132 action = self._option_string_actions[option_string]
2133 tup = action, option_string, explicit_arg
2143 def _get_nargs_pattern(self, action):
2146 nargs = action.nargs
2176 # if this is an optional action, -- is not allowed
2177 action.option_strings:
2187 def _get_values(self, action, arg_strings):
2189 if action.nargs not in [PARSER, REMAINDER]:
2196 if not arg_strings and action.nargs == OPTIONAL:
2197 if action.option_strings:
2198 value = action.const
2200 value = action.default
2202 value = self._get_value(action, value)
2203 self._check_value(action, value)
2207 elif (not arg_strings and action.nargs == ZERO_OR_MORE and
2208 not action.option_strings):
2209 if action.default is not None:
2210 value = action.default
2213 self._check_value(action, value)
2216 elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
2218 value = self._get_value(action, arg_string)
2219 self._check_value(action, value)
2222 elif action.nargs == REMAINDER:
2223 value = [self._get_value(action, v) for v in arg_strings]
2226 elif action.nargs == PARSER:
2227 value = [self._get_value(action, v) for v in arg_strings]
2228 self._check_value(action, value[0])
2232 value = [self._get_value(action, v) for v in arg_strings]
2234 self._check_value(action, v)
2239 def _get_value(self, action, arg_string):
2240 type_func = self._registry_get('type', action.type, action.type)
2243 raise ArgumentError(action, msg % type_func)
2251 name = getattr(action.type, '__name__', repr(action.type))
2253 raise ArgumentError(action, msg)
2257 name = getattr(action.type, '__name__', repr(action.type))
2259 raise ArgumentError(action, msg % (name, arg_string))
2264 def _check_value(self, action, value):
2266 if action.choices is not None and value not in action.choices:
2267 tup = value, ', '.join(map(repr, action.choices))
2269 raise ArgumentError(action, msg)