Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes
optparse.HelpFormatter Class Reference
Inheritance diagram for optparse.HelpFormatter:
Inheritance graph
[legend]
Collaboration diagram for optparse.HelpFormatter:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def set_parser
def set_short_opt_delimiter
def set_long_opt_delimiter
def indent
def dedent
def format_usage
def format_heading
def format_description
def format_epilog
def expand_default
def format_option
def store_option_strings
def format_option_strings

Public Attributes

 parser
 indent_increment
 help_position
 max_help_position
 width
 current_indent
 level
 help_width
 short_first
 default_tag
 option_strings

Static Public Attributes

string NO_DEFAULT_VALUE = "none"

Private Member Functions

def _format_text

Private Attributes

 _short_opt_fmt
 _long_opt_fmt

Detailed Description

Abstract base class for formatting option help.  OptionParser
instances should use one of the HelpFormatter subclasses for
formatting help; by default IndentedHelpFormatter is used.

Instance attributes:
  parser : OptionParser
    the controlling OptionParser instance
  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
    total number of columns for output (pass None to constructor for
    this value to be taken from the $COLUMNS environment variable)
  level : int
    current indentation level
  current_indent : int
    current indentation level (in columns)
  help_width : int
    number of columns available for option help text (calculated)
  default_tag : str
    text to replace with each option's default value, "%default"
    by default.  Set to false value to disable default value expansion.
  option_strings : { Option : str }
    maps Option instances to the snippet of help text explaining
    the syntax of that option, e.g. "-h, --help" or
    "-fFILE, --file=FILE"
  _short_opt_fmt : str
    format string controlling how short options with values are
    printed in help text.  Must be either "%s%s" ("-fFILE") or
    "%s %s" ("-f FILE"), because those are the two syntaxes that
    Optik supports.
  _long_opt_fmt : str
    similar but for long options; must be either "%s %s" ("--file FILE")
    or "%s=%s" ("--file=FILE").

Definition at line 154 of file optparse.py.


Constructor & Destructor Documentation

def optparse.HelpFormatter.__init__ (   self,
  indent_increment,
  max_help_position,
  width,
  short_first 
)

Reimplemented in optparse.TitledHelpFormatter, and optparse.IndentedHelpFormatter.

Definition at line 203 of file optparse.py.

00203 
00204                  short_first):
00205         self.parser = None
00206         self.indent_increment = indent_increment
00207         self.help_position = self.max_help_position = max_help_position
00208         if width is None:
00209             try:
00210                 width = int(os.environ['COLUMNS'])
00211             except (KeyError, ValueError):
00212                 width = 80
00213             width -= 2
00214         self.width = width
00215         self.current_indent = 0
00216         self.level = 0
00217         self.help_width = None          # computed later
00218         self.short_first = short_first
00219         self.default_tag = "%default"
00220         self.option_strings = {}
00221         self._short_opt_fmt = "%s %s"
00222         self._long_opt_fmt = "%s=%s"

Here is the caller graph for this function:


Member Function Documentation

def optparse.HelpFormatter._format_text (   self,
  text 
) [private]
Format a paragraph of free-form text for inclusion in the
help output at the current indentation level.

Definition at line 253 of file optparse.py.

00253 
00254     def _format_text(self, text):
00255         """
00256         Format a paragraph of free-form text for inclusion in the
00257         help output at the current indentation level.
00258         """
00259         text_width = self.width - self.current_indent
00260         indent = " "*self.current_indent
00261         return textwrap.fill(text,
00262                              text_width,
00263                              initial_indent=indent,
00264                              subsequent_indent=indent)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 242 of file optparse.py.

00242 
00243     def dedent(self):
00244         self.current_indent -= self.indent_increment
00245         assert self.current_indent >= 0, "Indent decreased below 0."
00246         self.level -= 1

Here is the caller graph for this function:

def optparse.HelpFormatter.expand_default (   self,
  option 
)

Definition at line 278 of file optparse.py.

00278 
00279     def expand_default(self, option):
00280         if self.parser is None or not self.default_tag:
00281             return option.help
00282 
00283         default_value = self.parser.defaults.get(option.dest)
00284         if default_value is NO_DEFAULT or default_value is None:
00285             default_value = self.NO_DEFAULT_VALUE
00286 
00287         return option.help.replace(self.default_tag, str(default_value))

Here is the caller graph for this function:

def optparse.HelpFormatter.format_description (   self,
  description 
)

Definition at line 265 of file optparse.py.

00265 
00266     def format_description(self, description):
00267         if description:
00268             return self._format_text(description) + "\n"
00269         else:
00270             return ""

Here is the call graph for this function:

Here is the caller graph for this function:

def optparse.HelpFormatter.format_epilog (   self,
  epilog 
)

Definition at line 271 of file optparse.py.

00271 
00272     def format_epilog(self, epilog):
00273         if epilog:
00274             return "\n" + self._format_text(epilog) + "\n"
00275         else:
00276             return ""
00277 

Here is the call graph for this function:

Here is the caller graph for this function:

def optparse.HelpFormatter.format_heading (   self,
  heading 
)

Reimplemented in optparse.TitledHelpFormatter, and optparse.IndentedHelpFormatter.

Definition at line 250 of file optparse.py.

00250 
00251     def format_heading(self, heading):
00252         raise NotImplementedError("subclasses must implement")

Here is the caller graph for this function:

def optparse.HelpFormatter.format_option (   self,
  option 
)

