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