fancy_getopt.py 17 KB

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