Skip to content

Commit b5e7cde

Browse files
committed
Merge pull request #465 from gvalkov/compact-optformat
Use a prettier/less verbose help formatter
2 parents cecd4cc + 0371ddb commit b5e7cde

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

pip/baseparser.py

+69-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,75 @@
99
from pip.locations import default_config_file, default_log_file
1010

1111

12-
class UpdatingDefaultsHelpFormatter(optparse.IndentedHelpFormatter):
12+
class PipPrettyHelpFormatter(optparse.IndentedHelpFormatter):
13+
"""A prettier/less verbose help formatter for optparse."""
14+
15+
def __init__(self, *args, **kw):
16+
kw['max_help_position'] = 23
17+
kw['indent_increment'] = 1
18+
19+
# do as argparse does
20+
try:
21+
kw['width'] = int(os.environ['COLUMNS']) - 2
22+
except:
23+
kw['width'] = 78
24+
25+
optparse.IndentedHelpFormatter.__init__(self, *args, **kw)
26+
27+
def format_option_strings(self, option):
28+
return self._format_option_strings(option, ' <%s>', ', ')
29+
30+
def _format_option_strings(self, option, mvarfmt=' <%s>', optsep=', '):
31+
"""
32+
Return a comma-separated list of option strings and metavars.
33+
34+
:param option: tuple of (short opt, long opt), e.g: ('-f', '--format')
35+
:param mvarfmt: metavar format string - evaluated as mvarfmt % metavar
36+
:param optsep: separator
37+
"""
38+
39+
opts = []
40+
41+
if option._short_opts: opts.append(option._short_opts[0])
42+
if option._long_opts: opts.append(option._long_opts[0])
43+
if len(opts) > 1: opts.insert(1, optsep)
44+
45+
if option.takes_value():
46+
metavar = option.metavar or option.dest.lower()
47+
opts.append(mvarfmt % metavar)
48+
49+
return ''.join(opts)
50+
51+
def format_heading(self, heading):
52+
if heading == 'Options': return ''
53+
return heading + ':\n'
54+
55+
def format_usage(self, usage):
56+
# ensure there is only one newline between usage and the first heading
57+
# if there is no description
58+
59+
msg = 'Usage: %s' % usage
60+
if self.parser.description:
61+
msg += '\n'
62+
63+
return msg
64+
65+
def format_description(self, description):
66+
# leave full control over description to us
67+
if description:
68+
return description
69+
else:
70+
return ''
71+
72+
def format_epilog(self, epilog):
73+
# leave full control over epilog to us
74+
if epilog:
75+
return epilog
76+
else:
77+
return ''
78+
79+
80+
class UpdatingDefaultsHelpFormatter(PipPrettyHelpFormatter):
1381
"""Custom help formatter for use in ConfigOptionParser that updates
1482
the defaults before expanding them, allowing them to show up correctly
1583
in the help listing"""

0 commit comments

Comments
 (0)