fancy_getopt.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. """distutils.fancy_getopt
  2. Wrapper around the standard getopt module that provides the following
  3. additional features:
  4. * short and long options are tied together
  5. * options have help strings, so fancy_getopt could potentially
  6. create a complete usage summary
  7. * options set attributes of a passed-in object
  8. """
  9. import sys, string, re
  10. import getopt
  11. from distutils.errors import *
  12. # Much like command_re in distutils.core, this is close to but not quite
  13. # the same as a Python NAME -- except, in the spirit of most GNU
  14. # utilities, we use '-' in place of '_'. (The spirit of LISP lives on!)
  15. # The similarities to NAME are again not a coincidence...
  16. longopt_pat = r'[a-zA-Z](?:[a-zA-Z0-9-]*)'
  17. longopt_re = re.compile(r'^%s$' % longopt_pat)
  18. # For recognizing "negative alias" options, eg. "quiet=!verbose"
  19. neg_alias_re = re.compile("^(%s)=!(%s)$" % (longopt_pat, longopt_pat))
  20. # This is used to translate long options to legitimate Python identifiers
  21. # (for use as attributes of some object).
  22. longopt_xlate = str.maketrans('-', '_')
  23. class FancyGetopt:
  24. """Wrapper around the standard 'getopt()' module that provides some
  25. handy extra functionality:
  26. * short and long options are tied together
  27. * options have help strings, and help text can be assembled
  28. from them
  29. * options set attributes of a passed-in object
  30. * boolean options can have "negative aliases" -- eg. if
  31. --quiet is the "negative alias" of --verbose, then "--quiet"
  32. on the command line sets 'verbose' to false
  33. """
  34. def __init__(self, option_table=None):
  35. # The option table is (currently) a list of tuples. The
  36. # tuples may have 3 or four values:
  37. # (long_option, short_option, help_string [, repeatable])
  38. # if an option takes an argument, its long_option should have '='
  39. # appended; short_option should just be a single character, no ':'
  40. # in any case. If a long_option doesn't have a corresponding
  41. # short_option, short_option should be None. All option tuples
  42. # must have long options.
  43. self.option_table = option_table
  44. # 'option_index' maps long option names to entries in the option
  45. # table (ie. those 3-tuples).
  46. self.option_index = {}
  47. if self.option_table:
  48. self._build_index()
  49. # 'alias' records (duh) alias options; {'foo': 'bar'} means
  50. # --foo is an alias for --bar
  51. self.alias = {}
  52. # 'negative_alias' keeps track of options that are the boolean
  53. # opposite of some other option
  54. self.negative_alias = {}
  55. # These keep track of the information in the option table. We
  56. # don't actually populate these structures until we're ready to
  57. # parse the command-line, since the 'option_table' passed in here
  58. # isn't necessarily the final word.
  59. self.short_opts = []
  60. self.long_opts = []
  61. self.short2long = {}
  62. self.attr_name = {}
  63. self.takes_arg = {}
  64. # And 'option_order' is filled up in 'getopt()'; it records the
  65. # original order of options (and their values) on the command-line,
  66. # but expands short options, converts aliases, etc.
  67. self.option_order = []
  68. def _build_index(self):
  69. self.option_index.clear()
  70. for option in self.option_table:
  71. self.option_index[option[0]] = option
  72. def set_option_table(self, option_table):
  73. self.option_table = option_table
  74. self._build_index()
  75. def add_option(self, long_option, short_option=None, help_string=None):
  76. if long_option in self.option_index:
  77. raise DistutilsGetoptError(
  78. "option conflict: already an option '%s'" % long_option)
  79. else:
  80. option = (long_option, short_option, help_string)
  81. self.option_table.append(option)
  82. self.option_index[long_option] = option
  83. def has_option(self, long_option):
  84. """Return true if the option table for this parser has an
  85. option with long name 'long_option'."""
  86. return long_option in self.option_index
  87. def get_attr_name(self, long_option):
  88. """Translate long option name 'long_option' to the form it
  89. has as an attribute of some object: ie., translate hyphens
  90. to underscores."""
  91. return long_option.translate(longopt_xlate)
  92. def _check_alias_dict(self, aliases, what):
  93. assert isinstance(aliases, dict)
  94. for (alias, opt) in aliases.items():
  95. if alias not in self.option_index:
  96. raise DistutilsGetoptError(("invalid %s '%s': "
  97. "option '%s' not defined") % (what, alias, alias))
  98. if opt not in self.option_index:
  99. raise DistutilsGetoptError(("invalid %s '%s': "
  100. "aliased option '%s' not defined") % (what, alias, opt))
  101. def set_aliases(self, alias):
  102. """Set the aliases for this option parser."""
  103. self._check_alias_dict(alias, "alias")
  104. self.alias = alias
  105. def set_negative_aliases(self, negative_alias):
  106. """Set the negative aliases for this option parser.
  107. 'negative_alias' should be a dictionary mapping option names to
  108. option names, both the key and value must already be defined
  109. in the option table."""
  110. self._check_alias_dict(negative_alias, "negative alias")
  111. self.negative_alias = negative_alias
  112. def _grok_option_table(self):
  113. """Populate the various data structures that keep tabs on the
  114. option table. Called by 'getopt()' before it can do anything
  115. worthwhile.
  116. """
  117. self.long_opts = []
  118. self.short_opts = []
  119. self.short2long.clear()
  120. self.repeat = {}
  121. for option in self.option_table:
  122. if len(option) == 3:
  123. long, short, help = option
  124. repeat = 0
  125. elif len(option) == 4:
  126. long, short, help, repeat = option
  127. else:
  128. # the option table is part of the code, so simply
  129. # assert that it is correct
  130. raise ValueError("invalid option tuple: %r" % (option,))
  131. # Type- and value-check the option names
  132. if not isinstance(long, str) or len(long) < 2:
  133. raise DistutilsGetoptError(("invalid long option '%s': "
  134. "must be a string of length >= 2") % long)
  135. if (not ((short is None) or
  136. (isinstance(short, str) and len(short) == 1))):
  137. raise DistutilsGetoptError("invalid short option '%s': "
  138. "must a single character or None" % short)
  139. self.repeat[long] = repeat
  140. self.long_opts.append(long)
  141. if long[-1] == '=': # option takes an argument?
  142. if short: short = short + ':'
  143. long = long[0:-1]
  144. self.takes_arg[long] = 1
  145. else:
  146. # Is option is a "negative alias" for some other option (eg.
  147. # "quiet" == "!verbose")?
  148. alias_to = self.negative_alias.get(long)
  149. if alias_to is not None:
  150. if self.takes_arg[alias_to]:
  151. raise DistutilsGetoptError(
  152. "invalid negative alias '%s': "
  153. "aliased option '%s' takes a value"
  154. % (long, alias_to))
  155. self.long_opts[-1] = long # XXX redundant?!
  156. self.takes_arg[long] = 0
  157. # If this is an alias option, make sure its "takes arg" flag is
  158. # the same as the option it's aliased to.
  159. alias_to = self.alias.get(long)
  160. if alias_to is not None:
  161. if self.takes_arg[long] != self.takes_arg[alias_to]:
  162. raise DistutilsGetoptError(
  163. "invalid alias '%s': inconsistent with "
  164. "aliased option '%s' (one of them takes a value, "
  165. "the other doesn't"
  166. % (long, alias_to))
  167. # Now enforce some bondage on the long option name, so we can
  168. # later translate it to an attribute name on some object. Have
  169. # to do this a bit late to make sure we've removed any trailing
  170. # '='.
  171. if not longopt_re.match(long):
  172. raise DistutilsGetoptError(
  173. "invalid long option name '%s' "
  174. "(must be letters, numbers, hyphens only" % long)
  175. self.attr_name[long] = self.get_attr_name(long)
  176. if short:
  177. self.short_opts.append(short)
  178. self.short2long[short[0]] = long
  179. def getopt(self, args=None, object=None):
  180. """Parse command-line options in args. Store as attributes on object.
  181. If 'args' is None or not supplied, uses 'sys.argv[1:]'. If
  182. 'object' is None or not supplied, creates a new OptionDummy
  183. object, stores option values there, and returns a tuple (args,
  184. object). If 'object' is supplied, it is modified in place and
  185. 'getopt()' just returns 'args'; in both cases, the returned
  186. 'args' is a modified copy of the passed-in 'args' list, which
  187. is left untouched.
  188. """
  189. if args is None:
  190. args = sys.argv[1:]
  191. if object is None:
  192. object = OptionDummy()
  193. created_object = True
  194. else:
  195. created_object = False
  196. self._grok_option_table()
  197. short_opts = ' '.join(self.short_opts)
  198. try:
  199. opts, args = getopt.getopt(args, short_opts, self.long_opts)
  200. except getopt.error as msg:
  201. raise DistutilsArgError(msg)
  202. for opt, val in opts:
  203. if len(opt) == 2 and opt[0] == '-': # it's a short option
  204. opt = self.short2long[opt[1]]
  205. else:
  206. assert len(opt) > 2 and opt[:2] == '--'
  207. opt = opt[2:]
  208. alias = self.alias.get(opt)
  209. if alias:
  210. opt = alias
  211. if not self.takes_arg[opt]: # boolean option?
  212. assert val == '', "boolean option can't have value"
  213. alias = self.negative_alias.get(opt)
  214. if alias:
  215. opt = alias
  216. val = 0
  217. else:
  218. val = 1
  219. attr = self.attr_name[opt]
  220. # The only repeating option at the moment is 'verbose'.
  221. # It has a negative option -q quiet, which should set verbose = 0.
  222. if val and self.repeat.get(attr) is not None:
  223. val = getattr(object, attr, 0) + 1
  224. setattr(object, attr, val)
  225. self.option_order.append((opt, val))
  226. # for opts
  227. if created_object:
  228. return args, object
  229. else:
  230. return args
  231. def get_option_order(self):
  232. """Returns the list of (option, value) tuples processed by the
  233. previous run of 'getopt()'. Raises RuntimeError if
  234. 'getopt()' hasn't been called yet.
  235. """
  236. if self.option_order is None:
  237. raise RuntimeError("'getopt()' hasn't been called yet")
  238. else:
  239. return self.option_order
  240. def generate_help(self, header=None):
  241. """Generate help text (a list of strings, one per suggested line of
  242. output) from the option table for this FancyGetopt object.
  243. """
  244. # Blithely assume the option table is good: probably wouldn't call
  245. # 'generate_help()' unless you've already called 'getopt()'.
  246. # First pass: determine maximum length of long option names
  247. max_opt = 0
  248. for option in self.option_table:
  249. long = option[0]
  250. short = option[1]
  251. l = len(long)
  252. if long[-1] == '=':
  253. l = l - 1
  254. if short is not None:
  255. l = l + 5 # " (-x)" where short == 'x'
  256. if l > max_opt:
  257. max_opt = l
  258. opt_width = max_opt + 2 + 2 + 2 # room for indent + dashes + gutter
  259. # Typical help block looks like this:
  260. # --foo controls foonabulation
  261. # Help block for longest option looks like this:
  262. # --flimflam set the flim-flam level
  263. # and with wrapped text:
  264. # --flimflam set the flim-flam level (must be between
  265. # 0 and 100, except on Tuesdays)
  266. # Options with short names will have the short name shown (but
  267. # it doesn't contribute to max_opt):
  268. # --foo (-f) controls foonabulation
  269. # If adding the short option would make the left column too wide,
  270. # we push the explanation off to the next line
  271. # --flimflam (-l)
  272. # set the flim-flam level
  273. # Important parameters:
  274. # - 2 spaces before option block start lines
  275. # - 2 dashes for each long option name
  276. # - min. 2 spaces between option and explanation (gutter)
  277. # - 5 characters (incl. space) for short option name
  278. # Now generate lines of help text. (If 80 columns were good enough
  279. # for Jesus, then 78 columns are good enough for me!)
  280. line_width = 78
  281. text_width = line_width - opt_width
  282. big_indent = ' ' * opt_width
  283. if header:
  284. lines = [header]
  285. else:
  286. lines = ['Option summary:']
  287. for option in self.option_table:
  288. long, short, help = option[:3]
  289. text = wrap_text(help, text_width)
  290. if long[-1] == '=':
  291. long = long[0:-1]
  292. # Case 1: no short option at all (makes life easy)
  293. if short is None:
  294. if text:
  295. lines.append(" --%-*s %s" % (max_opt, long, text[0]))
  296. else:
  297. lines.append(" --%-*s " % (max_opt, long))
  298. # Case 2: we have a short option, so we have to include it
  299. # just after the long option
  300. else:
  301. opt_names = "%s (-%s)" % (long, short)
  302. if text:
  303. lines.append(" --%-*s %s" %
  304. (max_opt, opt_names, text[0]))
  305. else:
  306. lines.append(" --%-*s" % opt_names)
  307. for l in text[1:]:
  308. lines.append(big_indent + l)
  309. return lines
  310. def print_help(self, header=None, file=None):
  311. if file is None:
  312. file = sys.stdout
  313. for line in self.generate_help(header):
  314. file.write(line + "\n")
  315. def fancy_getopt(options, negative_opt, object, args):
  316. parser = FancyGetopt(options)
  317. parser.set_negative_aliases(negative_opt)
  318. return parser.getopt(args, object)
  319. WS_TRANS = {ord(_wschar) : ' ' for _wschar in string.whitespace}
  320. def wrap_text(text, width):
  321. """wrap_text(text : string, width : int) -> [string]
  322. Split 'text' into multiple lines of no more than 'width' characters
  323. each, and return the list of strings that results.
  324. """
  325. if text is None:
  326. return []
  327. if len(text) <= width:
  328. return [text]
  329. text = text.expandtabs()
  330. text = text.translate(WS_TRANS)
  331. chunks = re.split(r'( +|-+)', text)
  332. chunks = [ch for ch in chunks if ch] # ' - ' results in empty strings
  333. lines = []
  334. while chunks:
  335. cur_line = [] # list of chunks (to-be-joined)
  336. cur_len = 0 # length of current line
  337. while chunks:
  338. l = len(chunks[0])
  339. if cur_len + l <= width: # can squeeze (at least) this chunk in
  340. cur_line.append(chunks[0])
  341. del chunks[0]
  342. cur_len = cur_len + l
  343. else: # this line is full
  344. # drop last chunk if all space
  345. if cur_line and cur_line[-1][0] == ' ':
  346. del cur_line[-1]
  347. break
  348. if chunks: # any chunks left to process?
  349. # if the current line is still empty, then we had a single
  350. # chunk that's too big too fit on a line -- so we break
  351. # down and break it up at the line width
  352. if cur_len == 0:
  353. cur_line.append(chunks[0][0:width])
  354. chunks[0] = chunks[0][width:]
  355. # all-whitespace chunks at the end of a line can be discarded
  356. # (and we know from the re.split above that if a chunk has
  357. # *any* whitespace, it is *all* whitespace)
  358. if chunks[0][0] == ' ':
  359. del chunks[0]
  360. # and store this line in the list-of-all-lines -- as a single
  361. # string, of course!
  362. lines.append(''.join(cur_line))
  363. return lines
  364. def translate_longopt(opt):
  365. """Convert a long option name to a valid Python identifier by
  366. changing "-" to "_".
  367. """
  368. return opt.translate(longopt_xlate)
  369. class OptionDummy:
  370. """Dummy class just used as a place to hold command-line option
  371. values as instance attributes."""
  372. def __init__(self, options=[]):
  373. """Create a new OptionDummy instance. The attributes listed in
  374. 'options' will be initialized to None."""
  375. for opt in options:
  376. setattr(self, opt, None)
  377. if __name__ == "__main__":
  378. text = """\
  379. Tra-la-la, supercalifragilisticexpialidocious.
  380. How *do* you spell that odd word, anyways?
  381. (Someone ask Mary -- she'll know [or she'll
  382. say, "How should I know?"].)"""
  383. for w in (10, 20, 30, 40):
  384. print("width: %d" % w)
  385. print("\n".join(wrap_text(text, w)))
  386. print()