Use a prettier/less verbose help formatter

* Prevents excessive wrapping (respects $COLUMNS if set)
 * More control over the look of pip's option handling
 * '-d DIR, --download=DIR' becomes '-d, --download <DIR>'
This commit is contained in:
Georgi Valkov 2012-03-02 01:43:13 +02:00
parent 900d95a82f
commit 3534ac56f0
1 changed files with 65 additions and 1 deletions

View File

@ -9,7 +9,71 @@ from pip.backwardcompat import ConfigParser, string_types
from pip.locations import default_config_file, default_log_file
class UpdatingDefaultsHelpFormatter(optparse.IndentedHelpFormatter):
class PipPrettyHelpFormatter(optparse.IndentedHelpFormatter):
""" A prettier/less verbose help formatter for optparse """
def __init__(self, *args, **kw):
kw['max_help_position'] = 23
kw['indent_increment'] = 1
# do as argparse does
try:
kw['width'] = int(os.environ['COLUMNS']) - 2
#except (KeyError, ValueError):
except:
kw['width'] = 78
optparse.IndentedHelpFormatter.__init__(self, *args, **kw)
def format_option_strings(self, option):
#return self._format_option_strings(option, ' %s', ' ')
return self._format_option_strings(option, ' <%s>', ', ')
def _format_option_strings(self, option, mvarfmt=' <%s>', optsep=', '):
""" ('-f', '--format') -> -f%(optsep)s--format mvarfmt % metavar"""
opts = []
if option._short_opts: opts.append(option._short_opts[0])
if option._long_opts: opts.append(option._long_opts[0])
if len(opts) > 1: opts.insert(1, optsep)
if option.takes_value():
metavar = option.metavar or option.dest.lower()
opts.append(mvarfmt % metavar)
return ''.join(opts)
def format_heading(self, heading):
if heading == 'Options': return ''
return heading + ':\n'
def format_usage(self, usage):
# ensure there is only one newline between usage and the first heading
# if there is no description
msg = 'Usage: %s' % usage
if self.parser.description:
msg += '\n'
return msg
# leave full control over description to us
def format_description(self, description):
if description:
return description
else:
return ''
# leave full control over epilog to us
def format_epilog(self, epilog):
if epilog:
return epilog
else:
return ''
class UpdatingDefaultsHelpFormatter(PipPrettyHelpFormatter):
"""Custom help formatter for use in ConfigOptionParser that updates
the defaults before expanding them, allowing them to show up correctly
in the help listing"""