| 1 |
"""distutils.fancy_getopt |
| 2 |
|
| 3 |
Wrapper around the standard getopt module that provides the following |
| 4 |
additional features: |
| 5 |
* short and long options are tied together |
| 6 |
* options have help strings, so fancy_getopt could potentially |
| 7 |
create a complete usage summary |
| 8 |
* options set attributes of a passed-in object |
| 9 |
""" |
| 10 |
|
| 11 |
import sys, string, re |
| 12 |
import getopt |
| 13 |
from distutils.errors import * |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
longopt_pat = r'[a-zA-Z](?:[a-zA-Z0-9-]*)' |
| 20 |
longopt_re = re.compile(r'^%s$' % longopt_pat) |
| 21 |
|
| 22 |
|
| 23 |
neg_alias_re = re.compile("^(%s)=!(%s)$" % (longopt_pat, longopt_pat)) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
longopt_xlate = str.maketrans('-', '_') |
| 28 |
|
| 29 |
class FancyGetopt: |
| 30 |
"""Wrapper around the standard 'getopt()' module that provides some |
| 31 |
handy extra functionality: |
| 32 |
* short and long options are tied together |
| 33 |
* options have help strings, and help text can be assembled |
| 34 |
from them |
| 35 |
* options set attributes of a passed-in object |
| 36 |
* boolean options can have "negative aliases" -- eg. if |
| 37 |
--quiet is the "negative alias" of --verbose, then "--quiet" |
| 38 |
on the command line sets 'verbose' to false |
| 39 |
""" |
| 40 |
|
| 41 |
def __init__(self, option_table=None): |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
self.option_table = option_table |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
self.option_index = {} |
| 55 |
if self.option_table: |
| 56 |
self._build_index() |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
self.alias = {} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
self.negative_alias = {} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
self.short_opts = [] |
| 71 |
self.long_opts = [] |
| 72 |
self.short2long = {} |
| 73 |
self.attr_name = {} |
| 74 |
self.takes_arg = {} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
self.option_order = [] |
| 80 |
|
| 81 |
def _build_index(self): |
| 82 |
self.option_index.clear() |
| 83 |
for option in self.option_table: |
| 84 |
self.option_index[option[0]] = option |
| 85 |
|
| 86 |
def set_option_table(self, option_table): |
| 87 |
self.option_table = option_table |
| 88 |
self._build_index() |
| 89 |
|
| 90 |
def add_option(self, long_option, short_option=None, help_string=None): |
| 91 |
if long_option in self.option_index: |
| 92 |
raise DistutilsGetoptError( |
| 93 |
"option conflict: already an option '%s'" % long_option) |
| 94 |
else: |
| 95 |
option = (long_option, short_option, help_string) |
| 96 |
self.option_table.append(option) |
| 97 |
self.option_index[long_option] = option |
| 98 |
|
| 99 |
def has_option(self, long_option): |
| 100 |
"""Return true if the option table for this parser has an |
| 101 |
option with long name 'long_option'.""" |
| 102 |
return long_option in self.option_index |
| 103 |
|
| 104 |
def get_attr_name(self, long_option): |
| 105 |
"""Translate long option name 'long_option' to the form it |
| 106 |
has as an attribute of some object: ie., translate hyphens |
| 107 |
to underscores.""" |
| 108 |
return long_option.translate(longopt_xlate) |
| 109 |
|
| 110 |
def _check_alias_dict(self, aliases, what): |
| 111 |
assert isinstance(aliases, dict) |
| 112 |
for (alias, opt) in aliases.items(): |
| 113 |
if alias not in self.option_index: |
| 114 |
raise DistutilsGetoptError(("invalid %s '%s': " |
| 115 |
"option '%s' not defined") % (what, alias, alias)) |
| 116 |
if opt not in self.option_index: |
| 117 |
raise DistutilsGetoptError(("invalid %s '%s': " |
| 118 |
"aliased option '%s' not defined") % (what, alias, opt)) |
| 119 |
|
| 120 |
def set_aliases(self, alias): |
| 121 |
"""Set the aliases for this option parser.""" |
| 122 |
self._check_alias_dict(alias, "alias") |
| 123 |
self.alias = alias |
| 124 |
|
| 125 |
def set_negative_aliases(self, negative_alias): |
| 126 |
"""Set the negative aliases for this option parser. |
| 127 |
'negative_alias' should be a dictionary mapping option names to |
| 128 |
option names, both the key and value must already be defined |
| 129 |
in the option table.""" |
| 130 |
self._check_alias_dict(negative_alias, "negative alias") |
| 131 |
self.negative_alias = negative_alias |
| 132 |
|
| 133 |
def _grok_option_table(self): |
| 134 |
"""Populate the various data structures that keep tabs on the |
| 135 |
option table. Called by 'getopt()' before it can do anything |
| 136 |
worthwhile. |
| 137 |
""" |
| 138 |
self.long_opts = [] |
| 139 |
self.short_opts = [] |
| 140 |
self.short2long.clear() |
| 141 |
self.repeat = {} |
| 142 |
|
| 143 |
for option in self.option_table: |
| 144 |
if len(option) == 3: |
| 145 |
long, short, help = option |
| 146 |
repeat = 0 |
| 147 |
elif len(option) == 4: |
| 148 |
long, short, help, repeat = option |
| 149 |
else: |
| 150 |
|
| 151 |
|
| 152 |
raise ValueError("invalid option tuple: %r" % (option,)) |
| 153 |
|
| 154 |
|
| 155 |
if not isinstance(long, str) or len(long) < 2: |
| 156 |
raise DistutilsGetoptError(("invalid long option '%s': " |
| 157 |
"must be a string of length >= 2") % long) |
| 158 |
|
| 159 |
if (not ((short is None) or |
| 160 |
(isinstance(short, str) and len(short) == 1))): |
| 161 |
raise DistutilsGetoptError("invalid short option '%s': " |
| 162 |
"must a single character or None" % short) |
| 163 |
|
| 164 |
self.repeat[long] = repeat |
| 165 |
self.long_opts.append(long) |
| 166 |
|
| 167 |
if long[-1] == '=': |
| 168 |
if short: short = short + ':' |
| 169 |
long = long[0:-1] |
| 170 |
self.takes_arg[long] = 1 |
| 171 |
else: |
| 172 |
|
| 173 |
|
| 174 |
alias_to = self.negative_alias.get(long) |
| 175 |
if alias_to is not None: |
| 176 |
if self.takes_arg[alias_to]: |
| 177 |
raise DistutilsGetoptError( |
| 178 |
"invalid negative alias '%s': " |
| 179 |
"aliased option '%s' takes a value" |
| 180 |
% (long, alias_to)) |
| 181 |
|
| 182 |
self.long_opts[-1] = long |
| 183 |
self.takes_arg[long] = 0 |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
alias_to = self.alias.get(long) |
| 188 |
if alias_to is not None: |
| 189 |
if self.takes_arg[long] != self.takes_arg[alias_to]: |
| 190 |
raise DistutilsGetoptError( |
| 191 |
"invalid alias '%s': inconsistent with " |
| 192 |
"aliased option '%s' (one of them takes a value, " |
| 193 |
"the other doesn't" |
| 194 |
% (long, alias_to)) |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
if not longopt_re.match(long): |
| 201 |
raise DistutilsGetoptError( |
| 202 |
"invalid long option name '%s' " |
| 203 |
"(must be letters, numbers, hyphens only" % long) |
| 204 |
|
| 205 |
self.attr_name[long] = self.get_attr_name(long) |
| 206 |
if short: |
| 207 |
self.short_opts.append(short) |
| 208 |
self.short2long[short[0]] = long |
| 209 |
|
| 210 |
def getopt(self, args=None, object=None): |
| 211 |
"""Parse command-line options in args. Store as attributes on object. |
| 212 |
|
| 213 |
If 'args' is None or not supplied, uses 'sys.argv[1:]'. If |
| 214 |
'object' is None or not supplied, creates a new OptionDummy |
| 215 |
object, stores option values there, and returns a tuple (args, |
| 216 |
object). If 'object' is supplied, it is modified in place and |
| 217 |
'getopt()' just returns 'args'; in both cases, the returned |
| 218 |
'args' is a modified copy of the passed-in 'args' list, which |
| 219 |
is left untouched. |
| 220 |
""" |
| 221 |
if args is None: |
| 222 |
args = sys.argv[1:] |
| 223 |
if object is None: |
| 224 |
object = OptionDummy() |
| 225 |
created_object = True |
| 226 |
else: |
| 227 |
created_object = False |
| 228 |
|
| 229 |
self._grok_option_table() |
| 230 |
|
| 231 |
short_opts = ' '.join(self.short_opts) |
| 232 |
try: |
| 233 |
opts, args = getopt.getopt(args, short_opts, self.long_opts) |
| 234 |
except getopt.error as msg: |
| 235 |
raise DistutilsArgError(msg) |
| 236 |
|
| 237 |
for opt, val in opts: |
| 238 |
if len(opt) == 2 and opt[0] == '-': |
| 239 |
opt = self.short2long[opt[1]] |
| 240 |
else: |
| 241 |
assert len(opt) > 2 and opt[:2] == '--' |
| 242 |
opt = opt[2:] |
| 243 |
|
| 244 |
alias = self.alias.get(opt) |
| 245 |
if alias: |
| 246 |
opt = alias |
| 247 |
|
| 248 |
if not self.takes_arg[opt]: |
| 249 |
assert val == '', "boolean option can't have value" |
| 250 |
alias = self.negative_alias.get(opt) |
| 251 |
if alias: |
| 252 |
opt = alias |
| 253 |
val = 0 |
| 254 |
else: |
| 255 |
val = 1 |
| 256 |
|
| 257 |
attr = self.attr_name[opt] |
| 258 |
|
| 259 |
|
| 260 |
if val and self.repeat.get(attr) is not None: |
| 261 |
val = getattr(object, attr, 0) + 1 |
| 262 |
setattr(object, attr, val) |
| 263 |
self.option_order.append((opt, val)) |
| 264 |
|
| 265 |
|
| 266 |
if created_object: |
| 267 |
return args, object |
| 268 |
else: |
| 269 |
return args |
| 270 |
|
| 271 |
def get_option_order(self): |
| 272 |
"""Returns the list of (option, value) tuples processed by the |
| 273 |
previous run of 'getopt()'. Raises RuntimeError if |
| 274 |
'getopt()' hasn't been called yet. |
| 275 |
""" |
| 276 |
if self.option_order is None: |
| 277 |
raise RuntimeError("'getopt()' hasn't been called yet") |
| 278 |
else: |
| 279 |
return self.option_order |
| 280 |
|
| 281 |
def generate_help(self, header=None): |
| 282 |
"""Generate help text (a list of strings, one per suggested line of |
| 283 |
output) from the option table for this FancyGetopt object. |
| 284 |
""" |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
max_opt = 0 |
| 290 |
for option in self.option_table: |
| 291 |
long = option[0] |
| 292 |
short = option[1] |
| 293 |
l = len(long) |
| 294 |
if long[-1] == '=': |
| 295 |
l = l - 1 |
| 296 |
if short is not None: |
| 297 |
l = l + 5 |
| 298 |
if l > max_opt: |
| 299 |
max_opt = l |
| 300 |
|
| 301 |
opt_width = max_opt + 2 + 2 + 2 |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
line_width = 78 |
| 326 |
text_width = line_width - opt_width |
| 327 |
big_indent = ' ' * opt_width |
| 328 |
if header: |
| 329 |
lines = [header] |
| 330 |
else: |
| 331 |
lines = ['Option summary:'] |
| 332 |
|
| 333 |
for option in self.option_table: |
| 334 |
long, short, help = option[:3] |
| 335 |
text = wrap_text(help, text_width) |
| 336 |
if long[-1] == '=': |
| 337 |
long = long[0:-1] |
| 338 |
|
| 339 |
|
| 340 |
if short is None: |
| 341 |
if text: |
| 342 |
lines.append(" --%-*s %s" % (max_opt, long, text[0])) |
| 343 |
else: |
| 344 |
lines.append(" --%-*s " % (max_opt, long)) |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
else: |
| 349 |
opt_names = "%s (-%s)" % (long, short) |
| 350 |
if text: |
| 351 |
lines.append(" --%-*s %s" % |
| 352 |
(max_opt, opt_names, text[0])) |
| 353 |
else: |
| 354 |
lines.append(" --%-*s" % opt_names) |
| 355 |
|
| 356 |
for l in text[1:]: |
| 357 |
lines.append(big_indent + l) |
| 358 |
return lines |
| 359 |
|
| 360 |
def print_help(self, header=None, file=None): |
| 361 |
if file is None: |
| 362 |
file = sys.stdout |
| 363 |
for line in self.generate_help(header): |
| 364 |
file.write(line + "\n") |
| 365 |
|
| 366 |
|
| 367 |
def fancy_getopt(options, negative_opt, object, args): |
| 368 |
parser = FancyGetopt(options) |
| 369 |
parser.set_negative_aliases(negative_opt) |
| 370 |
return parser.getopt(args, object) |
| 371 |
|
| 372 |
|
| 373 |
WS_TRANS = {ord(_wschar) : ' ' for _wschar in string.whitespace} |
| 374 |
|
| 375 |
def wrap_text(text, width): |
| 376 |
"""wrap_text(text : string, width : int) -> [string] |
| 377 |
|
| 378 |
Split 'text' into multiple lines of no more than 'width' characters |
| 379 |
each, and return the list of strings that results. |
| 380 |
""" |
| 381 |
if text is None: |
| 382 |
return [] |
| 383 |
if len(text) <= width: |
| 384 |
return [text] |
| 385 |
|
| 386 |
text = text.expandtabs() |
| 387 |
text = text.translate(WS_TRANS) |
| 388 |
chunks = re.split(r'( +|-+)', text) |
| 389 |
chunks = [ch for ch in chunks if ch] |
| 390 |
lines = [] |
| 391 |
|
| 392 |
while chunks: |
| 393 |
cur_line = [] |
| 394 |
cur_len = 0 |
| 395 |
|
| 396 |
while chunks: |
| 397 |
l = len(chunks[0]) |
| 398 |
if cur_len + l <= width: |
| 399 |
cur_line.append(chunks[0]) |
| 400 |
del chunks[0] |
| 401 |
cur_len = cur_len + l |
| 402 |
else: |
| 403 |
|
| 404 |
if cur_line and cur_line[-1][0] == ' ': |
| 405 |
del cur_line[-1] |
| 406 |
break |
| 407 |
|
| 408 |
if chunks: |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
if cur_len == 0: |
| 413 |
cur_line.append(chunks[0][0:width]) |
| 414 |
chunks[0] = chunks[0][width:] |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
if chunks[0][0] == ' ': |
| 420 |
del chunks[0] |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
lines.append(''.join(cur_line)) |
| 425 |
|
| 426 |
return lines |
| 427 |
|
| 428 |
|
| 429 |
def translate_longopt(opt): |
| 430 |
"""Convert a long option name to a valid Python identifier by |
| 431 |
changing "-" to "_". |
| 432 |
""" |
| 433 |
return opt.translate(longopt_xlate) |
| 434 |
|
| 435 |
|
| 436 |
class OptionDummy: |
| 437 |
"""Dummy class just used as a place to hold command-line option |
| 438 |
values as instance attributes.""" |
| 439 |
|
| 440 |
def __init__(self, options=[]): |
| 441 |
"""Create a new OptionDummy instance. The attributes listed in |
| 442 |
'options' will be initialized to None.""" |
| 443 |
for opt in options: |
| 444 |
setattr(self, opt, None) |
| 445 |
|
| 446 |
|
| 447 |
if __name__ == "__main__": |
| 448 |
text = """\ |
| 449 |
Tra-la-la, supercalifragilisticexpialidocious. |
| 450 |
How *do* you spell that odd word, anyways? |
| 451 |
(Someone ask Mary -- she'll know [or she'll |
| 452 |
say, "How should I know?"].)""" |
| 453 |
|
| 454 |
for w in (10, 20, 30, 40): |
| 455 |
print("width: %d" % w) |
| 456 |
print("\n".join(wrap_text(text, w))) |
| 457 |
print() |