(self)
| 89 | self._usagestr = self._gen_usage() |
| 90 | |
| 91 | def _gen_usage(self): |
| 92 | out = [] |
| 93 | lines = self.optspec.strip().split('\n') |
| 94 | lines.reverse() |
| 95 | first_syn = True |
| 96 | while lines: |
| 97 | l = lines.pop() |
| 98 | if l == '--': break |
| 99 | out.append('%s: %s\n' % (first_syn and 'usage' or ' or', l)) |
| 100 | first_syn = False |
| 101 | out.append('\n') |
| 102 | last_was_option = False |
| 103 | while lines: |
| 104 | l = lines.pop() |
| 105 | if l.startswith(' '): |
| 106 | out.append('%s%s\n' % (last_was_option and '\n' or '', |
| 107 | l.lstrip())) |
| 108 | last_was_option = False |
| 109 | elif l: |
| 110 | (flags, extra) = l.split(' ', 1) |
| 111 | extra = extra.strip() |
| 112 | if flags.endswith('='): |
| 113 | flags = flags[:-1] |
| 114 | has_parm = 1 |
| 115 | else: |
| 116 | has_parm = 0 |
| 117 | g = re.search(r'\[([^\]]*)\]$', extra) |
| 118 | if g: |
| 119 | defval = g.group(1) |
| 120 | else: |
| 121 | defval = None |
| 122 | flagl = flags.split(',') |
| 123 | flagl_nice = [] |
| 124 | for _f in flagl: |
| 125 | f,dvi = _remove_negative_kv(_f, _intify(defval)) |
| 126 | self._aliases[f] = _remove_negative_k(flagl[0]) |
| 127 | self._hasparms[f] = has_parm |
| 128 | self._defaults[f] = dvi |
| 129 | if len(f) == 1: |
| 130 | self._shortopts += f + (has_parm and ':' or '') |
| 131 | flagl_nice.append('-' + f) |
| 132 | else: |
| 133 | f_nice = re.sub(r'\W', '_', f) |
| 134 | self._aliases[f_nice] = _remove_negative_k(flagl[0]) |
| 135 | self._longopts.append(f + (has_parm and '=' or '')) |
| 136 | self._longopts.append('no-' + f) |
| 137 | flagl_nice.append('--' + _f) |
| 138 | flags_nice = ', '.join(flagl_nice) |
| 139 | if has_parm: |
| 140 | flags_nice += ' ...' |
| 141 | prefix = ' %-20s ' % flags_nice |
| 142 | argtext = '\n'.join(textwrap.wrap(extra, width=_tty_width(), |
| 143 | initial_indent=prefix, |
| 144 | subsequent_indent=' '*28)) |
| 145 | out.append(argtext + '\n') |
| 146 | last_was_option = True |
| 147 | else: |
| 148 | out.append('\n') |
no test coverage detected