blob: fe9b0d4d94d675910444445cc1dc62f25d76c3f9 [file] [log] [blame]
Greg Ward2689e3d1999-03-22 14:52:19 +00001"""distutils.fancy_getopt
2
3Wrapper around the standard getopt module that provides the following
4additional 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# created 1999/03/03, Greg Ward
12
Greg Ward3ce77fd2000-03-02 01:49:45 +000013__revision__ = "$Id$"
Greg Ward2689e3d1999-03-22 14:52:19 +000014
Greg Ward44f8e4e1999-12-12 16:54:55 +000015import sys, string, re
Greg Ward2689e3d1999-03-22 14:52:19 +000016from types import *
17import getopt
18from 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 Warda564cc31999-10-03 20:48:53 +000024longopt_pat = r'[a-zA-Z](?:[a-zA-Z0-9-]*)'
Greg Ward071ed762000-09-26 02:12:31 +000025longopt_re = re.compile(r'^%s$' % longopt_pat)
Greg Warda564cc31999-10-03 20:48:53 +000026
27# For recognizing "negative alias" options, eg. "quiet=!verbose"
Greg Ward071ed762000-09-26 02:12:31 +000028neg_alias_re = re.compile("^(%s)=!(%s)$" % (longopt_pat, longopt_pat))
Greg Warda564cc31999-10-03 20:48:53 +000029
Greg Ward2689e3d1999-03-22 14:52:19 +000030# This is used to translate long options to legitimate Python identifiers
31# (for use as attributes of some object).
Greg Ward071ed762000-09-26 02:12:31 +000032longopt_xlate = string.maketrans('-', '_')
Greg Ward2689e3d1999-03-22 14:52:19 +000033
Greg Wardffc10d92000-04-21 01:41:54 +000034# This records (option, value) pairs in the order seen on the command line;
35# it's close to what getopt.getopt() returns, but with short options
36# expanded. (Ugh, this module should be OO-ified.)
37_option_order = None
38
39
40class FancyGetopt:
41 """Wrapper around the standard 'getopt()' module that provides some
42 handy extra functionality:
43 * short and long options are tied together
44 * options have help strings, and help text can be assembled
45 from them
46 * options set attributes of a passed-in object
47 * boolean options can have "negative aliases" -- eg. if
48 --quiet is the "negative alias" of --verbose, then "--quiet"
49 on the command line sets 'verbose' to false
50 """
51
52 def __init__ (self, option_table=None):
53
54 # The option table is (currently) a list of 3-tuples:
55 # (long_option, short_option, help_string)
56 # if an option takes an argument, its long_option should have '='
57 # appended; short_option should just be a single character, no ':'
58 # in any case. If a long_option doesn't have a corresponding
59 # short_option, short_option should be None. All option tuples
60 # must have long options.
61 self.option_table = option_table
62
63 # 'option_index' maps long option names to entries in the option
64 # table (ie. those 3-tuples).
65 self.option_index = {}
66 if self.option_table:
Greg Ward283c7452000-04-21 02:09:26 +000067 self._build_index()
Greg Wardffc10d92000-04-21 01:41:54 +000068
Greg Ward1e7b5092000-04-21 04:22:01 +000069 # 'alias' records (duh) alias options; {'foo': 'bar'} means
70 # --foo is an alias for --bar
71 self.alias = {}
72
Greg Wardffc10d92000-04-21 01:41:54 +000073 # 'negative_alias' keeps track of options that are the boolean
74 # opposite of some other option
75 self.negative_alias = {}
Fred Drakeb94b8492001-12-06 20:51:35 +000076
Greg Wardffc10d92000-04-21 01:41:54 +000077 # These keep track of the information in the option table. We
78 # don't actually populate these structures until we're ready to
79 # parse the command-line, since the 'option_table' passed in here
80 # isn't necessarily the final word.
81 self.short_opts = []
82 self.long_opts = []
83 self.short2long = {}
84 self.attr_name = {}
85 self.takes_arg = {}
86
87 # And 'option_order' is filled up in 'getopt()'; it records the
88 # original order of options (and their values) on the command-line,
89 # but expands short options, converts aliases, etc.
90 self.option_order = []
91
92 # __init__ ()
Fred Drakeb94b8492001-12-06 20:51:35 +000093
Greg Wardffc10d92000-04-21 01:41:54 +000094
Greg Ward283c7452000-04-21 02:09:26 +000095 def _build_index (self):
Greg Ward0fd2dd62000-08-07 00:45:51 +000096 self.option_index.clear()
Greg Wardffc10d92000-04-21 01:41:54 +000097 for option in self.option_table:
98 self.option_index[option[0]] = option
99
Greg Ward283c7452000-04-21 02:09:26 +0000100 def set_option_table (self, option_table):
101 self.option_table = option_table
102 self._build_index()
103
Greg Wardffc10d92000-04-21 01:41:54 +0000104 def add_option (self, long_option, short_option=None, help_string=None):
105 if self.option_index.has_key(long_option):
106 raise DistutilsGetoptError, \
107 "option conflict: already an option '%s'" % long_option
108 else:
109 option = (long_option, short_option, help_string)
Greg Ward071ed762000-09-26 02:12:31 +0000110 self.option_table.append(option)
Greg Wardffc10d92000-04-21 01:41:54 +0000111 self.option_index[long_option] = option
112
Greg Ward320df702000-04-21 02:31:07 +0000113
114 def has_option (self, long_option):
115 """Return true if the option table for this parser has an
116 option with long name 'long_option'."""
117 return self.option_index.has_key(long_option)
118
119 def get_attr_name (self, long_option):
Fred Drakeb94b8492001-12-06 20:51:35 +0000120 """Translate long option name 'long_option' to the form it
Greg Ward320df702000-04-21 02:31:07 +0000121 has as an attribute of some object: ie., translate hyphens
122 to underscores."""
Greg Ward071ed762000-09-26 02:12:31 +0000123 return string.translate(long_option, longopt_xlate)
Greg Ward320df702000-04-21 02:31:07 +0000124
125
Greg Ward1e7b5092000-04-21 04:22:01 +0000126 def _check_alias_dict (self, aliases, what):
127 assert type(aliases) is DictionaryType
128 for (alias, opt) in aliases.items():
129 if not self.option_index.has_key(alias):
130 raise DistutilsGetoptError, \
131 ("invalid %s '%s': "
132 "option '%s' not defined") % (what, alias, alias)
133 if not self.option_index.has_key(opt):
134 raise DistutilsGetoptError, \
135 ("invalid %s '%s': "
136 "aliased option '%s' not defined") % (what, alias, opt)
Fred Drakeb94b8492001-12-06 20:51:35 +0000137
Greg Ward1e7b5092000-04-21 04:22:01 +0000138 def set_aliases (self, alias):
139 """Set the aliases for this option parser."""
Greg Ward071ed762000-09-26 02:12:31 +0000140 self._check_alias_dict(alias, "alias")
Greg Ward1e7b5092000-04-21 04:22:01 +0000141 self.alias = alias
142
Greg Wardffc10d92000-04-21 01:41:54 +0000143 def set_negative_aliases (self, negative_alias):
144 """Set the negative aliases for this option parser.
145 'negative_alias' should be a dictionary mapping option names to
146 option names, both the key and value must already be defined
147 in the option table."""
Greg Ward071ed762000-09-26 02:12:31 +0000148 self._check_alias_dict(negative_alias, "negative alias")
Greg Wardffc10d92000-04-21 01:41:54 +0000149 self.negative_alias = negative_alias
150
151
152 def _grok_option_table (self):
Greg Ward071ed762000-09-26 02:12:31 +0000153 """Populate the various data structures that keep tabs on the
154 option table. Called by 'getopt()' before it can do anything
155 worthwhile.
156 """
Greg Ward0fd2dd62000-08-07 00:45:51 +0000157 self.long_opts = []
158 self.short_opts = []
159 self.short2long.clear()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000160 self.repeat = {}
Greg Ward0fd2dd62000-08-07 00:45:51 +0000161
Greg Wardffc10d92000-04-21 01:41:54 +0000162 for option in self.option_table:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000163 if len(option) == 3:
164 long, short, help = option
165 repeat = 0
166 elif len(option) == 4:
167 long, short, help, repeat = option
168 else:
169 # the option table is part of the code, so simply
170 # assert that it is correct
171 assert "invalid option tuple: %s" % `option`
Greg Wardffc10d92000-04-21 01:41:54 +0000172
173 # Type- and value-check the option names
174 if type(long) is not StringType or len(long) < 2:
175 raise DistutilsGetoptError, \
176 ("invalid long option '%s': "
177 "must be a string of length >= 2") % long
178
179 if (not ((short is None) or
Greg Ward071ed762000-09-26 02:12:31 +0000180 (type(short) is StringType and len(short) == 1))):
Greg Wardffc10d92000-04-21 01:41:54 +0000181 raise DistutilsGetoptError, \
182 ("invalid short option '%s': "
183 "must a single character or None") % short
184
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000185 self.repeat[long] = 1
Greg Ward071ed762000-09-26 02:12:31 +0000186 self.long_opts.append(long)
Greg Wardffc10d92000-04-21 01:41:54 +0000187
188 if long[-1] == '=': # option takes an argument?
189 if short: short = short + ':'
190 long = long[0:-1]
191 self.takes_arg[long] = 1
192 else:
193
194 # Is option is a "negative alias" for some other option (eg.
195 # "quiet" == "!verbose")?
196 alias_to = self.negative_alias.get(long)
197 if alias_to is not None:
198 if self.takes_arg[alias_to]:
199 raise DistutilsGetoptError, \
200 ("invalid negative alias '%s': "
201 "aliased option '%s' takes a value") % \
202 (long, alias_to)
203
204 self.long_opts[-1] = long # XXX redundant?!
205 self.takes_arg[long] = 0
206
207 else:
208 self.takes_arg[long] = 0
209
Greg Ward1e7b5092000-04-21 04:22:01 +0000210 # If this is an alias option, make sure its "takes arg" flag is
211 # the same as the option it's aliased to.
212 alias_to = self.alias.get(long)
213 if alias_to is not None:
214 if self.takes_arg[long] != self.takes_arg[alias_to]:
215 raise DistutilsGetoptError, \
216 ("invalid alias '%s': inconsistent with "
217 "aliased option '%s' (one of them takes a value, "
218 "the other doesn't") % (long, alias_to)
219
Greg Wardffc10d92000-04-21 01:41:54 +0000220
221 # Now enforce some bondage on the long option name, so we can
222 # later translate it to an attribute name on some object. Have
223 # to do this a bit late to make sure we've removed any trailing
224 # '='.
Greg Ward071ed762000-09-26 02:12:31 +0000225 if not longopt_re.match(long):
Greg Wardffc10d92000-04-21 01:41:54 +0000226 raise DistutilsGetoptError, \
227 ("invalid long option name '%s' " +
228 "(must be letters, numbers, hyphens only") % long
229
Greg Ward071ed762000-09-26 02:12:31 +0000230 self.attr_name[long] = self.get_attr_name(long)
Greg Wardffc10d92000-04-21 01:41:54 +0000231 if short:
Greg Ward071ed762000-09-26 02:12:31 +0000232 self.short_opts.append(short)
Greg Wardffc10d92000-04-21 01:41:54 +0000233 self.short2long[short[0]] = long
234
235 # for option_table
236
237 # _grok_option_table()
238
239
240 def getopt (self, args=None, object=None):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000241 """Parse command-line options in args. Store as attributes on object.
242
243 If 'args' is None or not supplied, uses 'sys.argv[1:]'. If
244 'object' is None or not supplied, creates a new OptionDummy
245 object, stores option values there, and returns a tuple (args,
246 object). If 'object' is supplied, it is modified in place and
247 'getopt()' just returns 'args'; in both cases, the returned
248 'args' is a modified copy of the passed-in 'args' list, which
249 is left untouched.
Greg Ward071ed762000-09-26 02:12:31 +0000250 """
Greg Wardffc10d92000-04-21 01:41:54 +0000251 if args is None:
252 args = sys.argv[1:]
253 if object is None:
Greg Ward981f7362000-05-23 03:53:10 +0000254 object = OptionDummy()
Greg Wardffc10d92000-04-21 01:41:54 +0000255 created_object = 1
256 else:
257 created_object = 0
258
259 self._grok_option_table()
260
Greg Ward071ed762000-09-26 02:12:31 +0000261 short_opts = string.join(self.short_opts)
Greg Wardffc10d92000-04-21 01:41:54 +0000262 try:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000263 opts, args = getopt.getopt(args, short_opts, self.long_opts)
Greg Wardffc10d92000-04-21 01:41:54 +0000264 except getopt.error, msg:
265 raise DistutilsArgError, msg
266
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000267 for opt, val in opts:
Greg Ward071ed762000-09-26 02:12:31 +0000268 if len(opt) == 2 and opt[0] == '-': # it's a short option
Greg Wardffc10d92000-04-21 01:41:54 +0000269 opt = self.short2long[opt[1]]
Greg Wardffc10d92000-04-21 01:41:54 +0000270 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000271 assert len(opt) > 2 and opt[:2] == '--'
272 opt = opt[2:]
Greg Wardffc10d92000-04-21 01:41:54 +0000273
Greg Ward1e7b5092000-04-21 04:22:01 +0000274 alias = self.alias.get(opt)
275 if alias:
276 opt = alias
277
Greg Wardffc10d92000-04-21 01:41:54 +0000278 if not self.takes_arg[opt]: # boolean option?
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000279 assert val == '', "boolean option can't have value"
Greg Ward071ed762000-09-26 02:12:31 +0000280 alias = self.negative_alias.get(opt)
Greg Wardffc10d92000-04-21 01:41:54 +0000281 if alias:
282 opt = alias
283 val = 0
284 else:
285 val = 1
286
287 attr = self.attr_name[opt]
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000288 # The only repeating option at the moment is 'verbose'.
289 # It has a negative option -q quiet, which should set verbose = 0.
290 if val and self.repeat.get(attr) is not None:
291 val = getattr(object, attr, 0) + 1
Greg Ward071ed762000-09-26 02:12:31 +0000292 setattr(object, attr, val)
293 self.option_order.append((opt, val))
Greg Wardffc10d92000-04-21 01:41:54 +0000294
295 # for opts
Greg Wardffc10d92000-04-21 01:41:54 +0000296 if created_object:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000297 return args, object
Greg Wardffc10d92000-04-21 01:41:54 +0000298 else:
299 return args
300
301 # getopt()
302
303
Greg Ward283c7452000-04-21 02:09:26 +0000304 def get_option_order (self):
Greg Wardffc10d92000-04-21 01:41:54 +0000305 """Returns the list of (option, value) tuples processed by the
Greg Ward283c7452000-04-21 02:09:26 +0000306 previous run of 'getopt()'. Raises RuntimeError if
Greg Ward071ed762000-09-26 02:12:31 +0000307 'getopt()' hasn't been called yet.
308 """
Greg Wardffc10d92000-04-21 01:41:54 +0000309 if self.option_order is None:
Greg Ward283c7452000-04-21 02:09:26 +0000310 raise RuntimeError, "'getopt()' hasn't been called yet"
Greg Wardffc10d92000-04-21 01:41:54 +0000311 else:
312 return self.option_order
313
Greg Ward283c7452000-04-21 02:09:26 +0000314
Greg Ward66bf4462000-04-23 02:50:45 +0000315 def generate_help (self, header=None):
Greg Ward283c7452000-04-21 02:09:26 +0000316 """Generate help text (a list of strings, one per suggested line of
Greg Ward071ed762000-09-26 02:12:31 +0000317 output) from the option table for this FancyGetopt object.
318 """
Greg Ward283c7452000-04-21 02:09:26 +0000319 # Blithely assume the option table is good: probably wouldn't call
320 # 'generate_help()' unless you've already called 'getopt()'.
321
322 # First pass: determine maximum length of long option names
323 max_opt = 0
324 for option in self.option_table:
325 long = option[0]
326 short = option[1]
Greg Ward071ed762000-09-26 02:12:31 +0000327 l = len(long)
Greg Ward283c7452000-04-21 02:09:26 +0000328 if long[-1] == '=':
329 l = l - 1
330 if short is not None:
331 l = l + 5 # " (-x)" where short == 'x'
332 if l > max_opt:
333 max_opt = l
334
335 opt_width = max_opt + 2 + 2 + 2 # room for indent + dashes + gutter
336
337 # Typical help block looks like this:
338 # --foo controls foonabulation
339 # Help block for longest option looks like this:
340 # --flimflam set the flim-flam level
341 # and with wrapped text:
342 # --flimflam set the flim-flam level (must be between
343 # 0 and 100, except on Tuesdays)
344 # Options with short names will have the short name shown (but
345 # it doesn't contribute to max_opt):
346 # --foo (-f) controls foonabulation
347 # If adding the short option would make the left column too wide,
348 # we push the explanation off to the next line
349 # --flimflam (-l)
350 # set the flim-flam level
351 # Important parameters:
352 # - 2 spaces before option block start lines
353 # - 2 dashes for each long option name
354 # - min. 2 spaces between option and explanation (gutter)
355 # - 5 characters (incl. space) for short option name
356
357 # Now generate lines of help text. (If 80 columns were good enough
358 # for Jesus, then 78 columns are good enough for me!)
359 line_width = 78
360 text_width = line_width - opt_width
361 big_indent = ' ' * opt_width
362 if header:
363 lines = [header]
364 else:
365 lines = ['Option summary:']
366
367 for (long,short,help) in self.option_table:
368
Greg Ward071ed762000-09-26 02:12:31 +0000369 text = wrap_text(help, text_width)
Greg Ward283c7452000-04-21 02:09:26 +0000370 if long[-1] == '=':
371 long = long[0:-1]
372
373 # Case 1: no short option at all (makes life easy)
374 if short is None:
375 if text:
Greg Ward071ed762000-09-26 02:12:31 +0000376 lines.append(" --%-*s %s" % (max_opt, long, text[0]))
Greg Ward283c7452000-04-21 02:09:26 +0000377 else:
Greg Ward071ed762000-09-26 02:12:31 +0000378 lines.append(" --%-*s " % (max_opt, long))
Greg Ward283c7452000-04-21 02:09:26 +0000379
Greg Ward283c7452000-04-21 02:09:26 +0000380 # Case 2: we have a short option, so we have to include it
381 # just after the long option
382 else:
383 opt_names = "%s (-%s)" % (long, short)
384 if text:
Greg Ward071ed762000-09-26 02:12:31 +0000385 lines.append(" --%-*s %s" %
386 (max_opt, opt_names, text[0]))
Greg Ward283c7452000-04-21 02:09:26 +0000387 else:
Greg Ward071ed762000-09-26 02:12:31 +0000388 lines.append(" --%-*s" % opt_names)
Greg Ward283c7452000-04-21 02:09:26 +0000389
Greg Ward373dbfa2000-06-08 00:35:33 +0000390 for l in text[1:]:
Greg Ward071ed762000-09-26 02:12:31 +0000391 lines.append(big_indent + l)
Greg Ward373dbfa2000-06-08 00:35:33 +0000392
Greg Ward283c7452000-04-21 02:09:26 +0000393 # for self.option_table
394
395 return lines
396
397 # generate_help ()
398
Greg Ward66bf4462000-04-23 02:50:45 +0000399 def print_help (self, header=None, file=None):
Greg Ward283c7452000-04-21 02:09:26 +0000400 if file is None:
401 file = sys.stdout
Greg Ward071ed762000-09-26 02:12:31 +0000402 for line in self.generate_help(header):
403 file.write(line + "\n")
Greg Ward283c7452000-04-21 02:09:26 +0000404
Greg Wardffc10d92000-04-21 01:41:54 +0000405# class FancyGetopt
406
Greg Ward2689e3d1999-03-22 14:52:19 +0000407
Greg Ward44f8e4e1999-12-12 16:54:55 +0000408def fancy_getopt (options, negative_opt, object, args):
Greg Ward071ed762000-09-26 02:12:31 +0000409 parser = FancyGetopt(options)
410 parser.set_negative_aliases(negative_opt)
411 return parser.getopt(args, object)
Greg Wardffc10d92000-04-21 01:41:54 +0000412
413
Greg Ward071ed762000-09-26 02:12:31 +0000414WS_TRANS = string.maketrans(string.whitespace, ' ' * len(string.whitespace))
Greg Ward44f8e4e1999-12-12 16:54:55 +0000415
416def wrap_text (text, width):
Greg Ward46a69b92000-08-30 17:16:27 +0000417 """wrap_text(text : string, width : int) -> [string]
418
419 Split 'text' into multiple lines of no more than 'width' characters
420 each, and return the list of strings that results.
421 """
Greg Ward44f8e4e1999-12-12 16:54:55 +0000422
423 if text is None:
424 return []
Greg Ward071ed762000-09-26 02:12:31 +0000425 if len(text) <= width:
Greg Ward44f8e4e1999-12-12 16:54:55 +0000426 return [text]
427
Greg Ward071ed762000-09-26 02:12:31 +0000428 text = string.expandtabs(text)
429 text = string.translate(text, WS_TRANS)
430 chunks = re.split(r'( +|-+)', text)
431 chunks = filter(None, chunks) # ' - ' results in empty strings
Greg Ward44f8e4e1999-12-12 16:54:55 +0000432 lines = []
433
434 while chunks:
435
436 cur_line = [] # list of chunks (to-be-joined)
437 cur_len = 0 # length of current line
438
439 while chunks:
Greg Ward071ed762000-09-26 02:12:31 +0000440 l = len(chunks[0])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000441 if cur_len + l <= width: # can squeeze (at least) this chunk in
Greg Ward071ed762000-09-26 02:12:31 +0000442 cur_line.append(chunks[0])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000443 del chunks[0]
444 cur_len = cur_len + l
445 else: # this line is full
446 # drop last chunk if all space
447 if cur_line and cur_line[-1][0] == ' ':
448 del cur_line[-1]
449 break
450
451 if chunks: # any chunks left to process?
452
453 # if the current line is still empty, then we had a single
454 # chunk that's too big too fit on a line -- so we break
455 # down and break it up at the line width
456 if cur_len == 0:
Greg Ward071ed762000-09-26 02:12:31 +0000457 cur_line.append(chunks[0][0:width])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000458 chunks[0] = chunks[0][width:]
459
460 # all-whitespace chunks at the end of a line can be discarded
461 # (and we know from the re.split above that if a chunk has
462 # *any* whitespace, it is *all* whitespace)
463 if chunks[0][0] == ' ':
464 del chunks[0]
465
466 # and store this line in the list-of-all-lines -- as a single
467 # string, of course!
Greg Ward071ed762000-09-26 02:12:31 +0000468 lines.append(string.join(cur_line, ''))
Greg Ward44f8e4e1999-12-12 16:54:55 +0000469
470 # while chunks
471
472 return lines
473
474# wrap_text ()
Greg Ward68ded6e2000-09-25 01:58:31 +0000475
476
477def translate_longopt (opt):
478 """Convert a long option name to a valid Python identifier by
479 changing "-" to "_".
480 """
481 return string.translate(opt, longopt_xlate)
Fred Drakeb94b8492001-12-06 20:51:35 +0000482
Greg Ward44f8e4e1999-12-12 16:54:55 +0000483
Greg Wardffc10d92000-04-21 01:41:54 +0000484class OptionDummy:
485 """Dummy class just used as a place to hold command-line option
486 values as instance attributes."""
Greg Ward3c67b1d2000-05-23 01:44:20 +0000487
488 def __init__ (self, options=[]):
489 """Create a new OptionDummy instance. The attributes listed in
490 'options' will be initialized to None."""
491 for opt in options:
492 setattr(self, opt, None)
493
494# class OptionDummy
Fred Drakeb94b8492001-12-06 20:51:35 +0000495
Greg Ward44f8e4e1999-12-12 16:54:55 +0000496
497if __name__ == "__main__":
498 text = """\
499Tra-la-la, supercalifragilisticexpialidocious.
500How *do* you spell that odd word, anyways?
501(Someone ask Mary -- she'll know [or she'll
502say, "How should I know?"].)"""
503
504 for w in (10, 20, 30, 40):
505 print "width: %d" % w
Greg Ward071ed762000-09-26 02:12:31 +0000506 print string.join(wrap_text(text, w), "\n")
Greg Ward44f8e4e1999-12-12 16:54:55 +0000507 print