Lines Matching refs:self

118     def __repr__(self):
119 type_name = type(self).__name__
122 for arg in self._get_args():
124 for name, value in self._get_kwargs():
133 def _get_kwargs(self):
134 return list(self.__dict__.items())
136 def _get_args(self):
164 def __init__(self,
176 self._prog = prog
177 self._indent_increment = indent_increment
178 self._max_help_position = min(max_help_position,
180 self._width = width
182 self._current_indent = 0
183 self._level = 0
184 self._action_max_length = 0
186 self._root_section = self._Section(self, None)
187 self._current_section = self._root_section
189 self._whitespace_matcher = _re.compile(r'\s+', _re.ASCII)
190 self._long_break_matcher = _re.compile(r'\n\n\n+')
195 def _indent(self):
196 self._current_indent += self._indent_increment
197 self._level += 1
199 def _dedent(self):
200 self._current_indent -= self._indent_increment
201 assert self._current_indent >= 0, 'Indent decreased below 0.'
202 self._level -= 1
206 def __init__(self, formatter, parent, heading=None):
207 self.formatter = formatter
208 self.parent = parent
209 self.heading = heading
210 self.items = []
212 def format_help(self):
214 if self.parent is not None:
215 self.formatter._indent()
216 join = self.formatter._join_parts
217 item_help = join([func(*args) for func, args in self.items])
218 if self.parent is not None:
219 self.formatter._dedent()
226 if self.heading is not SUPPRESS and self.heading is not None:
227 current_indent = self.formatter._current_indent
228 heading = '%*s%s:\n' % (current_indent, '', self.heading)
235 def _add_item(self, func, args):
236 self._current_section.items.append((func, args))
241 def start_section(self, heading):
242 self._indent()
243 section = self._Section(self, self._current_section, heading)
244 self._add_item(section.format_help, [])
245 self._current_section = section
247 def end_section(self):
248 self._current_section = self._current_section.parent
249 self._dedent()
251 def add_text(self, text):
253 self._add_item(self._format_text, [text])
255 def add_usage(self, usage, actions, groups, prefix=None):
258 self._add_item(self._format_usage, args)
260 def add_argument(self, action):
264 get_invocation = self._format_action_invocation
266 for subaction in self._iter_indented_subactions(action):
271 action_length = invocation_length + self._current_indent
272 self._action_max_length = max(self._action_max_length,
276 self._add_item(self._format_action, [action])
278 def add_arguments(self, actions):
280 self.add_argument(action)
285 def format_help(self):
286 help = self._root_section.format_help()
288 help = self._long_break_matcher.sub('\n\n', help)
292 def _join_parts(self, part_strings):
297 def _format_usage(self, usage, actions, groups, prefix):
303 usage = usage % dict(prog=self._prog)
307 usage = '%(prog)s' % dict(prog=self._prog)
311 prog = '%(prog)s' % dict(prog=self._prog)
323 format = self._format_actions_usage
328 text_width = self._width - self._current_indent
393 def _format_actions_usage(self, actions, groups):
454 default = self._get_default_metavar_for_positional(action)
455 part = self._format_args(action, default)
477 default = self._get_default_metavar_for_optional(action)
478 args_string = self._format_args(action, default)
506 def _format_text(self, text):
508 text = text % dict(prog=self._prog)
509 text_width = max(self._width - self._current_indent, 11)
510 indent = ' ' * self._current_indent
511 return self._fill_text(text, text_width, indent) + '\n\n'
513 def _format_action(self, action):
515 help_position = min(self._action_max_length + 2,
516 self._max_help_position)
517 help_width = max(self._width - help_position, 11)
518 action_width = help_position - self._current_indent - 2
519 action_header = self._format_action_invocation(action)
523 tup = self._current_indent, '', action_header
528 tup = self._current_indent, '', action_width, action_header
534 tup = self._current_indent, '', action_header
543 help_text = self._expand_help(action)
545 help_lines = self._split_lines(help_text, help_width)
555 for subaction in self._iter_indented_subactions(action):
556 parts.append(self._format_action(subaction))
559 return self._join_parts(parts)
561 def _format_action_invocation(self, action):
563 default = self._get_default_metavar_for_positional(action)
564 metavar, = self._metavar_formatter(action, default)(1)
578 default = self._get_default_metavar_for_optional(action)
579 args_string = self._format_args(action, default)
585 def _metavar_formatter(self, action, default_metavar):
601 def _format_args(self, action, default_metavar):
602 get_metavar = self._metavar_formatter(action, default_metavar)
629 def _expand_help(self, action):
630 params = dict(vars(action), prog=self._prog)
640 return self._get_help_string(action) % params
642 def _iter_indented_subactions(self, action):
648 self._indent()
650 self._dedent()
652 def _split_lines(self, text, width):
653 text = self._whitespace_matcher.sub(' ', text).strip()
659 def _fill_text(self, text, width, indent):
660 text = self._whitespace_matcher.sub(' ', text).strip()
666 def _get_help_string(self, action):
669 def _get_default_metavar_for_optional(self, action):
672 def _get_default_metavar_for_positional(self, action):
683 def _fill_text(self, text, width, indent):
694 def _split_lines(self, text, width):
705 def _get_help_string(self, action):
735 def _get_default_metavar_for_optional(self, action):
738 def _get_default_metavar_for_positional(self, action):
768 def __init__(self, argument, message):
769 self.argument_name = _get_action_name(argument)
770 self.message = message
772 def __str__(self):
773 if self.argument_name is None:
777 return format % dict(message=self.message,
778 argument_name=self.argument_name)
841 def __init__(self,
852 self.option_strings = option_strings
853 self.dest = dest
854 self.nargs = nargs
855 self.const = const
856 self.default = default
857 self.type = type
858 self.choices = choices
859 self.required = required
860 self.help = help
861 self.metavar = metavar
863 def _get_kwargs(self):
876 return [(name, getattr(self, name)) for name in names]
878 def format_usage(self):
879 return self.option_strings[0]
881 def __call__(self, parser, namespace, values, option_string=None):
886 def __init__(self,
916 def __call__(self, parser, namespace, values, option_string=None):
917 if option_string in self.option_strings:
918 setattr(namespace, self.dest, not option_string.startswith('--no-'))
920 def format_usage(self):
921 return ' | '.join(self.option_strings)
926 def __init__(self,
943 super(_StoreAction, self).__init__(
955 def __call__(self, parser, namespace, values, option_string=None):
956 setattr(namespace, self.dest, values)
961 def __init__(self,
969 super(_StoreConstAction, self).__init__(
978 def __call__(self, parser, namespace, values, option_string=None):
979 setattr(namespace, self.dest, self.const)
984 def __init__(self,
990 super(_StoreTrueAction, self).__init__(
1001 def __init__(self,
1007 super(_StoreFalseAction, self).__init__(
1018 def __init__(self,
1035 super(_AppendAction, self).__init__(
1047 def __call__(self, parser, namespace, values, option_string=None):
1048 items = getattr(namespace, self.dest, None)
1051 setattr(namespace, self.dest, items)
1056 def __init__(self,
1064 super(_AppendConstAction, self).__init__(
1074 def __call__(self, parser, namespace, values, option_string=None):
1075 items = getattr(namespace, self.dest, None)
1077 items.append(self.const)
1078 setattr(namespace, self.dest, items)
1083 def __init__(self,
1089 super(_CountAction, self).__init__(
1097 def __call__(self, parser, namespace, values, option_string=None):
1098 count = getattr(namespace, self.dest, None)
1101 setattr(namespace, self.dest, count + 1)
1106 def __init__(self,
1111 super(_HelpAction, self).__init__(
1118 def __call__(self, parser, namespace, values, option_string=None):
1125 def __init__(self,
1131 super(_VersionAction, self).__init__(
1137 self.version = version
1139 def __call__(self, parser, namespace, values, option_string=None):
1140 version = self.version
1153 def __init__(self, name, aliases, help):
1157 sup = super(_SubParsersAction._ChoicesPseudoAction, self)
1161 def __init__(self,
1170 self._prog_prefix = prog
1171 self._parser_class = parser_class
1172 self._name_parser_map = {}
1173 self._choices_actions = []
1175 super(_SubParsersAction, self).__init__(
1179 choices=self._name_parser_map,
1184 def add_parser(self, name, **kwargs):
1187 kwargs['prog'] = '%s %s' % (self._prog_prefix, name)
1191 if name in self._name_parser_map:
1192 raise ArgumentError(self, _('conflicting subparser: %s') % name)
1194 if alias in self._name_parser_map:
1196 self, _('conflicting subparser alias: %s') % alias)
1201 choice_action = self._ChoicesPseudoAction(name, aliases, help)
1202 self._choices_actions.append(choice_action)
1205 parser = self._parser_class(**kwargs)
1206 self._name_parser_map[name] = parser
1210 self._name_parser_map[alias] = parser
1214 def _get_subactions(self):
1215 return self._choices_actions
1217 def __call__(self, parser, namespace, values, option_string=None):
1222 if self.dest is not SUPPRESS:
1223 setattr(namespace, self.dest, parser_name)
1227 parser = self._name_parser_map[parser_name]
1230 'choices': ', '.join(self._name_parser_map)}
1232 raise ArgumentError(self, msg)
1250 def __call__(self, parser, namespace, values, option_string=None):
1251 items = getattr(namespace, self.dest, None)
1254 setattr(namespace, self.dest, items)
1277 def __init__(self, mode='r', bufsize=-1, encoding=None, errors=None):
1278 self._mode = mode
1279 self._bufsize = bufsize
1280 self._encoding = encoding
1281 self._errors = errors
1283 def __call__(self, string):
1286 if 'r' in self._mode:
1287 return _sys.stdin.buffer if 'b' in self._mode else _sys.stdin
1288 elif any(c in self._mode for c in 'wax'):
1289 return _sys.stdout.buffer if 'b' in self._mode else _sys.stdout
1291 msg = _('argument "-" with mode %r') % self._mode
1296 return open(string, self._mode, self._bufsize, self._encoding,
1297 self._errors)
1303 def __repr__(self):
1304 args = self._mode, self._bufsize
1305 kwargs = [('encoding', self._encoding), ('errors', self._errors)]
1309 return '%s(%s)' % (type(self).__name__, args_str)
1322 def __init__(self, **kwargs):
1324 setattr(self, name, kwargs[name])
1326 def __eq__(self, other):
1329 return vars(self) == vars(other)
1331 def __contains__(self, key):
1332 return key in self.__dict__
1337 def __init__(self,
1342 super(_ActionsContainer, self).__init__()
1344 self.description = description
1345 self.argument_default = argument_default
1346 self.prefix_chars = prefix_chars
1347 self.conflict_handler = conflict_handler
1350 self._registries = {}
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)
1367 self._get_handler()
1370 self._actions = []
1371 self._option_string_actions = {}
1374 self._action_groups = []
1375 self._mutually_exclusive_groups = []
1378 self._defaults = {}
1381 self._negative_number_matcher = _re.compile(r'^-\d+$|^-\d*\.\d+$')
1385 self._has_negative_number_optionals = []
1390 def register(self, registry_name, value, object):
1391 registry = self._registries.setdefault(registry_name, {})
1394 def _registry_get(self, registry_name, value, default=None):
1395 return self._registries[registry_name].get(value, default)
1400 def set_defaults(self, **kwargs):
1401 self._defaults.update(kwargs)
1405 for action in self._actions:
1409 def get_default(self, dest):
1410 for action in self._actions:
1413 return self._defaults.get(dest, None)
1419 def add_argument(self, *args, **kwargs):
1428 chars = self.prefix_chars
1432 kwargs = self._get_positional_kwargs(*args, **kwargs)
1436 kwargs = self._get_optional_kwargs(*args, **kwargs)
1441 if dest in self._defaults:
1442 kwargs['default'] = self._defaults[dest]
1443 elif self.argument_default is not None:
1444 kwargs['default'] = self.argument_default
1447 action_class = self._pop_action_class(kwargs)
1453 type_func = self._registry_get('type', action.type, action.type)
1462 if hasattr(self, "_get_formatter"):
1464 self._get_formatter()._format_args(action, None)
1468 return self._add_action(action)
1470 def add_argument_group(self, *args, **kwargs):
1471 group = _ArgumentGroup(self, *args, **kwargs)
1472 self._action_groups.append(group)
1475 def add_mutually_exclusive_group(self, **kwargs):
1476 group = _MutuallyExclusiveGroup(self, **kwargs)
1477 self._mutually_exclusive_groups.append(group)
1480 def _add_action(self, action):
1482 self._check_conflict(action)
1485 self._actions.append(action)
1486 action.container = self
1490 self._option_string_actions[option_string] = action
1494 if self._negative_number_matcher.match(option_string):
1495 if not self._has_negative_number_optionals:
1496 self._has_negative_number_optionals.append(True)
1501 def _remove_action(self, action):
1502 self._actions.remove(action)
1504 def _add_container_actions(self, container):
1507 for group in self._action_groups:
1520 title_group_map[group.title] = self.add_argument_group(
1533 mutex_group = self.add_mutually_exclusive_group(
1542 group_map.get(action, self)._add_action(action)
1544 def _get_positional_kwargs(self, dest, **kwargs):
1560 def _get_optional_kwargs(self, *args, **kwargs):
1566 if not option_string[0] in self.prefix_chars:
1568 'prefix_chars': self.prefix_chars}
1575 if len(option_string) > 1 and option_string[1] in self.prefix_chars:
1585 dest = dest_option_string.lstrip(self.prefix_chars)
1594 def _pop_action_class(self, kwargs, default=None):
1596 return self._registry_get('action', action, action)
1598 def _get_handler(self):
1600 handler_func_name = '_handle_conflict_%s' % self.conflict_handler
1602 return getattr(self, handler_func_name)
1605 raise ValueError(msg % self.conflict_handler)
1607 def _check_conflict(self, action):
1612 if option_string in self._option_string_actions:
1613 confl_optional = self._option_string_actions[option_string]
1618 conflict_handler = self._get_handler()
1621 def _handle_conflict_error(self, action, conflicting_actions):
1630 def _handle_conflict_resolve(self, action, conflicting_actions):
1637 self._option_string_actions.pop(option_string, None)
1647 def __init__(self, container, title=None, description=None, **kwargs):
1653 super_init = super(_ArgumentGroup, self).__init__
1657 self.title = title
1658 self._group_actions = []
1661 self._registries = container._registries
1662 self._actions = container._actions
1663 self._option_string_actions = container._option_string_actions
1664 self._defaults = container._defaults
1665 self._has_negative_number_optionals = \
1667 self._mutually_exclusive_groups = container._mutually_exclusive_groups
1669 def _add_action(self, action):
1670 action = super(_ArgumentGroup, self)._add_action(action)
1671 self._group_actions.append(action)
1674 def _remove_action(self, action):
1675 super(_ArgumentGroup, self)._remove_action(action)
1676 self._group_actions.remove(action)
1678 def add_argument_group(self, *args, **kwargs):
1689 def __init__(self, container, required=False):
1690 super(_MutuallyExclusiveGroup, self).__init__(container)
1691 self.required = required
1692 self._container = container
1694 def _add_action(self, action):
1698 action = self._container._add_action(action)
1699 self._group_actions.append(action)
1702 def _remove_action(self, action):
1703 self._container._remove_action(action)
1704 self._group_actions.remove(action)
1706 def add_mutually_exclusive_group(self, *args, **kwargs):
1737 def __init__(self,
1752 superinit = super(ArgumentParser, self).__init__
1762 self.prog = prog
1763 self.usage = usage
1764 self.epilog = epilog
1765 self.formatter_class = formatter_class
1766 self.fromfile_prefix_chars = fromfile_prefix_chars
1767 self.add_help = add_help
1768 self.allow_abbrev = allow_abbrev
1769 self.exit_on_error = exit_on_error
1771 add_group = self.add_argument_group
1772 self._positionals = add_group(_('positional arguments'))
1773 self._optionals = add_group(_('options'))
1774 self._subparsers = None
1779 self.register('type', None, identity)
1784 if self.add_help:
1785 self.add_argument(
1792 self._add_container_actions(parent)
1798 self._defaults.update(defaults)
1803 def _get_kwargs(self):
1812 return [(name, getattr(self, name)) for name in names]
1817 def add_subparsers(self, **kwargs):
1818 if self._subparsers is not None:
1819 self.error(_('cannot have multiple subparser arguments'))
1822 kwargs.setdefault('parser_class', type(self))
1827 self._subparsers = self.add_argument_group(title, description)
1829 self._subparsers = self._positionals
1834 formatter = self._get_formatter()
1835 positionals = self._get_positional_actions()
1836 groups = self._mutually_exclusive_groups
1837 formatter.add_usage(self.usage, positionals, groups, '')
1841 parsers_class = self._pop_action_class(kwargs, 'parsers')
1843 self._subparsers._add_action(action)
1848 def _add_action(self, action):
1850 self._optionals._add_action(action)
1852 self._positionals._add_action(action)
1855 def _get_optional_actions(self):
1857 for action in self._actions
1860 def _get_positional_actions(self):
1862 for action in self._actions
1868 def parse_args(self, args=None, namespace=None):
1869 args, argv = self.parse_known_args(args, namespace)
1872 self.error(msg % ' '.join(argv))
1875 def parse_known_args(self, args=None, namespace=None):
1888 for action in self._actions:
1895 for dest in self._defaults:
1897 setattr(namespace, dest, self._defaults[dest])
1900 if self.exit_on_error:
1902 namespace, args = self._parse_known_args(args, namespace)
1904 self.error(str(err))
1906 namespace, args = self._parse_known_args(args, namespace)
1913 def _parse_known_args(self, arg_strings, namespace):
1915 if self.fromfile_prefix_chars is not None:
1916 arg_strings = self._read_args_from_files(arg_strings)
1921 for mutex_group in self._mutually_exclusive_groups:
1945 option_tuple = self._parse_optional(arg_string)
1962 argument_values = self._get_values(action, argument_strings)
1978 action(self, namespace, argument_values, option_string)
1989 match_argument = self._match_argument
2006 chars = self.prefix_chars
2016 optionals_map = self._option_string_actions
2059 positionals = self._get_positional_actions()
2064 match_partial = self._match_arguments_partial
2125 for action in self._actions:
2139 self._get_value(action, action.default))
2142 self.error(_('the following arguments are required: %s') %
2146 for group in self._mutually_exclusive_groups:
2158 self.error(msg % ' '.join(names))
2163 def _read_args_from_files(self, arg_strings):
2169 if not arg_string or arg_string[0] not in self.fromfile_prefix_chars:
2178 for arg in self.convert_arg_line_to_args(arg_line):
2180 arg_strings = self._read_args_from_files(arg_strings)
2183 self.error(str(err))
2188 def convert_arg_line_to_args(self, arg_line):
2191 def _match_argument(self, action, arg_strings_pattern):
2193 nargs_pattern = self._get_nargs_pattern(action)
2213 def _match_arguments_partial(self, actions, arg_strings_pattern):
2219 pattern = ''.join([self._get_nargs_pattern(action)
2229 def _parse_optional(self, arg_string):
2235 if not arg_string[0] in self.prefix_chars:
2239 if arg_string in self._option_string_actions:
2240 action = self._option_string_actions[arg_string]
2250 if option_string in self._option_string_actions:
2251 action = self._option_string_actions[option_string]
2256 option_tuples = self._get_option_tuples(arg_string)
2264 self.error(msg % args)
2275 if self._negative_number_matcher.match(arg_string):
2276 if not self._has_negative_number_optionals:
2287 def _get_option_tuples(self, option_string):
2292 chars = self.prefix_chars
2294 if self.allow_abbrev:
2300 for option_string in self._option_string_actions:
2302 action = self._option_string_actions[option_string]
2315 for option_string in self._option_string_actions:
2317 action = self._option_string_actions[option_string]
2321 action = self._option_string_actions[option_string]
2327 self.error(_('unexpected option string: %s') % option_string)
2332 def _get_nargs_pattern(self, action):
2381 def parse_intermixed_args(self, args=None, namespace=None):
2382 args, argv = self.parse_known_intermixed_args(args, namespace)
2385 self.error(msg % ' '.join(argv))
2388 def parse_known_intermixed_args(self, args=None, namespace=None):
2401 positionals = self._get_positional_actions()
2408 if [action.dest for group in self._mutually_exclusive_groups
2414 save_usage = self.usage
2416 if self.usage is None:
2418 self.usage = self.format_usage()[7:]
2426 namespace, remaining_args = self.parse_known_args(args,
2440 optionals = self._get_optional_actions()
2447 for group in self._mutually_exclusive_groups:
2450 namespace, extras = self.parse_known_args(remaining_args,
2456 for group in self._mutually_exclusive_groups:
2459 self.usage = save_usage
2465 def _get_values(self, action, arg_strings):
2480 value = self._get_value(action, value)
2481 self._check_value(action, value)
2491 self._check_value(action, value)
2496 value = self._get_value(action, arg_string)
2497 self._check_value(action, value)
2501 value = [self._get_value(action, v) for v in arg_strings]
2505 value = [self._get_value(action, v) for v in arg_strings]
2506 self._check_value(action, value[0])
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):
2522 type_func = self._registry_get('type', action.type, action.type)
2547 def _check_value(self, action, value):
2558 def format_usage(self):
2559 formatter = self._get_formatter()
2560 formatter.add_usage(self.usage, self._actions,
2561 self._mutually_exclusive_groups)
2564 def format_help(self):
2565 formatter = self._get_formatter()
2568 formatter.add_usage(self.usage, self._actions,
2569 self._mutually_exclusive_groups)
2572 formatter.add_text(self.description)
2575 for action_group in self._action_groups:
2582 formatter.add_text(self.epilog)
2587 def _get_formatter(self):
2588 return self.formatter_class(prog=self.prog)
2593 def print_usage(self, file=None):
2596 self._print_message(self.format_usage(), file)
2598 def print_help(self, file=None):
2601 self._print_message(self.format_help(), file)
2603 def _print_message(self, message, file=None):
2614 def exit(self, status=0, message=None):
2616 self._print_message(message, _sys.stderr)
2619 def error(self, message):
2628 self.print_usage(_sys.stderr)
2629 args = {'prog': self.prog, 'message': message}
2630 self.exit(2, _('%(prog)s: error: %(message)s\n') % args)