Skip to content

Commit 3534ac5

Browse files
committed
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>'
1 parent 900d95a commit 3534ac5

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

pip/baseparser.py

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,71 @@
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 (KeyError, ValueError):
23+
except:
24+
kw['width'] = 78
25+
26+
optparse.IndentedHelpFormatter.__init__(self, *args, **kw)
27+
28+
def format_option_strings(self, option):
29+
#return self._format_option_strings(option, ' %s', ' ')
30+
return self._format_option_strings(option, ' <%s>', ', ')
31+
32+
def _format_option_strings(self, option, mvarfmt=' <%s>', optsep=', '):
33+
""" ('-f', '--format') -> -f%(optsep)s--format mvarfmt % metavar"""
34+
35+
opts = []
36+
37+
if option._short_opts: opts.append(option._short_opts[0])
38+
if option._long_opts: opts.append(option._long_opts[0])
39+
if len(opts) > 1: opts.insert(1, optsep)
40+
41+
if option.takes_value():
42+
metavar = option.metavar or option.dest.lower()
43+
opts.append(mvarfmt % metavar)
44+
45+
return ''.join(opts)
46+
47+
def format_heading(self, heading):
48+
if heading == 'Options': return ''
49+
return heading + ':\n'
50+
51+
def format_usage(self, usage):
52+
# ensure there is only one newline between usage and the first heading
53+
# if there is no description
54+
55+
msg = 'Usage: %s' % usage
56+
if self.parser.description:
57+
msg += '\n'
58+
59+
return msg
60+
61+
# leave full control over description to us
62+
def format_description(self, description):
63+
if description:
64+
return description
65+
else:
66+
return ''
67+
68+
# leave full control over epilog to us
69+
def format_epilog(self, epilog):
70+
if epilog:
71+
return epilog
72+
else:
73+
return ''
74+
75+
76+
class UpdatingDefaultsHelpFormatter(PipPrettyHelpFormatter):
1377
"""Custom help formatter for use in ConfigOptionParser that updates
1478
the defaults before expanding them, allowing them to show up correctly
1579
in the help listing"""

0 commit comments

Comments
 (0)