blob: f78b0a6854416c7a0e91f1dded1bbdb90f5bb176 [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
Greg Ward3ce77fd2000-03-02 01:49:45 +000011__revision__ = "$Id$"
Greg Ward2689e3d1999-03-22 14:52:19 +000012
Greg Ward44f8e4e1999-12-12 16:54:55 +000013import sys, string, re
Greg Ward2689e3d1999-03-22 14:52:19 +000014from types import *
15import getopt
16from distutils.errors import *
17
18# Much like command_re in distutils.core, this is close to but not quite
19# the same as a Python NAME -- except, in the spirit of most GNU
20# utilities, we use '-' in place of '_'. (The spirit of LISP lives on!)
21# The similarities to NAME are again not a coincidence...
Greg Warda564cc31999-10-03 20:48:53 +000022longopt_pat = r'[a-zA-Z](?:[a-zA-Z0-9-]*)'
Greg Ward071ed762000-09-26 02:12:31 +000023longopt_re = re.compile(r'^%s$' % longopt_pat)
Greg Warda564cc31999-10-03 20:48:53 +000024
25# For recognizing "negative alias" options, eg. "quiet=!verbose"
Greg Ward071ed762000-09-26 02:12:31 +000026neg_alias_re = re.compile("^(%s)=!(%s)$" % (longopt_pat, longopt_pat))
Greg Warda564cc31999-10-03 20:48:53 +000027
Greg Ward2689e3d1999-03-22 14:52:19 +000028# This is used to translate long options to legitimate Python identifiers
29# (for use as attributes of some object).
Greg Ward071ed762000-09-26 02:12:31 +000030longopt_xlate = string.maketrans('-', '_')
Greg Ward2689e3d1999-03-22 14:52:19 +000031
Greg Wardffc10d92000-04-21 01:41:54 +000032class FancyGetopt:
33 """Wrapper around the standard 'getopt()' module that provides some
34 handy extra functionality:
35 * short and long options are tied together
36 * options have help strings, and help text can be assembled
37 from them
38 * options set attributes of a passed-in object
39 * boolean options can have "negative aliases" -- eg. if
40 --quiet is the "negative alias" of --verbose, then "--quiet"
41 on the command line sets 'verbose' to false
42 """
43
44 def __init__ (self, option_table=None):
45
46 # The option table is (currently) a list of 3-tuples:
47 # (long_option, short_option, help_string)
48 # if an option takes an argument, its long_option should have '='
49 # appended; short_option should just be a single character, no ':'
50 # in any case. If a long_option doesn't have a corresponding
51 # short_option, short_option should be None. All option tuples
52 # must have long options.
53 self.option_table = option_table
54
55 # 'option_index' maps long option names to entries in the option
56 # table (ie. those 3-tuples).
57 self.option_index = {}
58 if self.option_table:
Greg Ward283c7452000-04-21 02:09:26 +000059 self._build_index()
Greg Wardffc10d92000-04-21 01:41:54 +000060
Greg Ward1e7b5092000-04-21 04:22:01 +000061 # 'alias' records (duh) alias options; {'foo': 'bar'} means
62 # --foo is an alias for --bar
63 self.alias = {}
64
Greg Wardffc10d92000-04-21 01:41:54 +000065 # 'negative_alias' keeps track of options that are the boolean
66 # opposite of some other option
67 self.negative_alias = {}
Fred Drakeb94b8492001-12-06 20:51:35 +000068
Greg Wardffc10d92000-04-21 01:41:54 +000069 # These keep track of the information in the option table. We
70 # don't actually populate these structures until we're ready to
71 # parse the command-line, since the 'option_table' passed in here
72 # isn't necessarily the final word.
73 self.short_opts = []
74 self.long_opts = []
75 self.short2long = {}
76 self.attr_name = {}
77 self.takes_arg = {}
78
79 # And 'option_order' is filled up in 'getopt()'; it records the
80 # original order of options (and their values) on the command-line,
81 # but expands short options, converts aliases, etc.
82 self.option_order = []
83
84 # __init__ ()
Fred Drakeb94b8492001-12-06 20:51:35 +000085
Greg Wardffc10d92000-04-21 01:41:54 +000086
Greg Ward283c7452000-04-21 02:09:26 +000087 def _build_index (self):
Greg Ward0fd2dd62000-08-07 00:45:51 +000088 self.option_index.clear()
Greg Wardffc10d92000-04-21 01:41:54 +000089 for option in self.option_table:
90 self.option_index[option[0]] = option
91
Greg Ward283c7452000-04-21 02:09:26 +000092 def set_option_table (self, option_table):
93 self.option_table = option_table
94 self._build_index()
95
Greg Wardffc10d92000-04-21 01:41:54 +000096 def add_option (self, long_option, short_option=None, help_string=None):
97 if self.option_index.has_key(long_option):
98 raise DistutilsGetoptError, \
99 "option conflict: already an option '%s'" % long_option
100 else:
101 option = (long_option, short_option, help_string)
Greg Ward071ed762000-09-26 02:12:31 +0000102 self.option_table.append(option)
Greg Wardffc10d92000-04-21 01:41:54 +0000103 self.option_index[long_option] = option
104
Greg Ward320df702000-04-21 02:31:07 +0000105
106 def has_option (self, long_option):
107 """Return true if the option table for this parser has an
108 option with long name 'long_option'."""
109 return self.option_index.has_key(long_option)
110
111 def get_attr_name (self, long_option):
Fred Drakeb94b8492001-12-06 20:51:35 +0000112 """Translate long option name 'long_option' to the form it
Greg Ward320df702000-04-21 02:31:07 +0000113 has as an attribute of some object: ie., translate hyphens
114 to underscores."""
Greg Ward071ed762000-09-26 02:12:31 +0000115 return string.translate(long_option, longopt_xlate)
Greg Ward320df702000-04-21 02:31:07 +0000116
117
Greg Ward1e7b5092000-04-21 04:22:01 +0000118 def _check_alias_dict (self, aliases, what):
119 assert type(aliases) is DictionaryType
120 for (alias, opt) in aliases.items():
121 if not self.option_index.has_key(alias):
122 raise DistutilsGetoptError, \
123 ("invalid %s '%s': "
124 "option '%s' not defined") % (what, alias, alias)
125 if not self.option_index.has_key(opt):
126 raise DistutilsGetoptError, \
127 ("invalid %s '%s': "
128 "aliased option '%s' not defined") % (what, alias, opt)
Fred Drakeb94b8492001-12-06 20:51:35 +0000129
Greg Ward1e7b5092000-04-21 04:22:01 +0000130 def set_aliases (self, alias):
131 """Set the aliases for this option parser."""
Greg Ward071ed762000-09-26 02:12:31 +0000132 self._check_alias_dict(alias, "alias")
Greg Ward1e7b5092000-04-21 04:22:01 +0000133 self.alias = alias
134
Greg Wardffc10d92000-04-21 01:41:54 +0000135 def set_negative_aliases (self, negative_alias):
136 """Set the negative aliases for this option parser.
137 'negative_alias' should be a dictionary mapping option names to
138 option names, both the key and value must already be defined
139 in the option table."""
Greg Ward071ed762000-09-26 02:12:31 +0000140 self._check_alias_dict(negative_alias, "negative alias")
Greg Wardffc10d92000-04-21 01:41:54 +0000141 self.negative_alias = negative_alias
142
143
144 def _grok_option_table (self):
Greg Ward071ed762000-09-26 02:12:31 +0000145 """Populate the various data structures that keep tabs on the
146 option table. Called by 'getopt()' before it can do anything
147 worthwhile.
148 """
Greg Ward0fd2dd62000-08-07 00:45:51 +0000149 self.long_opts = []
150 self.short_opts = []
151 self.short2long.clear()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000152 self.repeat = {}
Greg Ward0fd2dd62000-08-07 00:45:51 +0000153
Greg Wardffc10d92000-04-21 01:41:54 +0000154 for option in self.option_table:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000155 if len(option) == 3:
156 long, short, help = option
157 repeat = 0
158 elif len(option) == 4:
159 long, short, help, repeat = option
160 else:
161 # the option table is part of the code, so simply
162 # assert that it is correct
163 assert "invalid option tuple: %s" % `option`
Greg Wardffc10d92000-04-21 01:41:54 +0000164
165 # Type- and value-check the option names
166 if type(long) is not StringType or len(long) < 2:
167 raise DistutilsGetoptError, \
168 ("invalid long option '%s': "
169 "must be a string of length >= 2") % long
170
171 if (not ((short is None) or
Greg Ward071ed762000-09-26 02:12:31 +0000172 (type(short) is StringType and len(short) == 1))):
Greg Wardffc10d92000-04-21 01:41:54 +0000173 raise DistutilsGetoptError, \
174 ("invalid short option '%s': "
175 "must a single character or None") % short
176
Jeremy Hyltona181ec02002-06-04 20:24:05 +0000177 self.repeat[long] = repeat
Greg Ward071ed762000-09-26 02:12:31 +0000178 self.long_opts.append(long)
Greg Wardffc10d92000-04-21 01:41:54 +0000179
180 if long[-1] == '=': # option takes an argument?
181 if short: short = short + ':'
182 long = long[0:-1]
183 self.takes_arg[long] = 1
184 else:
185
186 # Is option is a "negative alias" for some other option (eg.
187 # "quiet" == "!verbose")?
188 alias_to = self.negative_alias.get(long)
189 if alias_to is not None:
190 if self.takes_arg[alias_to]:
191 raise DistutilsGetoptError, \
192 ("invalid negative alias '%s': "
193 "aliased option '%s' takes a value") % \
194 (long, alias_to)
195
196 self.long_opts[-1] = long # XXX redundant?!
197 self.takes_arg[long] = 0
198
199 else:
200 self.takes_arg[long] = 0
201
Greg Ward1e7b5092000-04-21 04:22:01 +0000202 # If this is an alias option, make sure its "takes arg" flag is
203 # the same as the option it's aliased to.
204 alias_to = self.alias.get(long)
205 if alias_to is not None:
206 if self.takes_arg[long] != self.takes_arg[alias_to]:
207 raise DistutilsGetoptError, \
208 ("invalid alias '%s': inconsistent with "
209 "aliased option '%s' (one of them takes a value, "
210 "the other doesn't") % (long, alias_to)
211
Greg Wardffc10d92000-04-21 01:41:54 +0000212
213 # Now enforce some bondage on the long option name, so we can
214 # later translate it to an attribute name on some object. Have
215 # to do this a bit late to make sure we've removed any trailing
216 # '='.
Greg Ward071ed762000-09-26 02:12:31 +0000217 if not longopt_re.match(long):
Greg Wardffc10d92000-04-21 01:41:54 +0000218 raise DistutilsGetoptError, \
219 ("invalid long option name '%s' " +
220 "(must be letters, numbers, hyphens only") % long
221
Greg Ward071ed762000-09-26 02:12:31 +0000222 self.attr_name[long] = self.get_attr_name(long)
Greg Wardffc10d92000-04-21 01:41:54 +0000223 if short:
Greg Ward071ed762000-09-26 02:12:31 +0000224 self.short_opts.append(short)
Greg Wardffc10d92000-04-21 01:41:54 +0000225 self.short2long[short[0]] = long
226
227 # for option_table
228
229 # _grok_option_table()
230
231
232 def getopt (self, args=None, object=None):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000233 """Parse command-line options in args. Store as attributes on object.
234
235 If 'args' is None or not supplied, uses 'sys.argv[1:]'. If
236 'object' is None or not supplied, creates a new OptionDummy
237 object, stores option values there, and returns a tuple (args,
238 object). If 'object' is supplied, it is modified in place and
239 'getopt()' just returns 'args'; in both cases, the returned
240 'args' is a modified copy of the passed-in 'args' list, which
241 is left untouched.
Greg Ward071ed762000-09-26 02:12:31 +0000242 """
Greg Wardffc10d92000-04-21 01:41:54 +0000243 if args is None:
244 args = sys.argv[1:]
245 if object is None:
Greg Ward981f7362000-05-23 03:53:10 +0000246 object = OptionDummy()
Greg Wardffc10d92000-04-21 01:41:54 +0000247 created_object = 1
248 else:
249 created_object = 0
250
251 self._grok_option_table()
252
Greg Ward071ed762000-09-26 02:12:31 +0000253 short_opts = string.join(self.short_opts)
Greg Wardffc10d92000-04-21 01:41:54 +0000254 try:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000255 opts, args = getopt.getopt(args, short_opts, self.long_opts)
Greg Wardffc10d92000-04-21 01:41:54 +0000256 except getopt.error, msg:
257 raise DistutilsArgError, msg
258
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000259 for opt, val in opts:
Greg Ward071ed762000-09-26 02:12:31 +0000260 if len(opt) == 2 and opt[0] == '-': # it's a short option
Greg Wardffc10d92000-04-21 01:41:54 +0000261 opt = self.short2long[opt[1]]
Greg Wardffc10d92000-04-21 01:41:54 +0000262 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000263 assert len(opt) > 2 and opt[:2] == '--'
264 opt = opt[2:]
Greg Wardffc10d92000-04-21 01:41:54 +0000265
Greg Ward1e7b5092000-04-21 04:22:01 +0000266 alias = self.alias.get(opt)
267 if alias:
268 opt = alias
269
Greg Wardffc10d92000-04-21 01:41:54 +0000270 if not self.takes_arg[opt]: # boolean option?
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000271 assert val == '', "boolean option can't have value"
Greg Ward071ed762000-09-26 02:12:31 +0000272 alias = self.negative_alias.get(opt)
Greg Wardffc10d92000-04-21 01:41:54 +0000273 if alias:
274 opt = alias
275 val = 0
276 else:
277 val = 1
278
279 attr = self.attr_name[opt]
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000280 # The only repeating option at the moment is 'verbose'.
281 # It has a negative option -q quiet, which should set verbose = 0.
282 if val and self.repeat.get(attr) is not None:
283 val = getattr(object, attr, 0) + 1
Greg Ward071ed762000-09-26 02:12:31 +0000284 setattr(object, attr, val)
285 self.option_order.append((opt, val))
Greg Wardffc10d92000-04-21 01:41:54 +0000286
287 # for opts
Greg Wardffc10d92000-04-21 01:41:54 +0000288 if created_object:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000289 return args, object
Greg Wardffc10d92000-04-21 01:41:54 +0000290 else:
291 return args
292
293 # getopt()
294
295
Greg Ward283c7452000-04-21 02:09:26 +0000296 def get_option_order (self):
Greg Wardffc10d92000-04-21 01:41:54 +0000297 """Returns the list of (option, value) tuples processed by the
Greg Ward283c7452000-04-21 02:09:26 +0000298 previous run of 'getopt()'. Raises RuntimeError if
Greg Ward071ed762000-09-26 02:12:31 +0000299 'getopt()' hasn't been called yet.
300 """
Greg Wardffc10d92000-04-21 01:41:54 +0000301 if self.option_order is None:
Greg Ward283c7452000-04-21 02:09:26 +0000302 raise RuntimeError, "'getopt()' hasn't been called yet"
Greg Wardffc10d92000-04-21 01:41:54 +0000303 else:
304 return self.option_order
305
Greg Ward283c7452000-04-21 02:09:26 +0000306
Greg Ward66bf4462000-04-23 02:50:45 +0000307 def generate_help (self, header=None):
Greg Ward283c7452000-04-21 02:09:26 +0000308 """Generate help text (a list of strings, one per suggested line of
Greg Ward071ed762000-09-26 02:12:31 +0000309 output) from the option table for this FancyGetopt object.
310 """
Greg Ward283c7452000-04-21 02:09:26 +0000311 # Blithely assume the option table is good: probably wouldn't call
312 # 'generate_help()' unless you've already called 'getopt()'.
313
314 # First pass: determine maximum length of long option names
315 max_opt = 0
316 for option in self.option_table:
317 long = option[0]
318 short = option[1]
Greg Ward071ed762000-09-26 02:12:31 +0000319 l = len(long)
Greg Ward283c7452000-04-21 02:09:26 +0000320 if long[-1] == '=':
321 l = l - 1
322 if short is not None:
323 l = l + 5 # " (-x)" where short == 'x'
324 if l > max_opt:
325 max_opt = l
326
327 opt_width = max_opt + 2 + 2 + 2 # room for indent + dashes + gutter
328
329 # Typical help block looks like this:
330 # --foo controls foonabulation
331 # Help block for longest option looks like this:
332 # --flimflam set the flim-flam level
333 # and with wrapped text:
334 # --flimflam set the flim-flam level (must be between
335 # 0 and 100, except on Tuesdays)
336 # Options with short names will have the short name shown (but
337 # it doesn't contribute to max_opt):
338 # --foo (-f) controls foonabulation
339 # If adding the short option would make the left column too wide,
340 # we push the explanation off to the next line
341 # --flimflam (-l)
342 # set the flim-flam level
343 # Important parameters:
344 # - 2 spaces before option block start lines
345 # - 2 dashes for each long option name
346 # - min. 2 spaces between option and explanation (gutter)
347 # - 5 characters (incl. space) for short option name
348
349 # Now generate lines of help text. (If 80 columns were good enough
350 # for Jesus, then 78 columns are good enough for me!)
351 line_width = 78
352 text_width = line_width - opt_width
353 big_indent = ' ' * opt_width
354 if header:
355 lines = [header]
356 else:
357 lines = ['Option summary:']
358
Jeremy Hylton40ebbef2002-06-04 21:10:35 +0000359 for option in self.option_table:
Jeremy Hylton8f787bf2002-06-04 21:11:56 +0000360 long, short, help = option[:3]
Greg Ward071ed762000-09-26 02:12:31 +0000361 text = wrap_text(help, text_width)
Greg Ward283c7452000-04-21 02:09:26 +0000362 if long[-1] == '=':
363 long = long[0:-1]
364
365 # Case 1: no short option at all (makes life easy)
366 if short is None:
367 if text:
Greg Ward071ed762000-09-26 02:12:31 +0000368 lines.append(" --%-*s %s" % (max_opt, long, text[0]))
Greg Ward283c7452000-04-21 02:09:26 +0000369 else:
Greg Ward071ed762000-09-26 02:12:31 +0000370 lines.append(" --%-*s " % (max_opt, long))
Greg Ward283c7452000-04-21 02:09:26 +0000371
Greg Ward283c7452000-04-21 02:09:26 +0000372 # Case 2: we have a short option, so we have to include it
373 # just after the long option
374 else:
375 opt_names = "%s (-%s)" % (long, short)
376 if text:
Greg Ward071ed762000-09-26 02:12:31 +0000377 lines.append(" --%-*s %s" %
378 (max_opt, opt_names, text[0]))
Greg Ward283c7452000-04-21 02:09:26 +0000379 else:
Greg Ward071ed762000-09-26 02:12:31 +0000380 lines.append(" --%-*s" % opt_names)
Greg Ward283c7452000-04-21 02:09:26 +0000381
Greg Ward373dbfa2000-06-08 00:35:33 +0000382 for l in text[1:]:
Greg Ward071ed762000-09-26 02:12:31 +0000383 lines.append(big_indent + l)
Greg Ward373dbfa2000-06-08 00:35:33 +0000384
Greg Ward283c7452000-04-21 02:09:26 +0000385 # for self.option_table
386
387 return lines
388
389 # generate_help ()
390
Greg Ward66bf4462000-04-23 02:50:45 +0000391 def print_help (self, header=None, file=None):
Greg Ward283c7452000-04-21 02:09:26 +0000392 if file is None:
393 file = sys.stdout
Greg Ward071ed762000-09-26 02:12:31 +0000394 for line in self.generate_help(header):
395 file.write(line + "\n")
Greg Ward283c7452000-04-21 02:09:26 +0000396
Greg Wardffc10d92000-04-21 01:41:54 +0000397# class FancyGetopt
398
Greg Ward2689e3d1999-03-22 14:52:19 +0000399
Greg Ward44f8e4e1999-12-12 16:54:55 +0000400def fancy_getopt (options, negative_opt, object, args):
Greg Ward071ed762000-09-26 02:12:31 +0000401 parser = FancyGetopt(options)
402 parser.set_negative_aliases(negative_opt)
403 return parser.getopt(args, object)
Greg Wardffc10d92000-04-21 01:41:54 +0000404
405
Greg Ward071ed762000-09-26 02:12:31 +0000406WS_TRANS = string.maketrans(string.whitespace, ' ' * len(string.whitespace))
Greg Ward44f8e4e1999-12-12 16:54:55 +0000407
408def wrap_text (text, width):
Greg Ward46a69b92000-08-30 17:16:27 +0000409 """wrap_text(text : string, width : int) -> [string]
410
411 Split 'text' into multiple lines of no more than 'width' characters
412 each, and return the list of strings that results.
413 """
Greg Ward44f8e4e1999-12-12 16:54:55 +0000414
415 if text is None:
416 return []
Greg Ward071ed762000-09-26 02:12:31 +0000417 if len(text) <= width:
Greg Ward44f8e4e1999-12-12 16:54:55 +0000418 return [text]
419
Greg Ward071ed762000-09-26 02:12:31 +0000420 text = string.expandtabs(text)
421 text = string.translate(text, WS_TRANS)
422 chunks = re.split(r'( +|-+)', text)
423 chunks = filter(None, chunks) # ' - ' results in empty strings
Greg Ward44f8e4e1999-12-12 16:54:55 +0000424 lines = []
425
426 while chunks:
427
428 cur_line = [] # list of chunks (to-be-joined)
429 cur_len = 0 # length of current line
430
431 while chunks:
Greg Ward071ed762000-09-26 02:12:31 +0000432 l = len(chunks[0])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000433 if cur_len + l <= width: # can squeeze (at least) this chunk in
Greg Ward071ed762000-09-26 02:12:31 +0000434 cur_line.append(chunks[0])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000435 del chunks[0]
436 cur_len = cur_len + l
437 else: # this line is full
438 # drop last chunk if all space
439 if cur_line and cur_line[-1][0] == ' ':
440 del cur_line[-1]
441 break
442
443 if chunks: # any chunks left to process?
444
445 # if the current line is still empty, then we had a single
446 # chunk that's too big too fit on a line -- so we break
447 # down and break it up at the line width
448 if cur_len == 0:
Greg Ward071ed762000-09-26 02:12:31 +0000449 cur_line.append(chunks[0][0:width])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000450 chunks[0] = chunks[0][width:]
451
452 # all-whitespace chunks at the end of a line can be discarded
453 # (and we know from the re.split above that if a chunk has
454 # *any* whitespace, it is *all* whitespace)
455 if chunks[0][0] == ' ':
456 del chunks[0]
457
458 # and store this line in the list-of-all-lines -- as a single
459 # string, of course!
Greg Ward071ed762000-09-26 02:12:31 +0000460 lines.append(string.join(cur_line, ''))
Greg Ward44f8e4e1999-12-12 16:54:55 +0000461
462 # while chunks
463
464 return lines
465
466# wrap_text ()
Greg Ward68ded6e2000-09-25 01:58:31 +0000467
468
469def translate_longopt (opt):
470 """Convert a long option name to a valid Python identifier by
471 changing "-" to "_".
472 """
473 return string.translate(opt, longopt_xlate)
Fred Drakeb94b8492001-12-06 20:51:35 +0000474
Greg Ward44f8e4e1999-12-12 16:54:55 +0000475
Greg Wardffc10d92000-04-21 01:41:54 +0000476class OptionDummy:
477 """Dummy class just used as a place to hold command-line option
478 values as instance attributes."""
Greg Ward3c67b1d2000-05-23 01:44:20 +0000479
480 def __init__ (self, options=[]):
481 """Create a new OptionDummy instance. The attributes listed in
482 'options' will be initialized to None."""
483 for opt in options:
484 setattr(self, opt, None)
485
486# class OptionDummy
Fred Drakeb94b8492001-12-06 20:51:35 +0000487
Greg Ward44f8e4e1999-12-12 16:54:55 +0000488
489if __name__ == "__main__":
490 text = """\
491Tra-la-la, supercalifragilisticexpialidocious.
492How *do* you spell that odd word, anyways?
493(Someone ask Mary -- she'll know [or she'll
494say, "How should I know?"].)"""
495
496 for w in (10, 20, 30, 40):
497 print "width: %d" % w
Greg Ward071ed762000-09-26 02:12:31 +0000498 print string.join(wrap_text(text, w), "\n")
Greg Ward44f8e4e1999-12-12 16:54:55 +0000499 print