|
9 | 9 | from pip.locations import default_config_file, default_log_file
|
10 | 10 |
|
11 | 11 |
|
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): |
13 | 81 | """Custom help formatter for use in ConfigOptionParser that updates
|
14 | 82 | the defaults before expanding them, allowing them to show up correctly
|
15 | 83 | in the help listing"""
|
|
0 commit comments