Definition at line 288 of file optparse.py.

00288 
00289     def format_option(self, option):
00290         # The help for each option consists of two parts:
00291         #   * the opt strings and metavars
00292         #     eg. ("-x", or "-fFILENAME, --file=FILENAME")
00293         #   * the user-supplied help string
00294         #     eg. ("turn on expert mode", "read data from FILENAME")
00295         #
00296         # If possible, we write both of these on the same line:
00297         #   -x      turn on expert mode
00298         #
00299         # But if the opt string list is too long, we put the help
00300         # string on a second line, indented to the same column it would
00301         # start in if it fit on the first line.
00302         #   -fFILENAME, --file=FILENAME
00303         #           read data from FILENAME
00304         result = []
00305         opts = self.option_strings[option]
00306         opt_width = self.help_position - self.current_indent - 2
00307         if len(opts) > opt_width:
00308             opts = "%*s%s\n" % (self.current_indent, "", opts)
00309             indent_first = self.help_position
00310         else:                       # start help on same line as opts
00311             opts = "%*s%-*s  " % (self.current_indent, "", opt_width, opts)
00312             indent_first = 0
00313         result.append(opts)
00314         if option.help:
00315             help_text = self.expand_default(option)
00316             help_lines = textwrap.wrap(help_text, self.help_width)
00317             result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
00318             result.extend(["%*s%s\n" % (self.help_position, "", line)
00319                            for line in help_lines[1:]])
00320         elif opts[-1] != "\n":
00321             result.append("\n")
00322         return "".join(result)

Here is the call graph for this function:

def optparse.HelpFormatter.format_option_strings (   self,
  option 
)
Return a comma-separated list of option strings & metavariables.

Definition at line 341 of file optparse.py.

00341 
00342     def format_option_strings(self, option):
00343         """Return a comma-separated list of option strings & metavariables."""
00344         if option.takes_value():
00345             metavar = option.metavar or option.dest.upper()
00346             short_opts = [self._short_opt_fmt % (sopt, metavar)
00347                           for sopt in option._short_opts]
00348             long_opts = [self._long_opt_fmt % (lopt, metavar)
00349                          for lopt in option._long_opts]
00350         else:
00351             short_opts = option._short_opts
00352             long_opts = option._long_opts
00353 
00354         if self.short_first:
00355             opts = short_opts + long_opts
00356         else:
00357             opts = long_opts + short_opts
00358 
00359         return ", ".join(opts)

Here is the caller graph for this function:

def optparse.HelpFormatter.format_usage (   self,
  usage 
)

Reimplemented in optparse.TitledHelpFormatter, and optparse.IndentedHelpFormatter.

Definition at line 247 of file optparse.py.

00247 
00248     def format_usage(self, usage):
00249         raise NotImplementedError("subclasses must implement")

Definition at line 238 of file optparse.py.

00238 
00239     def indent(self):
00240         self.current_indent += self.indent_increment
00241         self.level += 1

Here is the caller graph for this function:

def optparse.HelpFormatter.set_long_opt_delimiter (   self,
  delim 
)

Definition at line 232 of file optparse.py.

00232 
00233     def set_long_opt_delimiter(self, delim):
00234         if delim not in ("=", " "):
00235             raise ValueError(
00236                 "invalid metavar delimiter for long options: %r" % delim)
00237         self._long_opt_fmt = "%s" + delim + "%s"

def optparse.HelpFormatter.set_parser (   self,
  parser 
)

Definition at line 223 of file optparse.py.

00223 
00224     def set_parser(self, parser):
00225         self.parser = parser

Definition at line 226 of file optparse.py.

00226 
00227     def set_short_opt_delimiter(self, delim):
00228         if delim not in ("", " "):
00229             raise ValueError(
00230                 "invalid metavar delimiter for short options: %r" % delim)
00231         self._short_opt_fmt = "%s" + delim + "%s"

def optparse.HelpFormatter.store_option_strings (   self,
  parser 
)

Definition at line 323 of file optparse.py.

00323 
00324     def store_option_strings(self, parser):
00325         self.indent()
00326         max_len = 0
00327         for opt in parser.option_list:
00328             strings = self.format_option_strings(opt)
00329             self.option_strings[opt] = strings
00330             max_len = max(max_len, len(strings) + self.current_indent)
00331         self.indent()
00332         for group in parser.option_groups:
00333             for opt in group.option_list:
00334                 strings = self.format_option_strings(opt)
00335                 self.option_strings[opt] = strings
00336                 max_len = max(max_len, len(strings) + self.current_indent)
00337         self.dedent()
00338         self.dedent()
00339         self.help_position = min(max_len + 2, self.max_help_position)
00340         self.help_width = self.width - self.help_position

Here is the call graph for this function:


Member Data Documentation

Definition at line 221 of file optparse.py.

Definition at line 220 of file optparse.py.

Definition at line 214 of file optparse.py.

Definition at line 218 of file optparse.py.

Definition at line 206 of file optparse.py.

Definition at line 216 of file optparse.py.

Definition at line 205 of file optparse.py.

Definition at line 215 of file optparse.py.

Definition at line 206 of file optparse.py.

Definition at line 197 of file optparse.py.

Definition at line 219 of file optparse.py.

Definition at line 204 of file optparse.py.

Definition at line 217 of file optparse.py.

Definition at line 213 of file optparse.py.


The documentation for this class was generated from the following file: