CMS 3D CMS Logo

Classes | Public Member Functions | Private Member Functions | Private Attributes

argparse::HelpFormatter Class Reference

Inheritance diagram for argparse::HelpFormatter:
argparse::ArgumentDefaultsHelpFormatter argparse::ArgumentDefaultsHelpFormatter argparse::RawDescriptionHelpFormatter argparse::RawDescriptionHelpFormatter argparse::RawTextHelpFormatter argparse::RawTextHelpFormatter argparse::RawTextHelpFormatter argparse::RawTextHelpFormatter

List of all members.

Classes

class  _Section

Public Member Functions

def __init__
def __init__
def add_argument
def add_argument
def add_arguments
def add_arguments
def add_text
def add_text
def add_usage
def add_usage
def end_section
def end_section
def format_help
def format_help
def start_section
def start_section

Private Member Functions

def _add_item
def _add_item
def _dedent
def _dedent
def _expand_help
def _expand_help
def _fill_text
def _fill_text
def _format_action
def _format_action
def _format_action_invocation
def _format_action_invocation
def _format_actions_usage
def _format_actions_usage
def _format_args
def _format_args
def _format_text
def _format_text
def _format_usage
def _format_usage
def _get_help_string
def _get_help_string
def _indent
def _indent
def _iter_indented_subactions
def _iter_indented_subactions
def _join_parts
def _join_parts
def _metavar_formatter
def _metavar_formatter
def _split_lines
def _split_lines

Private Attributes

 _action_max_length
 _current_indent
 _current_section
 _indent_increment
 _level
 _long_break_matcher
 _max_help_position
 _prog
 _root_section
 _whitespace_matcher
 _width

Detailed Description

Formatter for generating usage messages and argument help strings.

Only the name of this class is considered a public API. All the methods
provided by the class are considered an implementation detail.

Definition at line 146 of file argparse.py.


Constructor & Destructor Documentation

def argparse::HelpFormatter::__init__ (   self,
  prog,
  indent_increment = 2,
  max_help_position = 24,
  width = None 
)

Definition at line 153 of file argparse.py.

00158                             :
00159 
00160         # default setting for width
00161         if width is None:
00162             try:
00163                 width = int(_os.environ['COLUMNS'])
00164             except (KeyError, ValueError):
00165                 width = 80
00166             width -= 2
00167 
00168         self._prog = prog
00169         self._indent_increment = indent_increment
00170         self._max_help_position = max_help_position
00171         self._width = width
00172 
00173         self._current_indent = 0
00174         self._level = 0
00175         self._action_max_length = 0
00176 
00177         self._root_section = self._Section(self, None)
00178         self._current_section = self._root_section
00179 
00180         self._whitespace_matcher = _re.compile(r'\s+')
00181         self._long_break_matcher = _re.compile(r'\n\n\n+')

def argparse::HelpFormatter::__init__ (   self,
  prog,
  indent_increment = 2,
  max_help_position = 24,
  width = None 
)

Definition at line 184 of file argparse.py.

00188                             :
00189 
00190         # default setting for width
00191         if width is None:
00192             try:
00193                 width = int(_os.environ['COLUMNS'])
00194             except (KeyError, ValueError):
00195                 width = 80
00196             width -= 2
00197 
00198         self._prog = prog
00199         self._indent_increment = indent_increment
00200         self._max_help_position = max_help_position
00201         self._width = width
00202 
00203         self._current_indent = 0
00204         self._level = 0
00205         self._action_max_length = 0
00206 
00207         self._root_section = self._Section(self, None)
00208         self._current_section = self._root_section
00209 
00210         self._whitespace_matcher = _re.compile(r'\s+')
00211         self._long_break_matcher = _re.compile(r'\n\n\n+')
00212 

Member Function Documentation

def argparse::HelpFormatter::_add_item (   self,
  func,
  args 
) [private]

Definition at line 227 of file argparse.py.

00228                                    :
00229         self._current_section.items.append((func, args))

def argparse::HelpFormatter::_add_item (   self,
  func,
  args 
) [private]

Definition at line 258 of file argparse.py.

00258                                    :
00259         self._current_section.items.append((func, args))
00260 
def argparse::HelpFormatter::_dedent (   self) [private]

Definition at line 189 of file argparse.py.

00190                      :
00191         self._current_indent -= self._indent_increment
00192         assert self._current_indent >= 0, 'Indent decreased below 0.'
00193         self._level -= 1

def argparse::HelpFormatter::_dedent (   self) [private]

Definition at line 220 of file argparse.py.

00220                      :
00221         self._current_indent -= self._indent_increment
00222         assert self._current_indent >= 0, 'Indent decreased below 0.'
00223         self._level -= 1
00224 
def argparse::HelpFormatter::_expand_help (   self,
  action 
) [private]

Definition at line 589 of file argparse.py.

00590                                   :
00591         params = dict(vars(action), prog=self._prog)
00592         for name in list(params):
00593             if params[name] is SUPPRESS:
00594                 del params[name]
00595         for name in list(params):
00596             if hasattr(params[name], '__name__'):
00597                 params[name] = params[name].__name__
00598         if params.get('choices') is not None:
00599             choices_str = ', '.join([str(c) for c in params['choices']])
00600             params['choices'] = choices_str
00601         return self._get_help_string(action) % params

def argparse::HelpFormatter::_expand_help (   self,
  action 
) [private]

Definition at line 610 of file argparse.py.

00610                                   :
00611         params = dict(vars(action), prog=self._prog)
00612         for name in list(params):
00613             if params[name] is SUPPRESS:
00614                 del params[name]
00615         if params.get('choices') is not None:
00616             choices_str = ', '.join([str(c) for c in params['choices']])
00617             params['choices'] = choices_str
00618         return self._get_help_string(action) % params
00619 
def argparse::HelpFormatter::_fill_text (   self,
  text,
  width,
  indent 
) [private]

Reimplemented in argparse::RawDescriptionHelpFormatter, and argparse::RawDescriptionHelpFormatter.

Definition at line 617 of file argparse.py.

00618                                              :
00619         text = self._whitespace_matcher.sub(' ', text).strip()
00620         return _textwrap.fill(text, width, initial_indent=indent,
00621                                            subsequent_indent=indent)

def argparse::HelpFormatter::_fill_text (   self,
  text,
  width,
  indent 
) [private]

Reimplemented in argparse::RawDescriptionHelpFormatter, and argparse::RawDescriptionHelpFormatter.

Definition at line 635 of file argparse.py.

00635                                              :
00636         text = self._whitespace_matcher.sub(' ', text).strip()
00637         return _textwrap.fill(text, width, initial_indent=indent,
00638                                            subsequent_indent=indent)
00639 
def argparse::HelpFormatter::_format_action (   self,
  action 
) [private]

Definition at line 507 of file argparse.py.

00507                                     :
00508         # determine the required width and the entry label
00509         help_position = min(self._action_max_length + 2,
00510                             self._max_help_position)
00511         help_width = self._width - help_position
00512         action_width = help_position - self._current_indent - 2
00513         action_header = self._format_action_invocation(action)
00514 
00515         # ho nelp; start on same line and add a final newline
00516         if not action.help:
00517             tup = self._current_indent, '', action_header
00518             action_header = '%*s%s\n' % tup
00519 
00520         # short action name; start on the same line and pad two spaces
00521         elif len(action_header) <= action_width:
00522             tup = self._current_indent, '', action_width, action_header
00523             action_header = '%*s%-*s  ' % tup
00524             indent_first = 0
00525 
00526         # long action name; start on the next line
00527         else:
00528             tup = self._current_indent, '', action_header
00529             action_header = '%*s%s\n' % tup
00530             indent_first = help_position
00531 
00532         # collect the pieces of the action help
00533         parts = [action_header]
00534 
00535         # if there was help for the action, add lines of help text
00536         if action.help:
00537             help_text = self._expand_help(action)
00538             help_lines = self._split_lines(help_text, help_width)
00539             parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
00540             for line in help_lines[1:]:
00541                 parts.append('%*s%s\n' % (help_position, '', line))
00542 
00543         # or add a newline if the description doesn't end with one
00544         elif not action_header.endswith('\n'):
00545             parts.append('\n')
00546 
00547         # if there are any sub-actions, add their help as well
00548         for subaction in self._iter_indented_subactions(action):
00549             parts.append(self._format_action(subaction))
00550 
00551         # return a single string
00552         return self._join_parts(parts)
00553 
def argparse::HelpFormatter::_format_action (   self,
  action 
) [private]

Definition at line 484 of file argparse.py.

