blob: 5de64e15da64cd3c434fea7d4c55caf01962a55a [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 +000034class 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
48 # The option table is (currently) a list of 3-tuples:
49 # (long_option, short_option, help_string)
50 # if an option takes an argument, its long_option should have '='
51 # appended; short_option should just be a single character, no ':'
52 # in any case. If a long_option doesn't have a corresponding
53 # short_option, short_option should be None. All option tuples
54 # must have long options.
55 self.option_table = option_table
56
57 # 'option_index' maps long option names to entries in the option
58 # table (ie. those 3-tuples).
59 self.option_index = {}
60 if self.option_table:
Greg Ward283c7452000-04-21 02:09:26 +000061 self._build_index()
Greg Wardffc10d92000-04-21 01:41:54 +000062
Greg Ward1e7b5092000-04-21 04:22:01 +000063 # 'alias' records (duh) alias options; {'foo': 'bar'} means
64 # --foo is an alias for --bar
65 self.alias = {}
66
Greg Wardffc10d92000-04-21 01:41:54 +000067 # 'negative_alias' keeps track of options that are the boolean
68 # opposite of some other option
69 self.negative_alias = {}
Fred Drakeb94b8492001-12-06 20:51:35 +000070
Greg Wardffc10d92000-04-21 01:41:54 +000071 # These keep track of the information in the option table. We
72 # don't actually populate these structures until we're ready to
73 # parse the command-line, since the 'option_table' passed in here
74 # isn't necessarily the final word.
75 self.short_opts = []
76 self.long_opts = []
77 self.short2long = {}
78 self.attr_name = {}
79 self.takes_arg = {}
80
81 # And 'option_order' is filled up in 'getopt()'; it records the
82 # original order of options (and their values) on the command-line,
83 # but expands short options, converts aliases, etc.
84 self.option_order = []
85
86 # __init__ ()
Fred Drakeb94b8492001-12-06 20:51:35 +000087
Greg Wardffc10d92000-04-21 01:41:54 +000088
Greg Ward283c7452000-04-21 02:09:26 +000089 def _build_index (self):
Greg Ward0fd2dd62000-08-07 00:45:51 +000090 self.option_index.clear()
Greg Wardffc10d92000-04-21 01:41:54 +000091 for option in self.option_table:
92 self.option_index[option[0]] = option
93
Greg Ward283c7452000-04-21 02:09:26 +000094 def set_option_table (self, option_table):
95 self.option_table = option_table
96 self._build_index()
97
Greg Wardffc10d92000-04-21 01:41:54 +000098 def add_option (self, long_option, short_option=None, help_string=None):
99 if self.option_index.has_key(long_option):
100 raise DistutilsGetoptError, \
101 "option conflict: already an option '%s'" % long_option
102 else:
103 option = (long_option, short_option, help_string)
Greg Ward071ed762000-09-26 02:12:31 +0000104 self.option_table.append(option)
Greg Wardffc10d92000-04-21 01:41:54 +0000105 self.option_index[long_option] = option
106
Greg Ward320df702000-04-21 02:31:07 +0000107
108 def has_option (self, long_option):
109 """Return true if the option table for this parser has an
110 option with long name 'long_option'."""
111 return self.option_index.has_key(long_option)
112
113 def get_attr_name (self, long_option):
Fred Drakeb94b8492001-12-06 20:51:35 +0000114 """Translate long option name 'long_option' to the form it
Greg Ward320df702000-04-21 02:31:07 +0000115 has as an attribute of some object: ie., translate hyphens
116 to underscores."""
Greg Ward071ed762000-09-26 02:12:31 +0000117 return string.translate(long_option, longopt_xlate)
Greg Ward320df702000-04-21 02:31:07 +0000118
119
Greg Ward1e7b5092000-04-21 04:22:01 +0000120 def _check_alias_dict (self, aliases, what):
121 assert type(aliases) is DictionaryType
122 for (alias, opt) in aliases.items():
123 if not self.option_index.has_key(alias):
124 raise DistutilsGetoptError, \
125 ("invalid %s '%s': "
126 "option '%s' not defined") % (what, alias, alias)
127 if not self.option_index.has_key(opt):
128 raise DistutilsGetoptError, \
129 ("invalid %s '%s': "
130 "aliased option '%s' not defined") % (what, alias, opt)
Fred Drakeb94b8492001-12-06 20:51:35 +0000131
Greg Ward1e7b5092000-04-21 04:22:01 +0000132 def set_aliases (self, alias):
133 """Set the aliases for this option parser."""
Greg Ward071ed762000-09-26 02:12:31 +0000134 self._check_alias_dict(alias, "alias")
Greg Ward1e7b5092000-04-21 04:22:01 +0000135 self.alias = alias
136
Greg Wardffc10d92000-04-21 01:41:54 +0000137 def set_negative_aliases (self, negative_alias):
138 """Set the negative aliases for this option parser.
139 'negative_alias' should be a dictionary mapping option names to
140 option names, both the key and value must already be defined
141 in the option table."""
Greg Ward071ed762000-09-26 02:12:31 +0000142 self._check_alias_dict(negative_alias, "negative alias")
Greg Wardffc10d92000-04-21 01:41:54 +0000143 self.negative_alias = negative_alias
144
145
146 def _grok_option_table (self):
Greg Ward071ed762000-09-26 02:12:31 +0000147 """Populate the various data structures that keep tabs on the
148 option table. Called by 'getopt()' before it can do anything
149 worthwhile.
150 """
Greg Ward0fd2dd62000-08-07 00:45:51 +0000151 self.long_opts = []
152 self.short_opts = []
153 self.short2long.clear()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000154 self.repeat = {}
Greg Ward0fd2dd62000-08-07 00:45:51 +0000155
Greg Wardffc10d92000-04-21 01:41:54 +0000156 for option in self.option_table:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000157 if len(option) == 3:
158 long, short, help = option
159 repeat = 0
160 elif len(option) == 4:
161 long, short, help, repeat = option
162 else:
163 # the option table is part of the code, so simply
164 # assert that it is correct
165 assert "invalid option tuple: %s" % `option`
Greg Wardffc10d92000-04-21 01:41:54 +0000166
167 # Type- and value-check the option names
168 if type(long) is not StringType or len(long) < 2:
169 raise DistutilsGetoptError, \
170 ("invalid long option '%s': "
171 "must be a string of length >= 2") % long
172
173 if (not ((short is None) or
Greg Ward071ed762000-09-26 02:12:31 +0000174 (type(short) is StringType and len(short) == 1))):
Greg Wardffc10d92000-04-21 01:41:54 +0000175 raise DistutilsGetoptError, \
176 ("invalid short option '%s': "
177 "must a single character or None") % short
178
Jeremy Hyltona181ec02002-06-04 20:24:05 +0000179 self.repeat[long] = repeat
Greg Ward071ed762000-09-26 02:12:31 +0000180 self.long_opts.append(long)
Greg Wardffc10d92000-04-21 01:41:54 +0000181
182 if long[-1] == '=': # option takes an argument?
183 if short: short = short + ':'
184 long = long[0:-1]
185 self.takes_arg[long] = 1
186 else:
187
188 # Is option is a "negative alias" for some other option (eg.
189 # "quiet" == "!verbose")?
190 alias_to = self.negative_alias.get(long)
191 if alias_to is not None:
192 if self.takes_arg[alias_to]:
193 raise DistutilsGetoptError, \
194 ("invalid negative alias '%s': "
195 "aliased option '%s' takes a value") % \
196 (long, alias_to)
197
198 self.long_opts[-1] = long # XXX redundant?!
199 self.takes_arg[long] = 0
200
201 else:
202 self.takes_arg[long] = 0
203
Greg Ward1e7b5092000-04-21 04:22:01 +0000204 # If this is an alias option, make sure its "takes arg" flag is
205 # the same as the option it's aliased to.
206 alias_to = self.alias.get(long)
207 if alias_to is not None:
208 if self.takes_arg[long] != self.takes_arg[alias_to]:
209 raise DistutilsGetoptError, \
210 ("invalid alias '%s': inconsistent with "
211 "aliased option '%s' (one of them takes a value, "
212 "the other doesn't") % (long, alias_to)
213
Greg Wardffc10d92000-04-21 01:41:54 +0000214
215 # Now enforce some bondage on the long option name, so we can
216 # later translate it to an attribute name on some object. Have
217 # to do this a bit late to make sure we've removed any trailing
218 # '='.
Greg Ward071ed762000-09-26 02:12:31 +0000219 if not longopt_re.match(long):
Greg Wardffc10d92000-04-21 01:41:54 +0000220 raise DistutilsGetoptError, \
221 ("invalid long option name '%s' " +
222 "(must be letters, numbers, hyphens only") % long
223
Greg Ward071ed762000-09-26 02:12:31 +0000224 self.attr_name[long] = self.get_attr_name(long)
Greg Wardffc10d92000-04-21 01:41:54 +0000225 if short:
Greg Ward071ed762000-09-26 02:12:31 +0000226 self.short_opts.append(short)
Greg Wardffc10d92000-04-21 01:41:54 +0000227 self.short2long[short[0]] = long
228
229 # for option_table
230
231 # _grok_option_table()
232
233
234 def getopt (self, args=None, object=None):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000235 """Parse command-line options in args. Store as attributes on object.
236
237 If 'args' is None or not supplied, uses 'sys.argv[1:]'. If
238 'object' is None or not supplied, creates a new OptionDummy
239 object, stores option values there, and returns a tuple (args,
240 object). If 'object' is supplied, it is modified in place and
241 'getopt()' just returns 'args'; in both cases, the returned
242 'args' is a modified copy of the passed-in 'args' list, which
243 is left untouched.
Greg Ward071ed762000-09-26 02:12:31 +0000244 """
Greg Wardffc10d92000-04-21 01:41:54 +0000245 if args is None:
246 args = sys.argv[1:]
247 if object is None:
Greg Ward981f7362000-05-23 03:53:10 +0000248 object = OptionDummy()
Greg Wardffc10d92000-04-21 01:41:54 +0000249 created_object = 1
250 else:
251 created_object = 0
252
253 self._grok_option_table()
254
Greg Ward071ed762000-09-26 02:12:31 +0000255 short_opts = string.join(self.short_opts)
Greg Wardffc10d92000-04-21 01:41:54 +0000256 try:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000257 opts, args = getopt.getopt(args, short_opts, self.long_opts)
Greg Wardffc10d92000-04-21 01:41:54 +0000258 except getopt.error, msg:
259 raise DistutilsArgError, msg
260
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000261 for opt, val in opts:
Greg Ward071ed762000-09-26 02:12:31 +0000262 if len(opt) == 2 and opt[0] == '-': # it's a short option
Greg Wardffc10d92000-04-21 01:41:54 +0000263 opt = self.short2long[opt[1]]
Greg Wardffc10d92000-04-21 01:41:54 +0000264 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000265 assert len(opt) > 2 and opt[:2] == '--'
266 opt = opt[2:]
Greg Wardffc10d92000-04-21 01:41:54 +0000267
Greg Ward1e7b5092000-04-21 04:22:01 +0000268 alias = self.alias.get(opt)
269 if alias:
270 opt = alias
271
Greg Wardffc10d92000-04-21 01:41:54 +0000272 if not self.takes_arg[opt]: # boolean option?
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000273 assert val == '', "boolean option can't have value"
Greg Ward071ed762000-09-26 02:12:31 +0000274 alias = self.negative_alias.get(opt)
Greg Wardffc10d92000-04-21 01:41:54 +0000275 if alias:
276 opt = alias
277 val = 0
278 else:
279 val = 1
280
281 attr = self.attr_name[opt]
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000282 # The only repeating option at the moment is 'verbose'.
283 # It has a negative option -q quiet, which should set verbose = 0.
284 if val and self.repeat.get(attr) is not None:
285 val = getattr(object, attr, 0) + 1
Greg Ward071ed762000-09-26 02:12:31 +0000286 setattr(object, attr, val)
287 self.option_order.append((opt, val))
Greg Wardffc10d92000-04-21 01:41:54 +0000288
289 # for opts
Greg Wardffc10d92000-04-21 01:41:54 +0000290 if created_object:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000291 return args, object
Greg Wardffc10d92000-04-21 01:41:54 +0000292 else:
293 return args
294
295 # getopt()
296
297
Greg Ward283c7452000-04-21 02:09:26 +0000298 def get_option_order (self):
Greg Wardffc10d92000-04-21 01:41:54 +0000299 """Returns the list of (option, value) tuples processed by the
Greg Ward283c7452000-04-21 02:09:26 +0000300 previous run of 'getopt()'. Raises RuntimeError if
Greg Ward071ed762000-09-26 02:12:31 +0000301 'getopt()' hasn't been called yet.
302 """
Greg Wardffc10d92000-04-21 01:41:54 +0000303 if self.option_order is None:
Greg Ward283c7452000-04-21 02:09:26 +0000304 raise RuntimeError, "'getopt()' hasn't been called yet"
Greg Wardffc10d92000-04-21 01:41:54 +0000305 else:
306 return self.option_order
307
Greg Ward283c7452000-04-21 02:09:26 +0000308
Greg Ward66bf4462000-04-23 02:50:45 +0000309 def generate_help (self, header=None):
Greg Ward283c7452000-04-21 02:09:26 +0000310 """Generate help text (a list of strings, one per suggested line of
Greg Ward071ed762000-09-26 02:12:31 +0000311 output) from the option table for this FancyGetopt object.
312 """
Greg Ward283c7452000-04-21 02:09:26 +0000313 # Blithely assume the option table is good: probably wouldn't call
314 # 'generate_help()' unless you've already called 'getopt()'.
315
316 # First pass: determine maximum length of long option names
317 max_opt = 0
318 for option in self.option_table:
319 long = option[0]
320 short = option[1]
Greg Ward071ed762000-09-26 02:12:31 +0000321 l = len(long)
Greg Ward283c7452000-04-21 02:09:26 +0000322 if long[-1] == '=':
323 l = l - 1
324 if short is not None:
325 l = l + 5 # " (-x)" where short == 'x'
326 if l > max_opt:
327 max_opt = l
328
329 opt_width = max_opt + 2 + 2 + 2 # room for indent + dashes + gutter
330
331 # Typical help block looks like this:
332 # --foo controls foonabulation
333 # Help block for longest option looks like this:
334 # --flimflam set the flim-flam level
335 # and with wrapped text:
336 # --flimflam set the flim-flam level (must be between
337 # 0 and 100, except on Tuesdays)
338 # Options with short names will have the short name shown (but
339 # it doesn't contribute to max_opt):
340 # --foo (-f) controls foonabulation
341 # If adding the short option would make the left column too wide,
342 # we push the explanation off to the next line
343 # --flimflam (-l)
344 # set the flim-flam level
345 # Important parameters:
346 # - 2 spaces before option block start lines
347 # - 2 dashes for each long option name
348 # - min. 2 spaces between option and explanation (gutter)
349 # - 5 characters (incl. space) for short option name
350
351 # Now generate lines of help text. (If 80 columns were good enough
352 # for Jesus, then 78 columns are good enough for me!)
353 line_width = 78
354 text_width = line_width - opt_width
355 big_indent = ' ' * opt_width
356 if header:
357 lines = [header]
358 else:
359 lines = ['Option summary:']
360
Jeremy Hylton40ebbef2002-06-04 21:10:35 +0000361 for option in self.option_table:
Jeremy Hylton8f787bf2002-06-04 21:11:56 +0000362 long, short, help = option[:3]
Greg Ward071ed762000-09-26 02:12:31 +0000363 text = wrap_text(help, text_width)
Greg Ward283c7452000-04-21 02:09:26 +0000364 if long[-1] == '=':
365 long = long[0:-1]
366
367 # Case 1: no short option at all (makes life easy)
368 if short is None:
369 if text:
Greg Ward071ed762000-09-26 02:12:31 +0000370 lines.append(" --%-*s %s" % (max_opt, long, text[0]))
Greg Ward283c7452000-04-21 02:09:26 +0000371 else:
Greg Ward071ed762000-09-26 02:12:31 +0000372 lines.append(" --%-*s " % (max_opt, long))
Greg Ward283c7452000-04-21 02:09:26 +0000373
Greg Ward283c7452000-04-21 02:09:26 +0000374 # Case 2: we have a short option, so we have to include it
375 # just after the long option
376 else:
377 opt_names = "%s (-%s)" % (long, short)
378 if text:
Greg Ward071ed762000-09-26 02:12:31 +0000379 lines.append(" --%-*s %s" %
380 (max_opt, opt_names, text[0]))
Greg Ward283c7452000-04-21 02:09:26 +0000381 else:
Greg Ward071ed762000-09-26 02:12:31 +0000382 lines.append(" --%-*s" % opt_names)
Greg Ward283c7452000-04-21 02:09:26 +0000383
Greg Ward373dbfa2000-06-08 00:35:33 +0000384 for l in text[1:]:
Greg Ward071ed762000-09-26 02:12:31 +0000385 lines.append(big_indent + l)
Greg Ward373dbfa2000-06-08 00:35:33 +0000386
Greg Ward283c7452000-04-21 02:09:26 +0000387 # for self.option_table
388
389 return lines
390
391 # generate_help ()
392
Greg Ward66bf4462000-04-23 02:50:45 +0000393 def print_help (self, header=None, file=None):
Greg Ward283c7452000-04-21 02:09:26 +0000394 if file is None:
395 file = sys.stdout
Greg Ward071ed762000-09-26 02:12:31 +0000396 for line in self.generate_help(header):
397 file.write(line + "\n")
Greg Ward283c7452000-04-21 02:09:26 +0000398
Greg Wardffc10d92000-04-21 01:41:54 +0000399# class FancyGetopt
400
Greg Ward2689e3d1999-03-22 14:52:19 +0000401
Greg Ward44f8e4e1999-12-12 16:54:55 +0000402def fancy_getopt (options, negative_opt, object, args):
Greg Ward071ed762000-09-26 02:12:31 +0000403 parser = FancyGetopt(options)
404 parser.set_negative_aliases(negative_opt)
405 return parser.getopt(args, object)
Greg Wardffc10d92000-04-21 01:41:54 +0000406
407
Greg Ward071ed762000-09-26 02:12:31 +0000408WS_TRANS = string.maketrans(string.whitespace, ' ' * len(string.whitespace))
Greg Ward44f8e4e1999-12-12 16:54:55 +0000409
410def wrap_text (text, width):
Greg Ward46a69b92000-08-30 17:16:27 +0000411 """wrap_text(text : string, width : int) -> [string]
412
413 Split 'text' into multiple lines of no more than 'width' characters
414 each, and return the list of strings that results.
415 """
Greg Ward44f8e4e1999-12-12 16:54:55 +0000416
417 if text is None:
418 return []
Greg Ward071ed762000-09-26 02:12:31 +0000419 if len(text) <= width:
Greg Ward44f8e4e1999-12-12 16:54:55 +0000420 return [text]
421
Greg Ward071ed762000-09-26 02:12:31 +0000422 text = string.expandtabs(text)
423 text = string.translate(text, WS_TRANS)
424 chunks = re.split(r'( +|-+)', text)
425 chunks = filter(None, chunks) # ' - ' results in empty strings
Greg Ward44f8e4e1999-12-12 16:54:55 +0000426 lines = []
427
428 while chunks:
429
430 cur_line = [] # list of chunks (to-be-joined)
431 cur_len = 0 # length of current line
432
433 while chunks:
Greg Ward071ed762000-09-26 02:12:31 +0000434 l = len(chunks[0])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000435 if cur_len + l <= width: # can squeeze (at least) this chunk in
Greg Ward071ed762000-09-26 02:12:31 +0000436 cur_line.append(chunks[0])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000437 del chunks[0]
438 cur_len = cur_len + l
439 else: # this line is full
440 # drop last chunk if all space
441 if cur_line and cur_line[-1][0] == ' ':
442 del cur_line[-1]
443 break
444
445 if chunks: # any chunks left to process?
446
447 # if the current line is still empty, then we had a single
448 # chunk that's too big too fit on a line -- so we break
449 # down and break it up at the line width
450 if cur_len == 0:
Greg Ward071ed762000-09-26 02:12:31 +0000451 cur_line.append(chunks[0][0:width])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000452 chunks[0] = chunks[0][width:]
453
454 # all-whitespace chunks at the end of a line can be discarded
455 # (and we know from the re.split above that if a chunk has
456 # *any* whitespace, it is *all* whitespace)
457 if chunks[0][0] == ' ':
458 del chunks[0]
459
460 # and store this line in the list-of-all-lines -- as a single
461 # string, of course!
Greg Ward071ed762000-09-26 02:12:31 +0000462 lines.append(string.join(cur_line, ''))
Greg Ward44f8e4e1999-12-12 16:54:55 +0000463
464 # while chunks
465
466 return lines
467
468# wrap_text ()
Greg Ward68ded6e2000-09-25 01:58:31 +0000469
470
471def translate_longopt (opt):
472 """Convert a long option name to a valid Python identifier by
473 changing "-" to "_".
474 """
475 return string.translate(opt, longopt_xlate)
Fred Drakeb94b8492001-12-06 20:51:35 +0000476
Greg Ward44f8e4e1999-12-12 16:54:55 +0000477
Greg Wardffc10d92000-04-21 01:41:54 +0000478class OptionDummy:
479 """Dummy class just used as a place to hold command-line option
480 values as instance attributes."""
Greg Ward3c67b1d2000-05-23 01:44:20 +0000481
482 def __init__ (self, options=[]):
483 """Create a new OptionDummy instance. The attributes listed in
484 'options' will be initialized to None."""
485 for opt in options:
486 setattr(self, opt, None)
487
488# class OptionDummy
Fred Drakeb94b8492001-12-06 20:51:35 +0000489
Greg Ward44f8e4e1999-12-12 16:54:55 +0000490
491if __name__ == "__main__":
492 text = """\
493Tra-la-la, supercalifragilisticexpialidocious.
494How *do* you spell that odd word, anyways?
495(Someone ask Mary -- she'll know [or she'll
496say, "How should I know?"].)"""
497
498 for w in (10, 20, 30, 40):
499 print "width: %d" % w
Greg Ward071ed762000-09-26 02:12:31 +0000500 print string.join(wrap_text(text, w), "\n")
Greg Ward44f8e4e1999-12-12 16:54:55 +0000501 print