• Home
  • Raw
  • Download

Lines Matching full: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',
255 def add_argument(self, action): argument
256 if action.help is not SUPPRESS:
260 invocations = [get_invocation(action)]
261 for subaction in self._iter_indented_subactions(action):
271 self._add_item(self._format_action, [action])
274 for action in actions:
275 self.add_argument(action)
311 for action in actions:
312 if action.option_strings:
313 optionals.append(action)
315 positionals.append(action)
400 for action in group._group_actions:
401 group_actions.add(action)
419 for i, action in enumerate(actions):
423 if action.help is SUPPRESS:
431 elif not action.option_strings:
432 part = self._format_args(action, action.dest)
435 if action in group_actions:
439 # add the action string to the list
444 option_string = action.option_strings[0]
448 if action.nargs == 0:
454 default = action.dest.upper()
455 args_string = self._format_args(action, default)
459 if not action.required and action not in group_actions:
462 # add the action string to the list
469 # join all the action items with spaces
491 def _format_action(self, action): argument
497 action_header = self._format_action_invocation(action)
500 if not action.help:
504 # short action name; start on the same line and pad two spaces
510 # long action name; start on the next line
516 # collect the pieces of the action help
519 # if there was help for the action, add lines of help text
520 if action.help:
521 help_text = self._expand_help(action)
532 for subaction in self._iter_indented_subactions(action):
538 def _format_action_invocation(self, action): argument
539 if not action.option_strings:
540 metavar, = self._metavar_formatter(action, action.dest)(1)
548 if action.nargs == 0:
549 parts.extend(action.option_strings)
554 default = action.dest.upper()
555 args_string = self._format_args(action, default)
556 for option_string in action.option_strings:
561 def _metavar_formatter(self, action, default_metavar): argument
562 if action.metavar is not None:
563 result = action.metavar
564 elif action.choices is not None:
565 choice_strs = [str(choice) for choice in action.choices]
577 def _format_args(self, action, default_metavar): argument
578 get_metavar = self._metavar_formatter(action, default_metavar)
579 if action.nargs is None:
581 elif action.nargs == OPTIONAL:
583 elif action.nargs == ZERO_OR_MORE:
585 elif action.nargs == ONE_OR_MORE:
587 elif action.nargs == REMAINDER:
589 elif action.nargs == PARSER:
592 formats = ['%s' for _ in range(action.nargs)]
593 result = ' '.join(formats) % get_metavar(action.nargs)
596 def _expand_help(self, action): argument
597 params = dict(vars(action), prog=self._prog)
607 return self._get_help_string(action) % params
609 def _iter_indented_subactions(self, action): argument
611 get_subactions = action._get_subactions
629 def _get_help_string(self, action): argument
630 return action.help
662 def _get_help_string(self, action): argument
663 help = action.help
664 if '%(default)' not in action.help:
665 if action.default is not SUPPRESS:
667 if action.option_strings or action.nargs in defaulting_nargs:
715 # Action classes
718 class Action(_AttributeHolder): class
721 Action objects are used by an ArgumentParser to represent the information
723 command line. The keyword arguments to the Action constructor are also
724 all attributes of Action instances.
729 should be associated with this action.
745 option uses an action that takes no values.
759 - required -- True if the action must always be specified at the
809 class _StoreAction(Action):
844 class _StoreConstAction(Action):
901 class _AppendAction(Action):
917 'the append const action may be more appropriate')
938 class _AppendConstAction(Action):
964 class _CountAction(Action):
985 class _HelpAction(Action):
1004 class _VersionAction(Action):
1029 class _SubParsersAction(Action):
1031 class _ChoicesPseudoAction(Action):
1063 # create a pseudo-action to hold the choice help
1202 self.register('action', None, _StoreAction)
1203 self.register('action', 'store', _StoreAction)
1204 self.register('action', 'store_const', _StoreConstAction)
1205 self.register('action', 'store_true', _StoreTrueAction)
1206 self.register('action', 'store_false', _StoreFalseAction)
1207 self.register('action', 'append', _AppendAction)
1208 self.register('action', 'append_const', _AppendConstAction)
1209 self.register('action', 'count', _CountAction)
1210 self.register('action', 'help', _HelpAction)
1211 self.register('action', 'version', _VersionAction)
1212 self.register('action', 'parsers', _SubParsersAction)
1217 # action storage
1253 for action in self._actions:
1254 if action.dest in kwargs:
1255 action.default = kwargs[action.dest]
1258 for action in self._actions:
1259 if action.dest == dest and action.default is not None:
1260 return action.default
1294 # create the action object, and add it to the parser
1297 raise ValueError('unknown action "%s"' % (action_class,))
1298 action = action_class(**kwargs)
1300 # raise an error if the action type is not callable
1301 type_func = self._registry_get('type', action.type, action.type)
1308 self._get_formatter()._format_args(action, None)
1312 return self._add_action(action)
1324 def _add_action(self, action): argument
1326 self._check_conflict(action)
1329 self._actions.append(action)
1330 action.container = self
1332 # index the action by any option strings it has
1333 for option_string in action.option_strings:
1334 self._option_string_actions[option_string] = action
1337 for option_string in action.option_strings:
1342 # return the created action
1343 return action
1345 def _remove_action(self, action): argument
1346 self._actions.remove(action)
1357 # map each action to its group
1370 for action in group._group_actions:
1371 group_map[action] = title_group_map[group.title]
1381 for action in group._group_actions:
1382 group_map[action] = mutex_group
1385 for action in container._actions:
1386 group_map.get(action, self)._add_action(action)
1440 action = kwargs.pop('action', default)
1441 return self._registry_get('action', action, action)
1452 def _check_conflict(self, action): argument
1456 for option_string in action.option_strings:
1464 conflict_handler(action, confl_optionals)
1466 def _handle_conflict_error(self, action, conflicting_actions): argument
1469 for option_string, action
1471 raise ArgumentError(action, message % conflict_string)
1473 def _handle_conflict_resolve(self, action, conflicting_actions): argument
1476 for option_string, action in conflicting_actions:
1479 action.option_strings.remove(option_string)
1484 if not action.option_strings:
1485 action.container._remove_action(action)
1512 def _add_action(self, action): argument
1513 action = super(_ArgumentGroup, self)._add_action(action)
1514 self._group_actions.append(action)
1515 return action
1517 def _remove_action(self, action): argument
1518 super(_ArgumentGroup, self)._remove_action(action)
1519 self._group_actions.remove(action)
1529 def _add_action(self, action): argument
1530 if action.required:
1533 action = self._container._add_action(action)
1534 self._group_actions.append(action)
1535 return action
1537 def _remove_action(self, action): argument
1538 self._container._remove_action(action)
1539 self._group_actions.remove(action)
1579 """"add_argument(..., action='version', version="N", ...)" """
1616 action='help', default=SUPPRESS,
1621 action='version', default=SUPPRESS,
1676 # create the parsers action and add it to the positionals list
1678 action = parsers_class(option_strings=[], **kwargs)
1679 self._subparsers._add_action(action)
1681 # return the created parsers action
1682 return action
1684 def _add_action(self, action): argument
1685 if action.option_strings:
1686 self._optionals._add_action(action)
1688 self._positionals._add_action(action)
1689 return action
1692 return [action
1693 for action in self._actions
1694 if action.option_strings]
1697 return [action
1698 for action in self._actions
1699 if not action.option_strings]
1723 # add any action defaults that aren't present
1724 for action in self._actions:
1725 if action.dest is not SUPPRESS:
1726 if not hasattr(namespace, action.dest):
1727 if action.default is not SUPPRESS:
1728 setattr(namespace, action.dest, action.default)
1789 # converts arg strings to the appropriate and then takes the action
1793 def take_action(action, argument_strings, option_string=None): argument
1794 seen_actions.add(action)
1795 argument_values = self._get_values(action, argument_strings)
1800 if argument_values is not action.default:
1801 seen_non_default_actions.add(action)
1802 for conflict_action in action_conflicts.get(action, []):
1806 raise ArgumentError(action, msg % action_name)
1808 # take the action if we didn't receive a SUPPRESS value
1811 action(self, namespace, argument_values, option_string)
1813 # function to convert arg_strings into an optional action
1818 action, option_string, explicit_arg = option_tuple
1826 # if we found no optional action, skip it
1827 if action is None:
1834 arg_count = match_argument(action, 'A')
1836 # if the action is a single-dash option and takes no
1841 action_tuples.append((action, [], option_string))
1847 action = optionals_map[option_string]
1851 raise ArgumentError(action, msg % explicit_arg)
1853 # if the action expect exactly one argument, we've
1858 action_tuples.append((action, args, option_string))
1865 raise ArgumentError(action, msg % explicit_arg)
1873 arg_count = match_argument(action, selected_patterns)
1876 action_tuples.append((action, args, option_string))
1882 for action, args, option_string in action_tuples:
1883 take_action(action, args, option_string)
1899 for action, arg_count in zip(positionals, arg_counts):
1902 take_action(action, args)
1957 for action in self._actions:
1958 if action not in seen_actions:
1959 if action.required:
1960 name = _get_action_name(action)
1963 # Convert action default now instead of doing it before
1967 if (action.default is not None and
1968 isinstance(action.default, basestring) and
1969 hasattr(namespace, action.dest) and
1970 action.default is getattr(namespace, action.dest)):
1971 setattr(namespace, action.dest,
1972 self._get_value(action, action.default))
1977 for action in group._group_actions:
1978 if action in seen_non_default_actions:
1983 names = [_get_action_name(action)
1984 for action in group._group_actions
1985 if action.help is not SUPPRESS]
2024 def _match_argument(self, action, arg_strings_pattern): argument
2025 # match the pattern for this action to the arg strings
2026 nargs_pattern = self._get_nargs_pattern(action)
2036 default = _('expected %s argument(s)') % action.nargs
2037 msg = nargs_errors.get(action.nargs, default)
2038 raise ArgumentError(action, msg)
2049 pattern = ''.join([self._get_nargs_pattern(action)
2050 for action in actions_slice])
2068 # if the option string is present in the parser, return the action
2070 action = self._option_string_actions[arg_string]
2071 return action, arg_string, None
2077 # if the option string before the "=" is present, return the action
2081 action = self._option_string_actions[option_string]
2082 return action, option_string, explicit_arg
2091 for action, option_string, explicit_arg in option_tuples])
2095 # if exactly one action matched, this segmentation is good,
2096 # so return the parsed action
2130 action = self._option_string_actions[option_string]
2131 tup = action, option_string, explicit_arg
2145 action = self._option_string_actions[option_string]
2146 tup = action, option_string, short_explicit_arg
2149 action = self._option_string_actions[option_string]
2150 tup = action, option_string, explicit_arg
2160 def _get_nargs_pattern(self, action): argument
2163 nargs = action.nargs
2193 # if this is an optional action, -- is not allowed
2194 if action.option_strings:
2204 def _get_values(self, action, arg_strings): argument
2206 if action.nargs not in [PARSER, REMAINDER]:
2213 if not arg_strings and action.nargs == OPTIONAL:
2214 if action.option_strings:
2215 value = action.const
2217 value = action.default
2219 value = self._get_value(action, value)
2220 self._check_value(action, value)
2224 elif (not arg_strings and action.nargs == ZERO_OR_MORE and
2225 not action.option_strings):
2226 if action.default is not None:
2227 value = action.default
2230 self._check_value(action, value)
2233 elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
2235 value = self._get_value(action, arg_string)
2236 self._check_value(action, value)
2239 elif action.nargs == REMAINDER:
2240 value = [self._get_value(action, v) for v in arg_strings]
2243 elif action.nargs == PARSER:
2244 value = [self._get_value(action, v) for v in arg_strings]
2245 self._check_value(action, value[0])
2249 value = [self._get_value(action, v) for v in arg_strings]
2251 self._check_value(action, v)
2256 def _get_value(self, action, arg_string): argument
2257 type_func = self._registry_get('type', action.type, action.type)
2260 raise ArgumentError(action, msg % type_func)
2268 name = getattr(action.type, '__name__', repr(action.type))
2270 raise ArgumentError(action, msg)
2274 name = getattr(action.type, '__name__', repr(action.type))
2276 raise ArgumentError(action, msg % (name, arg_string))
2281 def _check_value(self, action, value): argument
2283 if action.choices is not None and value not in action.choices:
2284 tup = value, ', '.join(map(repr, action.choices))
2286 raise ArgumentError(action, msg)