00485                                     :
00486         # determine the required width and the entry label
00487         help_position = min(self._action_max_length + 2,
00488                             self._max_help_position)
00489         help_width = self._width - help_position
00490         action_width = help_position - self._current_indent - 2
00491         action_header = self._format_action_invocation(action)
00492 
00493         # ho nelp; start on same line and add a final newline
00494         if not action.help:
00495             tup = self._current_indent, '', action_header
00496             action_header = '%*s%s\n' % tup
00497 
00498         # short action name; start on the same line and pad two spaces
00499         elif len(action_header) <= action_width:
00500             tup = self._current_indent, '', action_width, action_header
00501             action_header = '%*s%-*s  ' % tup
00502             indent_first = 0
00503 
00504         # long action name; start on the next line
00505         else:
00506             tup = self._current_indent, '', action_header
00507             action_header = '%*s%s\n' % tup
00508             indent_first = help_position
00509 
00510         # collect the pieces of the action help
00511         parts = [action_header]
00512 
00513         # if there was help for the action, add lines of help text
00514         if action.help:
00515             help_text = self._expand_help(action)
00516             help_lines = self._split_lines(help_text, help_width)
00517             parts.append('%*s%s\n' % (indent_first, '', help_lines[0]))
00518             for line in help_lines[1:]:
00519                 parts.append('%*s%s\n' % (help_position, '', line))
00520 
00521         # or add a newline if the description doesn't end with one
00522         elif not action_header.endswith('\n'):
00523             parts.append('\n')
00524 
00525         # if there are any sub-actions, add their help as well
00526         for subaction in self._iter_indented_subactions(action):
00527             parts.append(self._format_action(subaction))
00528 
00529         # return a single string
00530         return self._join_parts(parts)

def argparse::HelpFormatter::_format_action_invocation (   self,
  action 
) [private]

Definition at line 531 of file argparse.py.

00532                                                :
00533         if not action.option_strings:
00534             metavar, = self._metavar_formatter(action, action.dest)(1)
00535             return metavar
00536 
00537         else:
00538             parts = []
00539 
00540             # if the Optional doesn't take a value, format is:
00541             #    -s, --long
00542             if action.nargs == 0:
00543                 parts.extend(action.option_strings)
00544 
00545             # if the Optional takes a value, format is:
00546             #    -s ARGS, --long ARGS
00547             else:
00548                 default = action.dest.upper()
00549                 args_string = self._format_args(action, default)
00550                 for option_string in action.option_strings:
00551                     parts.append('%s %s' % (option_string, args_string))
00552 
00553             return ', '.join(parts)

def argparse::HelpFormatter::_format_action_invocation (   self,
  action 
) [private]

Definition at line 554 of file argparse.py.

00554                                                :
00555         if not action.option_strings:
00556             metavar, = self._metavar_formatter(action, action.dest)(1)
00557             return metavar
00558 
00559         else:
00560             parts = []
00561 
00562             # if the Optional doesn't take a value, format is:
00563             #    -s, --long
00564             if action.nargs == 0:
00565                 parts.extend(action.option_strings)
00566 
00567             # if the Optional takes a value, format is:
00568             #    -s ARGS, --long ARGS
00569             else:
00570                 default = action.dest.upper()
00571                 args_string = self._format_args(action, default)
00572                 for option_string in action.option_strings:
00573                     parts.append('%s %s' % (option_string, args_string))
00574 
00575             return ', '.join(parts)
00576 
def argparse::HelpFormatter::_format_actions_usage (   self,
  actions,
  groups 
) [private]

Definition at line 412 of file argparse.py.

00412                                                     :
00413         # find group indices and identify actions in groups
00414         group_actions = _set()
00415         inserts = {}
00416         for group in groups:
00417             try:
00418                 start = actions.index(group._group_actions[0])
00419             except ValueError:
00420                 continue
00421             else:
00422                 end = start + len(group._group_actions)
00423                 if actions[start:end] == group._group_actions:
00424                     for action in group._group_actions:
00425                         group_actions.add(action)
00426                     if not group.required:
00427                         inserts[start] = '['
00428                         inserts[end] = ']'
00429                     else:
00430                         inserts[start] = '('
00431                         inserts[end] = ')'
00432                     for i in range(start + 1, end):
00433                         inserts[i] = '|'
00434 
00435         # collect all actions format strings
00436         parts = []
00437         for i, action in enumerate(actions):
00438 
00439             # suppressed arguments are marked with None
00440             # remove | separators for suppressed arguments
00441             if action.help is SUPPRESS:
00442                 parts.append(None)
00443                 if inserts.get(i) == '|':
00444                     inserts.pop(i)
00445                 elif inserts.get(i + 1) == '|':
00446                     inserts.pop(i + 1)
00447 
00448             # produce all arg strings
00449             elif not action.option_strings:
00450                 part = self._format_args(action, action.dest)
00451 
00452                 # if it's in a group, strip the outer []
00453                 if action in group_actions:
00454                     if part[0] == '[' and part[-1] == ']':
00455                         part = part[1:-1]
00456 
00457                 # add the action string to the list
00458                 parts.append(part)
00459 
00460             # produce the first way to invoke the option in brackets
00461             else:
00462                 option_string = action.option_strings[0]
00463 
00464                 # if the Optional doesn't take a value, format is:
00465                 #    -s or --long
00466                 if action.nargs == 0:
00467                     part = '%s' % option_string
00468 
00469                 # if the Optional takes a value, format is:
00470                 #    -s ARGS or --long ARGS
00471                 else:
00472                     default = action.dest.upper()
00473                     args_string = self._format_args(action, default)
00474                     part = '%s %s' % (option_string, args_string)
00475 
00476                 # make it look optional if it's not required or in a group
00477                 if not action.required and action not in group_actions:
00478                     part = '[%s]' % part
00479 
00480                 # add the action string to the list
00481                 parts.append(part)
00482 
00483         # insert things at the necessary indices
00484         for i in _sorted(inserts, reverse=True):
00485             parts[i:i] = [inserts[i]]
00486 
00487         # join all the action items with spaces
00488         text = ' '.join([item for item in parts if item is not None])
00489 
00490         # clean up separators for mutually exclusive groups
00491         open = r'[\[(]'
00492         close = r'[\])]'
00493         text = _re.sub(r'(%s) ' % open, r'\1', text)
00494         text = _re.sub(r' (%s)' % close, r'\1', text)
00495         text = _re.sub(r'%s *%s' % (open, close), r'', text)
00496         text = _re.sub(r'\(([^|]*)\)', r'\1', text)
00497         text = text.strip()
00498 
00499         # return the text
00500         return text
00501 
def argparse::HelpFormatter::_format_actions_usage (   self,
  actions,
  groups 
) [private]

