From: David G. <go...@us...> - 2002-06-20 03:57:02
|
Update of /cvsroot/docutils/docutils/docutils In directory usw-pr-cvs1:/tmp/cvs-serv12604/docutils/docutils Modified Files: optik.py Log Message: - Added new "choice" option type. - Re-enabled optimized help text positioning. - Renamed some methods related to help output. Index: optik.py =================================================================== RCS file: /cvsroot/docutils/docutils/docutils/optik.py,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** optik.py 14 Jun 2002 23:09:10 -0000 1.3 --- optik.py 20 Jun 2002 03:56:59 -0000 1.4 *************** *** 121,124 **** --- 121,132 ---- "option %s: invalid %s value: %r" % (opt, what, value)) + def check_choice(option, opt, value): + if value in option.choices: + return value + else: + raise OptionValueError( + "option %s: invalid choice: %r (choose one of %r)" + % (opt, value, option.choices)) + class Option: *************** *** 128,131 **** --- 136,142 ---- _long_opts : [string] + option_string : string + Set when help output is formatted. + action : string type : string *************** *** 134,137 **** --- 145,149 ---- nargs : int const : any + choices : [string] callback : function callback_args : (any*) *************** *** 149,152 **** --- 161,165 ---- 'nargs', 'const', + 'choices', 'callback', 'callback_args', *************** *** 185,189 **** # The set of known types for option parsers. Again, listed here for # constructor argument validation. ! TYPES = ("string", "int", "long", "float", "complex") # Dictionary of argument checking functions, which convert and --- 198,202 ---- # The set of known types for option parsers. Again, listed here for # constructor argument validation. ! TYPES = ("string", "int", "long", "float", "complex", "choice") # Dictionary of argument checking functions, which convert and *************** *** 207,210 **** --- 220,224 ---- "float" : check_builtin, "complex" : check_builtin, + "choice" : check_choice, } *************** *** 301,306 **** # actions that *require* a type if self.action in ("store", "append"): ! # No type given? "string" is the most sensible default. ! self.type = "string" else: if self.type not in self.TYPES: --- 315,324 ---- # actions that *require* a type if self.action in ("store", "append"): ! if self.choices is not None: ! # The "choices" attribute implies "choice" type. ! self.type = "choice" ! else: ! # No type given? "string" is the most sensible default. ! self.type = "string" else: if self.type not in self.TYPES: *************** *** 310,313 **** --- 328,344 ---- "must not supply a type for action %r" % self.action, self) + def _check_choice(self): + if self.type == "choice": + if self.choices is None: + raise OptionError( + "must supply a list of choices for type 'choice'", self) + elif type(self.choices) not in (TupleType, ListType): + raise OptionError( + "choices must be a list of strings ('%s' supplied)" + % str(type(self.choices)).split("'")[1], self) + elif self.choices is not None: + raise OptionError( + "must not supply choices for type %r" % self.type, self) + def _check_dest (self): if self.action in self.STORE_ACTIONS and self.dest is None: *************** *** 366,369 **** --- 397,401 ---- CHECK_METHODS = [_check_action, _check_type, + _check_choice, _check_dest, _check_const, *************** *** 606,610 **** # -- Feedback methods ---------------------------------------------- ! def get_options(self): if not self.option_list: return "" --- 638,642 ---- # -- Feedback methods ---------------------------------------------- ! def get_option_help(self): if not self.option_list: return "" *************** *** 615,619 **** return "".join(result) ! def get_description(self): if self.description: return self.format.format_description(self.description) --- 647,651 ---- return "".join(result) ! def get_description_help(self): if self.description: return self.format.format_description(self.description) *************** *** 624,629 **** result = "" if self.description: ! result = self.get_description() + "\n" ! return result + self.get_options() --- 656,661 ---- result = "" if self.description: ! result = self.get_description_help() + "\n" ! return result + self.get_option_help() *************** *** 1045,1049 **** sys.exit("%s: error: %s" % (get_prog_name(), msg)) ! def get_usage(self): if self.usage: return self.format.format_usage( --- 1077,1081 ---- sys.exit("%s: error: %s" % (get_prog_name(), msg)) ! def get_usage_help(self): if self.usage: return self.format.format_usage( *************** *** 1062,1068 **** """ if self.usage: ! print >>file, self.get_usage() ! def get_version(self): if self.version: return self.version.replace("%prog", get_prog_name()) --- 1094,1100 ---- """ if self.usage: ! print >>file, self.get_usage_help() ! def get_version_help(self): if self.version: return self.version.replace("%prog", get_prog_name()) *************** *** 1079,1090 **** """ if self.version: ! print >>file, self.get_version() ! def get_options(self): result = [] result.append(self.format.format_heading("Options")) self.format.increase_nesting() if self.option_list: ! result.append(OptionContainer.get_options(self)) result.append("\n") for group in self.option_groups: --- 1111,1123 ---- """ if self.version: ! print >>file, self.get_version_help() ! def get_option_help(self): ! self.format.store_option_strings(self) result = [] result.append(self.format.format_heading("Options")) self.format.increase_nesting() if self.option_list: ! result.append(OptionContainer.get_option_help(self)) result.append("\n") for group in self.option_groups: *************** *** 1098,1105 **** result = [] if self.usage: ! result.append(self.get_usage() + "\n") if self.description: ! result.append(self.get_description() + "\n") ! result.append(self.get_options()) return "".join(result) --- 1131,1138 ---- result = [] if self.usage: ! result.append(self.get_usage_help() + "\n") if self.description: ! result.append(self.get_description_help() + "\n") ! result.append(self.get_option_help()) return "".join(result) *************** *** 1124,1151 **** Instance attributes: indent_increment : int ! number of columns to indent per nesting level ! help_indent : int ! the starting column for option help text width : int ! the overall width (in columns) for output current_indent : int ! in columns, calculated level : int ! increased for each additional nesting level help_width : int ! number of columns available for option help text """ ! def __init__(self, indent_increment, help_indent, width, short_first): self.indent_increment = indent_increment ! self.help_indent = help_indent self.width = width self.current_indent = 0 self.level = 0 ! self.help_width = self.width - self.help_indent if short_first: ! self.format_option_list = self.format_option_list_short_first else: ! self.format_option_list = self.format_option_list_long_first def increase_nesting(self): --- 1157,1188 ---- Instance attributes: indent_increment : int ! The number of columns to indent per nesting level. ! max_help_position : int ! The maximum starting column for option help text. ! help_position : int ! The calculated starting column for option help text; ! initially the same as the maximum. width : int ! The overall width (in columns) for output. current_indent : int ! In columns, calculated. level : int ! Increased for each additional nesting level. help_width : int ! Number of columns available for option help text, calculated. """ ! def __init__(self, indent_increment, max_help_position, width, ! short_first): self.indent_increment = indent_increment ! self.help_position = self.max_help_position = max_help_position self.width = width self.current_indent = 0 self.level = 0 ! self.help_width = width - max_help_position if short_first: ! self.format_option_strings = self.format_option_strings_short_first else: ! self.format_option_strings = self.format_option_strings_long_first def increase_nesting(self): *************** *** 1187,1195 **** # read data from FILENAME result = [] ! opts = self.format_option_list(option) ! opt_width = self.help_indent - self.current_indent - 2 if len(opts) > opt_width: opts = "%*s%s\n" % (self.current_indent, "", opts) ! indent_first = self.help_indent else: # start help on same line as opts opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts) --- 1224,1232 ---- # read data from FILENAME result = [] ! opts = option.option_strings ! opt_width = self.help_position - self.current_indent - 2 if len(opts) > opt_width: opts = "%*s%s\n" % (self.current_indent, "", opts) ! indent_first = self.help_position else: # start help on same line as opts opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts) *************** *** 1199,1203 **** help_lines = wrap_text(option.help, self.help_width) result.append("%*s%s\n" % (indent_first, "", help_lines[0])) ! result.extend(["%*s%s\n" % (self.help_indent, "", line) for line in help_lines[1:]]) elif opts[-1] != "\n": --- 1236,1240 ---- help_lines = wrap_text(option.help, self.help_width) result.append("%*s%s\n" % (indent_first, "", help_lines[0])) ! result.extend(["%*s%s\n" % (self.help_position, "", line) for line in help_lines[1:]]) elif opts[-1] != "\n": *************** *** 1205,1215 **** return "".join(result) ! def format_option_list(self, option): """Return a comma-separated list of option strigs & metavariables.""" raise NotImplementedError( ! "Virtual method; use format_option_list_short_first or " ! "format_option_list_long_first instead.") ! def format_option_list_short_first(self, option): opts = [] # list of "-a" or "--foo=FILE" strings takes_value = option.takes_value() --- 1242,1269 ---- return "".join(result) ! def store_option_strings(self, parser): ! self.increase_nesting() ! max_len = 0 ! for opt in parser.option_list: ! strings = self.format_option_strings(opt) ! opt.option_strings = strings ! max_len = max(max_len, len(strings) + self.current_indent) ! self.increase_nesting() ! for group in parser.option_groups: ! for opt in group.option_list: ! strings = self.format_option_strings(opt) ! opt.option_strings = strings ! max_len = max(max_len, len(strings) + self.current_indent) ! self.decrease_nesting() ! self.decrease_nesting() ! self.help_position = min(max_len + 2, self.max_help_position) ! ! def format_option_strings(self, option): """Return a comma-separated list of option strigs & metavariables.""" raise NotImplementedError( ! "Virtual method; use format_option_strings_short_first or " ! "format_option_strings_long_first instead.") ! def format_option_strings_short_first(self, option): opts = [] # list of "-a" or "--foo=FILE" strings takes_value = option.takes_value() *************** *** 1225,1229 **** return ", ".join(opts) ! def format_option_list_long_first(self, option): opts = [] # list of "-a" or "--foo=FILE" strings takes_value = option.takes_value() --- 1279,1283 ---- return ", ".join(opts) ! def format_option_strings_long_first(self, option): opts = [] # list of "-a" or "--foo=FILE" strings takes_value = option.takes_value() *************** *** 1244,1250 **** """Formats help with indented section bodies.""" ! def __init__(self, indent_increment=2, help_indent=24, width=78, short_first=1): ! HelpFormat.__init__(self, indent_increment, help_indent, width, short_first) --- 1298,1304 ---- """Formats help with indented section bodies.""" ! def __init__(self, indent_increment=2, max_help_position=24, width=78, short_first=1): ! HelpFormat.__init__(self, indent_increment, max_help_position, width, short_first) *************** *** 1260,1266 **** """Formats help with underlined section headers.""" ! def __init__(self, indent_increment=0, help_indent=24, width=78, short_first=None): ! HelpFormat.__init__(self, indent_increment, help_indent, width, short_first) --- 1314,1320 ---- """Formats help with underlined section headers.""" ! def __init__(self, indent_increment=0, max_help_position=24, width=78, short_first=None): ! HelpFormat.__init__(self, indent_increment, max_help_position, width, short_first) |