Skip to content

Use a prettier/less verbose help formatter #465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 14, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 69 additions & 1 deletion pip/baseparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,75 @@
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:
kw['width'] = 78

optparse.IndentedHelpFormatter.__init__(self, *args, **kw)

def format_option_strings(self, option):
return self._format_option_strings(option, ' <%s>', ', ')

def _format_option_strings(self, option, mvarfmt=' <%s>', optsep=', '):
"""
Return a comma-separated list of option strings and metavars.

:param option: tuple of (short opt, long opt), e.g: ('-f', '--format')
:param mvarfmt: metavar format string - evaluated as mvarfmt % metavar
:param optsep: separator
"""

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

def format_description(self, description):
# leave full control over description to us
if description:
return description
else:
return ''

def format_epilog(self, epilog):
# leave full control over epilog to us
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"""
Expand Down