Definition at line 381 of file argparse.py.

00382                                                     :
00383         # find group indices and identify actions in groups
00384         group_actions = set()
00385         inserts = {}
00386         for group in groups:
00387             try:
00388                 start = actions.index(group._group_actions[0])
00389             except ValueError:
00390                 continue
00391             else:
00392                 end = start + len(group._group_actions)
00393                 if actions[start:end] == group._group_actions:
00394                     for action in group._group_actions:
00395                         group_actions.add(action)
00396                     if not group.required:
00397                         if start in inserts:
00398                             inserts[start] += ' ['
00399                         else:
00400                             inserts[start] = '['
00401                         inserts[end] = ']'
00402                     else:
00403                         if start in inserts:
00404                             inserts[start] += ' ('
00405                         else:
00406                             inserts[start] = '('
00407                         inserts[end] = ')'
00408                     for i in range(start + 1, end):
00409                         inserts[i] = '|'
00410 
00411         # collect all actions format strings
00412         parts = []
00413         for i, action in enumerate(actions):
00414 
00415             # suppressed arguments are marked with None
00416             # remove | separators for suppressed arguments
00417             if action.help is SUPPRESS:
00418                 parts.append(None)
00419                 if inserts.get(i) == '|':
00420                     inserts.pop(i)
00421                 elif inserts.get(i + 1) == '|':
00422                     inserts.pop(i + 1)
00423 
00424             # produce all arg strings
00425             elif not action.option_strings:
00426                 part = self._format_args(action, action.dest)
00427 
00428                 # if it's in a group, strip the outer []
00429                 if action in group_actions:
00430                     if part[0] == '[' and part[-1] == ']':
00431                         part = part[1:-1]
00432 
00433                 # add the action string to the list
00434                 parts.append(part)
00435 
00436             # produce the first way to invoke the option in brackets
00437             else:
00438                 option_string = action.option_strings[0]
00439 
00440                 # if the Optional doesn't take a value, format is:
00441                 #    -s or --long
00442                 if action.nargs == 0:
00443                     part = '%s' % option_string
00444 
00445                 # if the Optional takes a value, format is:
00446                 #    -s ARGS or --long ARGS
00447                 else:
00448                     default = action.dest.upper()
00449                     args_string = self._format_args(action, default)
00450                     part = '%s %s' % (option_string, args_string)
00451 
00452                 # make it look optional if it's not required or in a group
00453                 if not action.required and action not in group_actions:
00454                     part = '[%s]' % part
00455 
00456                 # add the action string to the list
00457                 parts.append(part)
00458 
00459         # insert things at the necessary indices
00460         for i in sorted(inserts, reverse=True):
00461             parts[i:i] = [inserts[i]]
00462 
00463         # join all the action items with spaces
00464         text = ' '.join([item for item in parts if item is not None])
00465 
00466         # clean up separators for mutually exclusive groups
00467         open = r'[\[(]'
00468         close = r'[\])]'
00469         text = _re.sub(r'(%s) ' % open, r'\1', text)
00470         text = _re.sub(r' (%s)' % close, r'\1', text)
00471         text = _re.sub(r'%s *%s' % (open, close), r'', text)
00472         text = _re.sub(r'\(([^|]*)\)', r'\1', text)
00473         text = text.strip()
00474 
00475         # return the text
00476         return text

def argparse::HelpFormatter::_format_args (   self,
  action,
  default_metavar 
) [private]

Definition at line 570 of file argparse.py.

