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.
76 'Action',
258 def add_argument(self, action): argument
259 if action.help is not SUPPRESS:
263 invocations = [get_invocation(action)]
264 for subaction in self._iter_indented_subactions(action):
274 self._add_item(self._format_action, [action])
277 for action in actions:
278 self.add_argument(action)
314 for action in actions:
315 if action.option_strings:
316 optionals.append(action)
318 positionals.append(action)
403 for action in group._group_actions:
404 group_actions.add(action)
422 for i, action in enumerate(actions):
426 if action.help is SUPPRESS:
434 elif not action.option_strings:
435 default = self._get_default_metavar_for_positional(action)
436 part = self._format_args(action, default)
439 if action in group_actions:
443 # add the action string to the list
448 option_string = action.option_strings[0]
452 if action.nargs == 0:
458 default = self._get_default_metavar_for_optional(action)
459 args_string = self._format_args(action, default)
463 if not action.required and action not in group_actions:
466 # add the action string to the list
473 # join all the action items with spaces
495 def _format_action(self, action): argument
501 action_header = self._format_action_invocation(action)
504 if not action.help:
508 # short action name; start on the same line and pad two spaces
514 # long action name; start on the next line
520 # collect the pieces of the action help
523 # if there was help for the action, add lines of help text
524 if action.help:
525 help_text = self._expand_help(action)
536 for subaction in self._iter_indented_subactions(action):
542 def _format_action_invocation(self, action): argument
543 if not action.option_strings:
544 default = self._get_default_metavar_for_positional(action)
545 metavar, = self._metavar_formatter(action, default)(1)
553 if action.nargs == 0:
554 parts.extend(action.option_strings)
559 default = self._get_default_metavar_for_optional(action)
560 args_string = self._format_args(action, default)
561 for option_string in action.option_strings:
566 def _metavar_formatter(self, action, default_metavar): argument
567 if action.metavar is not None:
568 result = action.metavar
569 elif action.choices is not None:
570 choice_strs = [str(choice) for choice in action.choices]
582 def _format_args(self, action, default_metavar): argument
583 get_metavar = self._metavar_formatter(action, default_metavar)
584 if action.nargs is None:
586 elif action.nargs == OPTIONAL:
588 elif action.nargs == ZERO_OR_MORE:
590 elif action.nargs == ONE_OR_MORE:
592 elif action.nargs == REMAINDER:
594 elif action.nargs == PARSER:
596 elif action.nargs == SUPPRESS:
599 formats = ['%s' for _ in range(action.nargs)]
600 result = ' '.join(formats) % get_metavar(action.nargs)
603 def _expand_help(self, action): argument
604 params = dict(vars(action), prog=self._prog)
614 return self._get_help_string(action) % params
616 def _iter_indented_subactions(self, action): argument
618 get_subactions = action._get_subactions
640 def _get_help_string(self, action): argument
641 return action.help
643 def _get_default_metavar_for_optional(self, action): argument
644 return action.dest.upper()
646 def _get_default_metavar_for_positional(self, action): argument
647 return action.dest
679 def _get_help_string(self, action): argument
680 help = action.help
681 if '%(default)' not in action.help:
682 if action.default is not SUPPRESS:
684 if action.option_strings or action.nargs in defaulting_nargs:
697 def _get_default_metavar_for_optional(self, action): argument
698 return action.type.__name__
700 def _get_default_metavar_for_positional(self, action): argument
701 return action.type.__name__
748 # Action classes
751 class Action(_AttributeHolder): class
754 Action objects are used by an ArgumentParser to represent the information
756 command line. The keyword arguments to the Action constructor are also
757 all attributes of Action instances.
762 should be associated with this action.
778 option uses an action that takes no values.
792 - required -- True if the action must always be specified at the
842 class _StoreAction(Action):
877 class _StoreConstAction(Action):
934 class _AppendAction(Action):
950 'the append const action may be more appropriate')
972 class _AppendConstAction(Action):
999 class _CountAction(Action):
1022 class _HelpAction(Action):
1041 class _VersionAction(Action):
1067 class _SubParsersAction(Action):
1069 class _ChoicesPseudoAction(Action):
1109 # create a pseudo-action to hold the choice help
1257 self.register('action', None, _StoreAction)
1258 self.register('action', 'store', _StoreAction)
1259 self.register('action', 'store_const', _StoreConstAction)
1260 self.register('action', 'store_true', _StoreTrueAction)
1261 self.register('action', 'store_false', _StoreFalseAction)
1262 self.register('action', 'append', _AppendAction)
1263 self.register('action', 'append_const', _AppendConstAction)
1264 self.register('action', 'count', _CountAction)
1265 self.register('action', 'help', _HelpAction)
1266 self.register('action', 'version', _VersionAction)
1267 self.register('action', 'parsers', _SubParsersAction)
1272 # action storage
1308 for action in self._actions:
1309 if action.dest in kwargs:
1310 action.default = kwargs[action.dest]
1313 for action in self._actions:
1314 if action.dest == dest and action.default is not None:
1315 return action.default
1349 # create the action object, and add it to the parser
1352 raise ValueError('unknown action "%s"' % (action_class,))
1353 action = action_class(**kwargs)
1355 # raise an error if the action type is not callable
1356 type_func = self._registry_get('type', action.type, action.type)
1363 self._get_formatter()._format_args(action, None)
1367 return self._add_action(action)
1379 def _add_action(self, action): argument
1381 self._check_conflict(action)
1384 self._actions.append(action)
1385 action.container = self
1387 # index the action by any option strings it has
1388 for option_string in action.option_strings:
1389 self._option_string_actions[option_string] = action
1392 for option_string in action.option_strings:
1397 # return the created action
1398 return action
1400 def _remove_action(self, action): argument
1401 self._actions.remove(action)
1412 # map each action to its group
1425 for action in group._group_actions:
1426 group_map[action] = title_group_map[group.title]
1436 for action in group._group_actions:
1437 group_map[action] = mutex_group
1440 for action in container._actions:
1441 group_map.get(action, self)._add_action(action)
1496 action = kwargs.pop('action', default)
1497 return self._registry_get('action', action, action)
1508 def _check_conflict(self, action): argument
1512 for option_string in action.option_strings:
1520 conflict_handler(action, confl_optionals)
1522 def _handle_conflict_error(self, action, conflicting_actions): argument
1527 for option_string, action
1529 raise ArgumentError(action, message % conflict_string)
1531 def _handle_conflict_resolve(self, action, conflicting_actions): argument
1534 for option_string, action in conflicting_actions:
1537 action.option_strings.remove(option_string)
1542 if not action.option_strings:
1543 action.container._remove_action(action)
1570 def _add_action(self, action): argument
1571 action = super(_ArgumentGroup, self)._add_action(action)
1572 self._group_actions.append(action)
1573 return action
1575 def _remove_action(self, action): argument
1576 super(_ArgumentGroup, self)._remove_action(action)
1577 self._group_actions.remove(action)
1587 def _add_action(self, action): argument
1588 if action.required:
1591 action = self._container._add_action(action)
1592 self._group_actions.append(action)
1593 return action
1595 def _remove_action(self, action): argument
1596 self._container._remove_action(action)
1597 self._group_actions.remove(action)
1667 action='help', default=SUPPRESS,
1720 # create the parsers action and add it to the positionals list
1722 action = parsers_class(option_strings=[], **kwargs)
1723 self._subparsers._add_action(action)
1725 # return the created parsers action
1726 return action
1728 def _add_action(self, action): argument
1729 if action.option_strings:
1730 self._optionals._add_action(action)
1732 self._positionals._add_action(action)
1733 return action
1736 return [action
1737 for action in self._actions
1738 if action.option_strings]
1741 return [action
1742 for action in self._actions
1743 if not action.option_strings]
1767 # add any action defaults that aren't present
1768 for action in self._actions:
1769 if action.dest is not SUPPRESS:
1770 if not hasattr(namespace, action.dest):
1771 if action.default is not SUPPRESS:
1772 setattr(namespace, action.dest, action.default)
1833 # converts arg strings to the appropriate and then takes the action
1837 def take_action(action, argument_strings, option_string=None): argument
1838 seen_actions.add(action)
1839 argument_values = self._get_values(action, argument_strings)
1844 if argument_values is not action.default:
1845 seen_non_default_actions.add(action)
1846 for conflict_action in action_conflicts.get(action, []):
1850 raise ArgumentError(action, msg % action_name)
1852 # take the action if we didn't receive a SUPPRESS value
1855 action(self, namespace, argument_values, option_string)
1857 # function to convert arg_strings into an optional action
1862 action, option_string, explicit_arg = option_tuple
1870 # if we found no optional action, skip it
1871 if action is None:
1878 arg_count = match_argument(action, 'A')
1880 # if the action is a single-dash option and takes no
1885 action_tuples.append((action, [], option_string))
1891 action = optionals_map[option_string]
1895 raise ArgumentError(action, msg % explicit_arg)
1897 # if the action expect exactly one argument, we've
1902 action_tuples.append((action, args, option_string))
1909 raise ArgumentError(action, msg % explicit_arg)
1917 arg_count = match_argument(action, selected_patterns)
1920 action_tuples.append((action, args, option_string))
1926 for action, args, option_string in action_tuples:
1927 take_action(action, args, option_string)
1943 for action, arg_count in zip(positionals, arg_counts):
1946 take_action(action, args)
1996 # action defaults which were not given as arguments
1998 for action in self._actions:
1999 if action not in seen_actions:
2000 if action.required:
2001 required_actions.append(_get_action_name(action))
2003 # Convert action default now instead of doing it before
2007 if (action.default is not None and
2008 isinstance(action.default, str) and
2009 hasattr(namespace, action.dest) and
2010 action.default is getattr(namespace, action.dest)):
2011 setattr(namespace, action.dest,
2012 self._get_value(action, action.default))
2021 for action in group._group_actions:
2022 if action in seen_non_default_actions:
2027 names = [_get_action_name(action)
2028 for action in group._group_actions
2029 if action.help is not SUPPRESS]
2065 def _match_argument(self, action, arg_strings_pattern): argument
2066 # match the pattern for this action to the arg strings
2067 nargs_pattern = self._get_nargs_pattern(action)
2079 action.nargs) % action.nargs
2080 msg = nargs_errors.get(action.nargs, default)
2081 raise ArgumentError(action, msg)
2092 pattern = ''.join([self._get_nargs_pattern(action)
2093 for action in actions_slice])
2111 # if the option string is present in the parser, return the action
2113 action = self._option_string_actions[arg_string]
2114 return action, arg_string, None
2120 # if the option string before the "=" is present, return the action
2124 action = self._option_string_actions[option_string]
2125 return action, option_string, explicit_arg
2135 for action, option_string, explicit_arg in option_tuples])
2140 # if exactly one action matched, this segmentation is good,
2141 # so return the parsed action
2175 action = self._option_string_actions[option_string]
2176 tup = action, option_string, explicit_arg
2190 action = self._option_string_actions[option_string]
2191 tup = action, option_string, short_explicit_arg
2194 action = self._option_string_actions[option_string]
2195 tup = action, option_string, explicit_arg
2205 def _get_nargs_pattern(self, action): argument
2208 nargs = action.nargs
2234 # suppress action, like nargs=0
2242 # if this is an optional action, -- is not allowed
2243 if action.option_strings:
2275 a = [action for action in positionals
2276 if action.nargs in [PARSER, REMAINDER]]
2281 if [action.dest for group in self._mutually_exclusive_groups
2282 for action in group._group_actions if action in positionals]:
2292 for action in positionals:
2294 action.save_nargs = action.nargs
2295 # action.nargs = 0
2296 action.nargs = SUPPRESS
2297 action.save_default = action.default
2298 action.default = SUPPRESS
2301 for action in positionals:
2303 if (hasattr(namespace, action.dest)
2304 and getattr(namespace, action.dest)==[]):
2306 warn('Do not expect %s in %s' % (action.dest, namespace))
2307 delattr(namespace, action.dest)
2310 for action in positionals:
2311 action.nargs = action.save_nargs
2312 action.default = action.save_default
2317 for action in optionals:
2318 action.save_required = action.required
2319 action.required = False
2327 for action in optionals:
2328 action.required = action.save_required
2338 def _get_values(self, action, arg_strings): argument
2340 if action.nargs not in [PARSER, REMAINDER]:
2347 if not arg_strings and action.nargs == OPTIONAL:
2348 if action.option_strings:
2349 value = action.const
2351 value = action.default
2353 value = self._get_value(action, value)
2354 self._check_value(action, value)
2358 elif (not arg_strings and action.nargs == ZERO_OR_MORE and
2359 not action.option_strings):
2360 if action.default is not None:
2361 value = action.default
2364 self._check_value(action, value)
2367 elif len(arg_strings) == 1 and action.nargs in [None, OPTIONAL]:
2369 value = self._get_value(action, arg_string)
2370 self._check_value(action, value)
2373 elif action.nargs == REMAINDER:
2374 value = [self._get_value(action, v) for v in arg_strings]
2377 elif action.nargs == PARSER:
2378 value = [self._get_value(action, v) for v in arg_strings]
2379 self._check_value(action, value[0])
2382 elif action.nargs == SUPPRESS:
2387 value = [self._get_value(action, v) for v in arg_strings]
2389 self._check_value(action, v)
2394 def _get_value(self, action, arg_string): argument
2395 type_func = self._registry_get('type', action.type, action.type)
2398 raise ArgumentError(action, msg % type_func)
2406 name = getattr(action.type, '__name__', repr(action.type))
2408 raise ArgumentError(action, msg)
2412 name = getattr(action.type, '__name__', repr(action.type))
2415 raise ArgumentError(action, msg % args)
2420 def _check_value(self, action, value): argument
2422 if action.choices is not None and value not in action.choices:
2424 'choices': ', '.join(map(repr, action.choices))}
2426 raise ArgumentError(action, msg % args)