Lines Matching refs:self
79 def _repr(self):
80 return "<%s at 0x%x: %s>" % (self.__class__.__name__, id(self), self)
104 def __init__(self, msg):
105 self.msg = msg
107 def __str__(self):
108 return self.msg
117 def __init__(self, msg, option):
118 self.msg = msg
119 self.option_id = str(option)
121 def __str__(self):
122 if self.option_id:
123 return "option %s: %s" % (self.option_id, self.msg)
125 return self.msg
142 def __init__(self, opt_str):
143 self.opt_str = opt_str
145 def __str__(self):
146 return _("no such option: %s") % self.opt_str
152 def __init__(self, opt_str, possibilities):
153 BadOptionError.__init__(self, opt_str)
154 self.possibilities = possibilities
156 def __str__(self):
158 % (self.opt_str, ", ".join(self.possibilities)))
206 def __init__(self,
211 self.parser = None
212 self.indent_increment = indent_increment
219 self.width = width
220 self.help_position = self.max_help_position = \
222 self.current_indent = 0
223 self.level = 0
224 self.help_width = None # computed later
225 self.short_first = short_first
226 self.default_tag = "%default"
227 self.option_strings = {}
228 self._short_opt_fmt = "%s %s"
229 self._long_opt_fmt = "%s=%s"
231 def set_parser(self, parser):
232 self.parser = parser
234 def set_short_opt_delimiter(self, delim):
238 self._short_opt_fmt = "%s" + delim + "%s"
240 def set_long_opt_delimiter(self, delim):
244 self._long_opt_fmt = "%s" + delim + "%s"
246 def indent(self):
247 self.current_indent += self.indent_increment
248 self.level += 1
250 def dedent(self):
251 self.current_indent -= self.indent_increment
252 assert self.current_indent >= 0, "Indent decreased below 0."
253 self.level -= 1
255 def format_usage(self, usage):
258 def format_heading(self, heading):
261 def _format_text(self, text):
266 text_width = max(self.width - self.current_indent, 11)
267 indent = " "*self.current_indent
273 def format_description(self, description):
275 return self._format_text(description) + "\n"
279 def format_epilog(self, epilog):
281 return "\n" + self._format_text(epilog) + "\n"
286 def expand_default(self, option):
287 if self.parser is None or not self.default_tag:
290 default_value = self.parser.defaults.get(option.dest)
292 default_value = self.NO_DEFAULT_VALUE
294 return option.help.replace(self.default_tag, str(default_value))
296 def format_option(self, option):
312 opts = self.option_strings[option]
313 opt_width = self.help_position - self.current_indent - 2
315 opts = "%*s%s\n" % (self.current_indent, "", opts)
316 indent_first = self.help_position
318 opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts)
322 help_text = self.expand_default(option)
323 help_lines = textwrap.wrap(help_text, self.help_width)
325 result.extend(["%*s%s\n" % (self.help_position, "", line)
331 def store_option_strings(self, parser):
332 self.indent()
335 strings = self.format_option_strings(opt)
336 self.option_strings[opt] = strings
337 max_len = max(max_len, len(strings) + self.current_indent)
338 self.indent()
341 strings = self.format_option_strings(opt)
342 self.option_strings[opt] = strings
343 max_len = max(max_len, len(strings) + self.current_indent)
344 self.dedent()
345 self.dedent()
346 self.help_position = min(max_len + 2, self.max_help_position)
347 self.help_width = max(self.width - self.help_position, 11)
349 def format_option_strings(self, option):
353 short_opts = [self._short_opt_fmt % (sopt, metavar)
355 long_opts = [self._long_opt_fmt % (lopt, metavar)
361 if self.short_first:
372 def __init__(self,
378 self, indent_increment, max_help_position, width, short_first)
380 def format_usage(self, usage):
383 def format_heading(self, heading):
384 return "%*s%s:\n" % (self.current_indent, "", heading)
391 def __init__(self,
397 self, indent_increment, max_help_position, width, short_first)
399 def format_usage(self, usage):
400 return "%s %s\n" % (self.format_heading(_("Usage")), usage)
402 def format_heading(self, heading):
403 return "%s\n%s\n" % (heading, "=-"[self.level] * len(heading))
564 def __init__(self, *opts, **attrs):
567 self._short_opts = []
568 self._long_opts = []
569 opts = self._check_opt_strings(opts)
570 self._set_opt_strings(opts)
573 self._set_attrs(attrs)
580 for checker in self.CHECK_METHODS:
581 checker(self)
583 def _check_opt_strings(self, opts):
592 def _set_opt_strings(self, opts):
597 "must be at least two characters long" % opt, self)
603 self)
604 self._short_opts.append(opt)
610 self)
611 self._long_opts.append(opt)
613 def _set_attrs(self, attrs):
614 for attr in self.ATTRS:
616 setattr(self, attr, attrs[attr])
620 setattr(self, attr, NO_DEFAULT)
622 setattr(self, attr, None)
627 self)
632 def _check_action(self):
633 if self.action is None:
634 self.action = "store"
635 elif self.action not in self.ACTIONS:
636 raise OptionError("invalid action: %r" % self.action, self)
638 def _check_type(self):
639 if self.type is None:
640 if self.action in self.ALWAYS_TYPED_ACTIONS:
641 if self.choices is not None:
643 self.type = "choice"
646 self.type = "string"
650 if isinstance(self.type, type):
651 self.type = self.type.__name__
653 if self.type == "str":
654 self.type = "string"
656 if self.type not in self.TYPES:
657 raise OptionError("invalid option type: %r" % self.type, self)
658 if self.action not in self.TYPED_ACTIONS:
660 "must not supply a type for action %r" % self.action, self)
662 def _check_choice(self):
663 if self.type == "choice":
664 if self.choices is None:
666 "must supply a list of choices for type 'choice'", self)
667 elif not isinstance(self.choices, (tuple, list)):
670 % str(type(self.choices)).split("'")[1], self)
671 elif self.choices is not None:
673 "must not supply choices for type %r" % self.type, self)
675 def _check_dest(self):
677 # self.type check is for callbacks that take a value.
678 takes_value = (self.action in self.STORE_ACTIONS or
679 self.type is not None)
680 if self.dest is None and takes_value:
684 if self._long_opts:
686 self.dest = self._long_opts[0][2:].replace('-', '_')
688 self.dest = self._short_opts[0][1]
690 def _check_const(self):
691 if self.action not in self.CONST_ACTIONS and self.const is not None:
693 "'const' must not be supplied for action %r" % self.action,
694 self)
696 def _check_nargs(self):
697 if self.action in self.TYPED_ACTIONS:
698 if self.nargs is None:
699 self.nargs = 1
700 elif self.nargs is not None:
702 "'nargs' must not be supplied for action %r" % self.action,
703 self)
705 def _check_callback(self):
706 if self.action == "callback":
707 if not callable(self.callback):
709 "callback not callable: %r" % self.callback, self)
710 if (self.callback_args is not None and
711 not isinstance(self.callback_args, tuple)):
714 % self.callback_args, self)
715 if (self.callback_kwargs is not None and
716 not isinstance(self.callback_kwargs, dict)):
719 % self.callback_kwargs, self)
721 if self.callback is not None:
724 % self.callback, self)
725 if self.callback_args is not None:
727 "callback_args supplied for non-callback option", self)
728 if self.callback_kwargs is not None:
730 "callback_kwargs supplied for non-callback option", self)
744 def __str__(self):
745 return "/".join(self._short_opts + self._long_opts)
749 def takes_value(self):
750 return self.type is not None
752 def get_opt_string(self):
753 if self._long_opts:
754 return self._long_opts[0]
756 return self._short_opts[0]
761 def check_value(self, opt, value):
762 checker = self.TYPE_CHECKER.get(self.type)
766 return checker(self, opt, value)
768 def convert_value(self, opt, value):
770 if self.nargs == 1:
771 return self.check_value(opt, value)
773 return tuple([self.check_value(opt, v) for v in value])
775 def process(self, opt, value, values, parser):
779 value = self.convert_value(opt, value)
784 return self.take_action(
785 self.action, self.dest, opt, value, values, parser)
787 def take_action(self, action, dest, opt, value, values, parser):
791 setattr(values, dest, self.const)
799 values.ensure_value(dest, []).append(self.const)
803 args = self.callback_args or ()
804 kwargs = self.callback_kwargs or {}
805 self.callback(self, opt, value, parser, *args, **kwargs)
813 raise ValueError("unknown action %r" % self.action)
825 def __init__(self, defaults=None):
828 setattr(self, attr, val)
830 def __str__(self):
831 return str(self.__dict__)
835 def __eq__(self, other):
837 return self.__dict__ == other.__dict__
839 return self.__dict__ == other
843 def _update_careful(self, dict):
847 in self. Any keys in dict without a corresponding attribute
850 for attr in dir(self):
854 setattr(self, attr, dval)
856 def _update_loose(self, dict):
860 they have a corresponding attribute in self or not.
862 self.__dict__.update(dict)
864 def _update(self, dict, mode):
866 self._update_careful(dict)
868 self._update_loose(dict)
872 def read_module(self, modname, mode="careful"):
875 self._update(vars(mod), mode)
877 def read_file(self, filename, mode="careful"):
880 self._update(vars, mode)
882 def ensure_value(self, attr, value):
883 if not hasattr(self, attr) or getattr(self, attr) is None:
884 setattr(self, attr, value)
885 return getattr(self, attr)
920 def __init__(self, option_class, conflict_handler, description):
925 self._create_option_list()
927 self.option_class = option_class
928 self.set_conflict_handler(conflict_handler)
929 self.set_description(description)
931 def _create_option_mappings(self):
935 self._short_opt = {} # single letter -> Option instance
936 self._long_opt = {} # long option -> Option instance
937 self.defaults = {} # maps option dest -> default value
940 def _share_option_mappings(self, parser):
943 self._short_opt = parser._short_opt
944 self._long_opt = parser._long_opt
945 self.defaults = parser.defaults
947 def set_conflict_handler(self, handler):
950 self.conflict_handler = handler
952 def set_description(self, description):
953 self.description = description
955 def get_description(self):
956 return self.description
959 def destroy(self):
961 del self._short_opt
962 del self._long_opt
963 del self.defaults
968 def _check_conflict(self, option):
971 if opt in self._short_opt:
972 conflict_opts.append((opt, self._short_opt[opt]))
974 if opt in self._long_opt:
975 conflict_opts.append((opt, self._long_opt[opt]))
978 handler = self.conflict_handler
988 del self._long_opt[opt]
991 del self._short_opt[opt]
995 def add_option(self, *args, **kwargs):
1000 option = self.option_class(*args, **kwargs)
1008 self._check_conflict(option)
1010 self.option_list.append(option)
1011 option.container = self
1013 self._short_opt[opt] = option
1015 self._long_opt[opt] = option
1019 self.defaults[option.dest] = option.default
1020 elif option.dest not in self.defaults:
1021 self.defaults[option.dest] = None
1025 def add_options(self, option_list):
1027 self.add_option(option)
1031 def get_option(self, opt_str):
1032 return (self._short_opt.get(opt_str) or
1033 self._long_opt.get(opt_str))
1035 def has_option(self, opt_str):
1036 return (opt_str in self._short_opt or
1037 opt_str in self._long_opt)
1039 def remove_option(self, opt_str):
1040 option = self._short_opt.get(opt_str)
1042 option = self._long_opt.get(opt_str)
1047 del self._short_opt[opt]
1049 del self._long_opt[opt]
1055 def format_option_help(self, formatter):
1056 if not self.option_list:
1059 for option in self.option_list:
1064 def format_description(self, formatter):
1065 return formatter.format_description(self.get_description())
1067 def format_help(self, formatter):
1069 if self.description:
1070 result.append(self.format_description(formatter))
1071 if self.option_list:
1072 result.append(self.format_option_help(formatter))
1078 def __init__(self, parser, title, description=None):
1079 self.parser = parser
1081 self, parser.option_class, parser.conflict_handler, description)
1082 self.title = title
1084 def _create_option_list(self):
1085 self.option_list = []
1086 self._share_option_mappings(self.parser)
1088 def set_title(self, title):
1089 self.title = title
1091 def destroy(self):
1093 OptionContainer.destroy(self)
1094 del self.option_list
1098 def format_help(self, formatter):
1099 result = formatter.format_heading(self.title)
1101 result += OptionContainer.format_help(self, formatter)
1118 your program (self.prog or os.path.basename(sys.argv[0])).
1178 def __init__(self,
1190 self, option_class, conflict_handler, description)
1191 self.set_usage(usage)
1192 self.prog = prog
1193 self.version = version
1194 self.allow_interspersed_args = True
1195 self.process_default_values = True
1198 self.formatter = formatter
1199 self.formatter.set_parser(self)
1200 self.epilog = epilog
1206 self._populate_option_list(option_list,
1209 self._init_parsing_state()
1212 def destroy(self):
1219 OptionContainer.destroy(self)
1220 for group in self.option_groups:
1222 del self.option_list
1223 del self.option_groups
1224 del self.formatter
1230 def _create_option_list(self):
1231 self.option_list = []
1232 self.option_groups = []
1233 self._create_option_mappings()
1235 def _add_help_option(self):
1236 self.add_option("-h", "--help",
1240 def _add_version_option(self):
1241 self.add_option("--version",
1245 def _populate_option_list(self, option_list, add_help=True):
1246 if self.standard_option_list:
1247 self.add_options(self.standard_option_list)
1249 self.add_options(option_list)
1250 if self.version:
1251 self._add_version_option()
1253 self._add_help_option()
1255 def _init_parsing_state(self):
1257 self.rargs = None
1258 self.largs = None
1259 self.values = None
1264 def set_usage(self, usage):
1266 self.usage = _("%prog [options]")
1268 self.usage = None
1271 self.usage = usage[7:]
1273 self.usage = usage
1275 def enable_interspersed_args(self):
1281 self.allow_interspersed_args = True
1283 def disable_interspersed_args(self):
1289 self.allow_interspersed_args = False
1291 def set_process_default_values(self, process):
1292 self.process_default_values = process
1294 def set_default(self, dest, value):
1295 self.defaults[dest] = value
1297 def set_defaults(self, **kwargs):
1298 self.defaults.update(kwargs)
1300 def _get_all_options(self):
1301 options = self.option_list[:]
1302 for group in self.option_groups:
1306 def get_default_values(self):
1307 if not self.process_default_values:
1309 return Values(self.defaults)
1311 defaults = self.defaults.copy()
1312 for option in self._get_all_options():
1323 def add_option_group(self, *args, **kwargs):
1326 group = OptionGroup(self, *args, **kwargs)
1331 if group.parser is not self:
1336 self.option_groups.append(group)
1339 def get_option_group(self, opt_str):
1340 option = (self._short_opt.get(opt_str) or
1341 self._long_opt.get(opt_str))
1342 if option and option.container is not self:
1349 def _get_args(self, args):
1355 def parse_args(self, args=None, values=None):
1369 rargs = self._get_args(args)
1371 values = self.get_default_values()
1382 self.rargs = rargs
1383 self.largs = largs = []
1384 self.values = values
1387 stop = self._process_args(largs, rargs, values)
1389 self.error(str(err))
1392 return self.check_values(values, args)
1394 def check_values(self, values, args):
1407 def _process_args(self, largs, rargs, values):
1427 self._process_long_opt(rargs, values)
1431 self._process_short_opts(rargs, values)
1432 elif self.allow_interspersed_args:
1458 def _match_long_opt(self, opt):
1465 return _match_abbrev(opt, self._long_opt)
1467 def _process_long_opt(self, rargs, values):
1480 opt = self._match_long_opt(opt)
1481 option = self._long_opt[opt]
1485 self.error(ngettext(
1496 self.error(_("%s option does not take a value") % opt)
1501 option.process(opt, value, values, self)
1503 def _process_short_opts(self, rargs, values):
1509 option = self._short_opt.get(opt)
1523 self.error(ngettext(
1536 option.process(opt, value, values, self)
1544 def get_prog_name(self):
1545 if self.prog is None:
1548 return self.prog
1550 def expand_prog_name(self, s):
1551 return s.replace("%prog", self.get_prog_name())
1553 def get_description(self):
1554 return self.expand_prog_name(self.description)
1556 def exit(self, status=0, msg=None):
1561 def error(self, msg):
1568 self.print_usage(sys.stderr)
1569 self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
1571 def get_usage(self):
1572 if self.usage:
1573 return self.formatter.format_usage(
1574 self.expand_prog_name(self.usage))
1578 def print_usage(self, file=None):
1581 Print the usage message for the current program (self.usage) to
1583 self.usage is replaced with the name of the current program
1584 (basename of sys.argv[0]). Does nothing if self.usage is empty
1587 if self.usage:
1588 print(self.get_usage(), file=file)
1590 def get_version(self):
1591 if self.version:
1592 return self.expand_prog_name(self.version)
1596 def print_version(self, file=None):
1599 Print the version message for this program (self.version) to
1601 of "%prog" in self.version is replaced by the current program's
1602 name. Does nothing if self.version is empty or undefined.
1604 if self.version:
1605 print(self.get_version(), file=file)
1607 def format_option_help(self, formatter=None):
1609 formatter = self.formatter
1610 formatter.store_option_strings(self)
1614 if self.option_list:
1615 result.append(OptionContainer.format_option_help(self, formatter))
1617 for group in self.option_groups:
1624 def format_epilog(self, formatter):
1625 return formatter.format_epilog(self.epilog)
1627 def format_help(self, formatter=None):
1629 formatter = self.formatter
1631 if self.usage:
1632 result.append(self.get_usage() + "\n")
1633 if self.description:
1634 result.append(self.format_description(formatter) + "\n")
1635 result.append(self.format_option_help(formatter))
1636 result.append(self.format_epilog(formatter))
1639 def print_help(self, file=None):
1647 file.write(self.format_help())