00571                                                    :
00572         get_metavar = self._metavar_formatter(action, default_metavar)
00573         if action.nargs is None:
00574             result = '%s' % get_metavar(1)
00575         elif action.nargs == OPTIONAL:
00576             result = '[%s]' % get_metavar(1)
00577         elif action.nargs == ZERO_OR_MORE:
00578             result = '[%s [%s ...]]' % get_metavar(2)
00579         elif action.nargs == ONE_OR_MORE:
00580             result = '%s [%s ...]' % get_metavar(2)
00581         elif action.nargs == REMAINDER:
00582             result = '...'
00583         elif action.nargs == PARSER:
00584             result = '%s ...' % get_metavar(1)
00585         else:
00586             formats = ['%s' for _ in range(action.nargs)]
00587             result = ' '.join(formats) % get_metavar(action.nargs)
00588         return result

def argparse::HelpFormatter::_format_args (   self,
  action,
  default_metavar 
) [private]

Definition at line 593 of file argparse.py.

00593                                                    :
00594         get_metavar = self._metavar_formatter(action, default_metavar)
00595         if action.nargs is None:
00596             result = '%s' % get_metavar(1)
00597         elif action.nargs == OPTIONAL:
00598             result = '[%s]' % get_metavar(1)
00599         elif action.nargs == ZERO_OR_MORE:
00600             result = '[%s [%s ...]]' % get_metavar(2)
00601         elif action.nargs == ONE_OR_MORE:
00602             result = '%s [%s ...]' % get_metavar(2)
00603         elif action.nargs is PARSER:
00604             result = '%s ...' % get_metavar(1)
00605         else:
00606             formats = ['%s' for _ in range(action.nargs)]
00607             result = ' '.join(formats) % get_metavar(action.nargs)
00608         return result
00609 
def argparse::HelpFormatter::_format_text (   self,
  text 
) [private]

Definition at line 502 of file argparse.py.

00502                                 :
00503         text_width = self._width - self._current_indent
00504         indent = ' ' * self._current_indent
00505         return self._fill_text(text, text_width, indent) + '\n\n'
00506 
def argparse::HelpFormatter::_format_text (   self,
  text 
) [private]

Definition at line 477 of file argparse.py.

00478                                 :
00479         if '%(prog)' in text:
00480             text = text % dict(prog=self._prog)
00481         text_width = self._width - self._current_indent
00482         indent = ' ' * self._current_indent
00483         return self._fill_text(text, text_width, indent) + '\n\n'

def argparse::HelpFormatter::_format_usage (   self,
  usage,
  actions,
  groups,
  prefix 
) [private]

Definition at line 320 of file argparse.py.

00320                                                            :
00321         if prefix is None:
00322             prefix = _('usage: ')
00323 
00324         # if usage is specified, use that
00325         if usage is not None:
00326             usage = usage % dict(prog=self._prog)
00327 
00328         # if no optionals or positionals are available, usage is just prog
00329         elif usage is None and not actions:
00330             usage = '%(prog)s' % dict(prog=self._prog)
00331 
00332         # if optionals and positionals are available, calculate usage
00333         elif usage is None:
00334             prog = '%(prog)s' % dict(prog=self._prog)
00335 
00336             # split optionals from positionals
00337             optionals = []
00338             positionals = []
00339             for action in actions:
00340                 if action.option_strings:
00341                     optionals.append(action)
00342                 else:
00343                     positionals.append(action)
00344 
00345             # build full usage string
00346             format = self._format_actions_usage
00347             action_usage = format(optionals + positionals, groups)
00348             usage = ' '.join([s for s in [prog, action_usage] if s])
00349 
00350             # wrap the usage parts if it's too long
00351             text_width = self._width - self._current_indent
00352             if len(prefix) + len(usage) > text_width:
00353 
00354                 # break usage into wrappable parts
00355                 part_regexp = r'\(.*?\)+|\[.*?\]+|\S+'
00356                 opt_usage = format(optionals, groups)
00357                 pos_usage = format(positionals, groups)
00358                 opt_parts = _re.findall(part_regexp, opt_usage)
00359                 pos_parts = _re.findall(part_regexp, pos_usage)
00360                 assert ' '.join(opt_parts) == opt_usage
00361                 assert ' '.join(pos_parts) == pos_usage
00362 
00363                 # helper for wrapping lines
00364                 def get_lines(parts, indent, prefix=None):
00365                     lines = []
00366                     line = []
00367                     if prefix is not None:
00368                         line_len = len(prefix) - 1
00369                     else:
00370                         line_len = len(indent) - 1
00371                     for part in parts:
00372                         if line_len + 1 + len(part) > text_width:
00373                             lines.append(indent + ' '.join(line))
00374                             line = []
00375                             line_len = len(indent) - 1
00376                         line.append(part)
00377                         line_len += len(part) + 1
00378                     if line:
00379                         lines.append(indent + ' '.join(line))
00380                     if prefix is not None:
00381                         lines[0] = lines[0][len(indent):]
00382                     return lines
00383 
00384                 # if prog is short, follow it with optionals or positionals
00385                 if len(prefix) + len(prog) <= 0.75 * text_width:
00386                     indent = ' ' * (len(prefix) + len(prog) + 1)
00387                     if opt_parts:
00388                         lines = get_lines([prog] + opt_parts, indent, prefix)
00389                         lines.extend(get_lines(pos_parts, indent))
00390                     elif pos_parts:
00391                         lines = get_lines([prog] + pos_parts, indent, prefix)
00392                     else:
00393                         lines = [prog]
00394 
00395                 # if prog is long, put it on its own line
00396                 else:
00397                     indent = ' ' * len(prefix)
00398                     parts = opt_parts + pos_parts
00399                     lines = get_lines(parts, indent)
00400                     if len(lines) > 1:
00401                         lines = []
00402                         lines.extend(get_lines(opt_parts, indent))
00403                         lines.extend(get_lines(pos_parts, indent))
00404                     lines = [prog] + lines
00405 
00406                 # join lines into usage
00407                 usage = '\n'.join(lines)
00408 
00409         # prefix with 'usage:'
00410         return '%s%s\n\n' % (prefix, usage)
00411 
def argparse::HelpFormatter::_format_usage (   self,
  usage,
  actions,
  groups,
  prefix 
) [private]

Definition at line 289 of file argparse.py.

00290                                                            :
00291         if prefix is None:
00292             prefix = _('usage: ')
00293 
00294         # if usage is specified, use that
00295         if usage is not None:
00296             usage = usage % dict(prog=self._prog)
00297 
00298         # if no optionals or positionals are available, usage is just prog
00299         elif usage is None and not actions:
00300             usage = '%(prog)s' % dict(prog=self._prog)
00301 
00302         # if optionals and positionals are available, calculate usage
00303         elif usage is None:
00304             prog = '%(prog)s' % dict(prog=self._prog)
00305 
00306             # split optionals from positionals
00307             optionals = []
00308             positionals = []
00309             for action in actions:
00310                 if action.option_strings:
00311                     optionals.append(action)
00312                 else:
00313                     positionals.append(action)
00314 
00315             # build full usage string
00316             format = self._format_actions_usage
00317             action_usage = format(optionals + positionals, groups)
00318             usage = ' '.join([s for s in [prog, action_usage] if s])
00319 
00320             # wrap the usage parts if it's too long
00321             text_width = self._width - self._current_indent
00322             if len(prefix) + len(usage) > text_width:
00323 
00324                 # break usage into wrappable parts
00325                 part_regexp = r'\(.*?\)+|\[.*?\]+|\S+'
00326                 opt_usage = format(optionals, groups)
00327                 pos_usage = format(positionals, groups)
00328                 opt_parts = _re.findall(part_regexp, opt_usage)
00329                 pos_parts = _re.findall(part_regexp, pos_usage)
00330                 assert ' '.join(opt_parts) == opt_usage
00331                 assert ' '.join(pos_parts) == pos_usage
00332 
00333                 # helper for wrapping lines
00334                 def get_lines(parts, indent, prefix=None):
00335                     lines = []
00336                     line = []
00337                     if prefix is not None:
00338                         line_len = len(prefix) - 1
00339                     else:
00340                         line_len = len(indent) - 1
00341                     for part in parts:
00342                         if line_len + 1 + len(part) > text_width:
00343                             lines.append(indent + ' '.join(line))
00344                             line = []
00345                             line_len = len(indent) - 1
00346                         line.append(part)
00347                         line_len += len(part) + 1
00348                     if line:
00349                         lines.append(indent + ' '.join(line))
00350                     if prefix is not None:
00351                         lines[0] = lines[0][len(indent):]
00352                     return lines
00353 
00354                 # if prog is short, follow it with optionals or positionals
00355                 if len(prefix) + len(prog) <= 0.75 * text_width:
00356                     indent = ' ' * (len(prefix) + len(prog) + 1)
00357                     if opt_parts:
00358                         lines = get_lines([prog] + opt_parts, indent, prefix)
00359                         lines.extend(get_lines(pos_parts, indent))
00360                     elif pos_parts:
00361                         lines = get_lines([prog] + pos_parts, indent, prefix)
00362                     else:
00363                         lines = [prog]
00364 
00365                 # if prog is long, put it on its own line
00366                 else:
00367                     indent = ' ' * len(prefix)
00368                     parts = opt_parts + pos_parts
00369                     lines = get_lines(parts, indent)
00370                     if len(lines) > 1:
00371                         lines = []
00372                         lines.extend(get_lines(opt_parts, indent))
00373                         lines.extend(get_lines(pos_parts, indent))
00374                     lines = [prog] + lines
00375 
00376                 # join lines into usage
00377                 usage = '\n'.join(lines)
00378 
00379         # prefix with 'usage:'
00380         return '%s%s\n\n' % (prefix, usage)

def argparse::HelpFormatter::_get_help_string (   self,
  action 
) [private]

Reimplemented in argparse::ArgumentDefaultsHelpFormatter, and argparse::ArgumentDefaultsHelpFormatter.

Definition at line 622 of file argparse.py.

00623                                       :
00624         return action.help
00625 

def argparse::HelpFormatter::_get_help_string (   self,
  action 
) [private]

Reimplemented in argparse::ArgumentDefaultsHelpFormatter, and argparse::ArgumentDefaultsHelpFormatter.

Definition at line 640 of file argparse.py.

00640                                       :
00641         return action.help
00642 
00643 
def argparse::HelpFormatter::_indent (   self) [private]

Definition at line 185 of file argparse.py.

00186                      :
00187         self._current_indent += self._indent_increment
00188         self._level += 1

def argparse::HelpFormatter::_indent (   self) [private]

Definition at line 216 of file argparse.py.

00216                      :
00217         self._current_indent += self._indent_increment
00218         self._level += 1
00219 
def argparse::HelpFormatter::_iter_indented_subactions (   self,
  action 
) [private]

Definition at line 620 of file argparse.py.

00620                                                :
00621         try:
00622             get_subactions = action._get_subactions
00623         except AttributeError:
00624             pass
00625         else:
00626             self._indent()
00627             for subaction in get_subactions():
00628                 yield subaction
00629             self._dedent()
00630 
def argparse::HelpFormatter::_iter_indented_subactions (   self,
  action 
) [private]

Definition at line 602 of file argparse.py.

00603                                                :
00604         try:
00605             get_subactions = action._get_subactions
00606         except AttributeError:
00607             pass
00608         else:
00609             self._indent()
00610             for subaction in get_subactions():
00611                 yield subaction
00612             self._dedent()

def argparse::HelpFormatter::_join_parts (   self,
  part_strings 
) [private]

Definition at line 315 of file argparse.py.

00315                                        :
00316         return ''.join([part
00317                         for part in part_strings
00318                         if part and part is not SUPPRESS])
00319 
def argparse::HelpFormatter::_join_parts (   self,
  part_strings 
) [private]

Definition at line 284 of file argparse.py.

00285                                        :
00286         return ''.join([part
00287                         for part in part_strings
00288                         if part and part is not SUPPRESS])

def argparse::HelpFormatter::_metavar_formatter (   self,
  action,
  default_metavar 
) [private]

Definition at line 554 of file argparse.py.

00555                                                          :
00556         if action.metavar is not None:
00557             result = action.metavar
00558         elif action.choices is not None:
00559             choice_strs = [str(choice) for choice in action.choices]
00560             result = '{%s}' % ','.join(choice_strs)
00561         else:
00562             result = default_metavar
00563 
00564         def format(tuple_size):
00565             if isinstance(result, tuple):
00566                 return result
00567             else:
00568                 return (result, ) * tuple_size
00569         return format

def argparse::HelpFormatter::_metavar_formatter (   self,
  action,
  default_metavar 
) [private]

Definition at line 577 of file argparse.py.

00577                                                          :
00578         if action.metavar is not None:
00579             result = action.metavar
00580         elif action.choices is not None:
00581             choice_strs = [str(choice) for choice in action.choices]
00582             result = '{%s}' % ','.join(choice_strs)
00583         else:
00584             result = default_metavar
00585 
00586         def format(tuple_size):
00587             if isinstance(result, tuple):
00588                 return result
00589             else:
00590                 return (result, ) * tuple_size
00591         return format
00592 
def argparse::HelpFormatter::_split_lines (   self,
  text,
  width 
) [private]

Reimplemented in argparse::RawTextHelpFormatter, and argparse::RawTextHelpFormatter.

Definition at line 613 of file argparse.py.

00614                                        :
00615         text = self._whitespace_matcher.sub(' ', text).strip()
00616         return _textwrap.wrap(text, width)

def argparse::HelpFormatter::_split_lines (   self,
  text,
  width 
) [private]

Reimplemented in argparse::RawTextHelpFormatter, and argparse::RawTextHelpFormatter.

Definition at line 631 of file argparse.py.

00631                                        :
00632         text = self._whitespace_matcher.sub(' ', text).strip()
00633         return _textwrap.wrap(text, width)
00634 
def argparse::HelpFormatter::add_argument (   self,
  action 
)

Definition at line 252 of file argparse.py.

00253                                   :
00254         if action.help is not SUPPRESS:
00255 
00256             # find all invocations
00257             get_invocation = self._format_action_invocation
00258             invocations = [get_invocation(action)]
00259             for subaction in self._iter_indented_subactions(action):
00260                 invocations.append(get_invocation(subaction))
00261 
00262             # update the maximum item length
00263             invocation_length = max([len(s) for s in invocations])
00264             action_length = invocation_length + self._current_indent
00265             self._action_max_length = max(self._action_max_length,
00266                                           action_length)
00267 
00268             # add the item to the list
00269             self._add_item(self._format_action, [action])

def argparse::HelpFormatter::add_argument (   self,
  action 
)

Definition at line 283 of file argparse.py.

00283                                   :
00284         if action.help is not SUPPRESS:
00285 
00286             # find all invocations
00287             get_invocation = self._format_action_invocation
00288             invocations = [get_invocation(action)]
00289             for subaction in self._iter_indented_subactions(action):
00290                 invocations.append(get_invocation(subaction))
00291 
00292             # update the maximum item length
00293             invocation_length = max([len(s) for s in invocations])
00294             action_length = invocation_length + self._current_indent
00295             self._action_max_length = max(self._action_max_length,
00296                                           action_length)
00297 
00298             # add the item to the list
00299             self._add_item(self._format_action, [action])
00300 
def argparse::HelpFormatter::add_arguments (   self,
  actions 
)

Definition at line 301 of file argparse.py.

00301                                     :
00302         for action in actions:
00303             self.add_argument(action)
00304 
def argparse::HelpFormatter::add_arguments (   self,
  actions 
)

Definition at line 270 of file argparse.py.

00271                                     :
00272         for action in actions:
00273             self.add_argument(action)

def argparse::HelpFormatter::add_text (   self,
  text 
)

Definition at line 243 of file argparse.py.

00244                             :
00245         if text is not SUPPRESS and text is not None:
00246             self._add_item(self._format_text, [text])

def argparse::HelpFormatter::add_text (   self,
  text 
)

Definition at line 274 of file argparse.py.

00274                             :
00275         if text is not SUPPRESS and text is not None:
00276             self._add_item(self._format_text, [text])
00277 
def argparse::HelpFormatter::add_usage (   self,
  usage,
  actions,
  groups,
  prefix = None 
)

Definition at line 278 of file argparse.py.

00278                                                             :
00279         if usage is not SUPPRESS:
00280             args = usage, actions, groups, prefix
00281             self._add_item(self._format_usage, args)
00282 
def argparse::HelpFormatter::add_usage (   self,
  usage,
  actions,
  groups,
  prefix = None 
)

Definition at line 247 of file argparse.py.

00248                                                             :
00249         if usage is not SUPPRESS:
00250             args = usage, actions, groups, prefix
00251             self._add_item(self._format_usage, args)

def argparse::HelpFormatter::end_section (   self)

Definition at line 270 of file argparse.py.

00270                          :
00271         self._current_section = self._current_section.parent
00272         self._dedent()
00273 
def argparse::HelpFormatter::end_section (   self)

Definition at line 239 of file argparse.py.

00240                          :
00241         self._current_section = self._current_section.parent
00242         self._dedent()

def argparse::HelpFormatter::format_help (   self)

Definition at line 277 of file argparse.py.

00278                          :
00279         help = self._root_section.format_help()
00280         if help:
00281             help = self._long_break_matcher.sub('\n\n', help)
00282             help = help.strip('\n') + '\n'
00283         return help

def argparse::HelpFormatter::format_help (   self)

Definition at line 308 of file argparse.py.

00308                          :
00309         help = self._root_section.format_help()
00310         if help:
00311             help = self._long_break_matcher.sub('\n\n', help)
00312             help = help.strip('\n') + '\n'
00313         return help
00314 
def argparse::HelpFormatter::start_section (   self,
  heading 
)

Definition at line 233 of file argparse.py.

00234                                     :
00235         self._indent()
00236         section = self._Section(self, self._current_section, heading)
00237         self._add_item(section.format_help, [])
00238         self._current_section = section

def argparse::HelpFormatter::start_section (   self,
  heading 
)

Definition at line 264 of file argparse.py.

00264                                     :
00265         self._indent()
00266         section = self._Section(self, self._current_section, heading)
00267         self._add_item(section.format_help, [])
00268         self._current_section = section
00269 

Member Data Documentation

Definition at line 153 of file argparse.py.

Definition at line 153 of file argparse.py.

Definition at line 153 of file argparse.py.

Definition at line 153 of file argparse.py.

Definition at line 153 of file argparse.py.

Definition at line 153 of file argparse.py.

Definition at line 153 of file argparse.py.

Definition at line 153 of file argparse.py.

Definition at line 153 of file argparse.py.

Definition at line 153 of file argparse.py.

Definition at line 153 of file argparse.py.