blob: e19319ae9d0a8a8285818f20580f721f40aec3be [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
Fred Drake576298d2004-08-02 17:58:51 +000046 # The option table is (currently) a list of tuples. The
47 # tuples may have 3 or four values:
48 # (long_option, short_option, help_string [, repeatable])
Greg Wardffc10d92000-04-21 01:41:54 +000049 # if an option takes an argument, its long_option should have '='
50 # appended; short_option should just be a single character, no ':'
51 # in any case. If a long_option doesn't have a corresponding
52 # short_option, short_option should be None. All option tuples
53 # must have long options.
54 self.option_table = option_table
55
56 # 'option_index' maps long option names to entries in the option
57 # table (ie. those 3-tuples).
58 self.option_index = {}
59 if self.option_table:
Greg Ward283c7452000-04-21 02:09:26 +000060 self._build_index()
Greg Wardffc10d92000-04-21 01:41:54 +000061
Greg Ward1e7b5092000-04-21 04:22:01 +000062 # 'alias' records (duh) alias options; {'foo': 'bar'} means
63 # --foo is an alias for --bar
64 self.alias = {}
65
Greg Wardffc10d92000-04-21 01:41:54 +000066 # 'negative_alias' keeps track of options that are the boolean
67 # opposite of some other option
68 self.negative_alias = {}
Fred Drakeb94b8492001-12-06 20:51:35 +000069
Greg Wardffc10d92000-04-21 01:41:54 +000070 # These keep track of the information in the option table. We
71 # don't actually populate these structures until we're ready to
72 # parse the command-line, since the 'option_table' passed in here
73 # isn't necessarily the final word.
74 self.short_opts = []
75 self.long_opts = []
76 self.short2long = {}
77 self.attr_name = {}
78 self.takes_arg = {}
79
80 # And 'option_order' is filled up in 'getopt()'; it records the
81 # original order of options (and their values) on the command-line,
82 # but expands short options, converts aliases, etc.
83 self.option_order = []
84
85 # __init__ ()
Fred Drakeb94b8492001-12-06 20:51:35 +000086
Greg Wardffc10d92000-04-21 01:41:54 +000087
Greg Ward283c7452000-04-21 02:09:26 +000088 def _build_index (self):
Greg Ward0fd2dd62000-08-07 00:45:51 +000089 self.option_index.clear()
Greg Wardffc10d92000-04-21 01:41:54 +000090 for option in self.option_table:
91 self.option_index[option[0]] = option
92
Greg Ward283c7452000-04-21 02:09:26 +000093 def set_option_table (self, option_table):
94 self.option_table = option_table
95 self._build_index()
96
Greg Wardffc10d92000-04-21 01:41:54 +000097 def add_option (self, long_option, short_option=None, help_string=None):
Guido van Rossum8bc09652008-02-21 18:18:37 +000098 if long_option in self.option_index:
Greg Wardffc10d92000-04-21 01:41:54 +000099 raise DistutilsGetoptError, \
100 "option conflict: already an option '%s'" % long_option
101 else:
102 option = (long_option, short_option, help_string)
Greg Ward071ed762000-09-26 02:12:31 +0000103 self.option_table.append(option)
Greg Wardffc10d92000-04-21 01:41:54 +0000104 self.option_index[long_option] = option
105
Greg Ward320df702000-04-21 02:31:07 +0000106
107 def has_option (self, long_option):
108 """Return true if the option table for this parser has an
109 option with long name 'long_option'."""
Guido van Rossum8bc09652008-02-21 18:18:37 +0000110 return long_option in self.option_index
Greg Ward320df702000-04-21 02:31:07 +0000111
112 def get_attr_name (self, long_option):
Fred Drakeb94b8492001-12-06 20:51:35 +0000113 """Translate long option name 'long_option' to the form it
Greg Ward320df702000-04-21 02:31:07 +0000114 has as an attribute of some object: ie., translate hyphens
115 to underscores."""
Greg Ward071ed762000-09-26 02:12:31 +0000116 return string.translate(long_option, longopt_xlate)
Greg Ward320df702000-04-21 02:31:07 +0000117
118
Greg Ward1e7b5092000-04-21 04:22:01 +0000119 def _check_alias_dict (self, aliases, what):
120 assert type(aliases) is DictionaryType
121 for (alias, opt) in aliases.items():
Guido van Rossum8bc09652008-02-21 18:18:37 +0000122 if alias not in self.option_index:
Greg Ward1e7b5092000-04-21 04:22:01 +0000123 raise DistutilsGetoptError, \
124 ("invalid %s '%s': "
125 "option '%s' not defined") % (what, alias, alias)
Guido van Rossum8bc09652008-02-21 18:18:37 +0000126 if opt not in self.option_index:
Greg Ward1e7b5092000-04-21 04:22:01 +0000127 raise DistutilsGetoptError, \
128 ("invalid %s '%s': "
129 "aliased option '%s' not defined") % (what, alias, opt)
Fred Drakeb94b8492001-12-06 20:51:35 +0000130
Greg Ward1e7b5092000-04-21 04:22:01 +0000131 def set_aliases (self, alias):
132 """Set the aliases for this option parser."""
Greg Ward071ed762000-09-26 02:12:31 +0000133 self._check_alias_dict(alias, "alias")
Greg Ward1e7b5092000-04-21 04:22:01 +0000134 self.alias = alias
135
Greg Wardffc10d92000-04-21 01:41:54 +0000136 def set_negative_aliases (self, negative_alias):
137 """Set the negative aliases for this option parser.
138 'negative_alias' should be a dictionary mapping option names to
139 option names, both the key and value must already be defined
140 in the option table."""
Greg Ward071ed762000-09-26 02:12:31 +0000141 self._check_alias_dict(negative_alias, "negative alias")
Greg Wardffc10d92000-04-21 01:41:54 +0000142 self.negative_alias = negative_alias
143
144
145 def _grok_option_table (self):
Greg Ward071ed762000-09-26 02:12:31 +0000146 """Populate the various data structures that keep tabs on the
147 option table. Called by 'getopt()' before it can do anything
148 worthwhile.
149 """
Greg Ward0fd2dd62000-08-07 00:45:51 +0000150 self.long_opts = []
151 self.short_opts = []
152 self.short2long.clear()
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000153 self.repeat = {}
Greg Ward0fd2dd62000-08-07 00:45:51 +0000154
Greg Wardffc10d92000-04-21 01:41:54 +0000155 for option in self.option_table:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000156 if len(option) == 3:
157 long, short, help = option
158 repeat = 0
159 elif len(option) == 4:
160 long, short, help, repeat = option
161 else:
162 # the option table is part of the code, so simply
163 # assert that it is correct
Fred Drake576298d2004-08-02 17:58:51 +0000164 raise ValueError, "invalid option tuple: %r" % (option,)
Greg Wardffc10d92000-04-21 01:41:54 +0000165
166 # Type- and value-check the option names
167 if type(long) is not StringType or len(long) < 2:
168 raise DistutilsGetoptError, \
169 ("invalid long option '%s': "
170 "must be a string of length >= 2") % long
171
172 if (not ((short is None) or
Greg Ward071ed762000-09-26 02:12:31 +0000173 (type(short) is StringType and len(short) == 1))):
Greg Wardffc10d92000-04-21 01:41:54 +0000174 raise DistutilsGetoptError, \
175 ("invalid short option '%s': "
176 "must a single character or None") % short
177
Jeremy Hyltona181ec02002-06-04 20:24:05 +0000178 self.repeat[long] = repeat
Greg Ward071ed762000-09-26 02:12:31 +0000179 self.long_opts.append(long)
Greg Wardffc10d92000-04-21 01:41:54 +0000180
181 if long[-1] == '=': # option takes an argument?
182 if short: short = short + ':'
183 long = long[0:-1]
184 self.takes_arg[long] = 1
185 else:
186
187 # Is option is a "negative alias" for some other option (eg.
188 # "quiet" == "!verbose")?
189 alias_to = self.negative_alias.get(long)
190 if alias_to is not None:
191 if self.takes_arg[alias_to]:
192 raise DistutilsGetoptError, \
193 ("invalid negative alias '%s': "
194 "aliased option '%s' takes a value") % \
195 (long, alias_to)
196
197 self.long_opts[-1] = long # XXX redundant?!
198 self.takes_arg[long] = 0
199
200 else:
201 self.takes_arg[long] = 0
202
Greg Ward1e7b5092000-04-21 04:22:01 +0000203 # If this is an alias option, make sure its "takes arg" flag is
204 # the same as the option it's aliased to.
205 alias_to = self.alias.get(long)
206 if alias_to is not None:
207 if self.takes_arg[long] != self.takes_arg[alias_to]:
208 raise DistutilsGetoptError, \
209 ("invalid alias '%s': inconsistent with "
210 "aliased option '%s' (one of them takes a value, "
211 "the other doesn't") % (long, alias_to)
212
Greg Wardffc10d92000-04-21 01:41:54 +0000213
214 # Now enforce some bondage on the long option name, so we can
215 # later translate it to an attribute name on some object. Have
216 # to do this a bit late to make sure we've removed any trailing
217 # '='.
Greg Ward071ed762000-09-26 02:12:31 +0000218 if not longopt_re.match(long):
Greg Wardffc10d92000-04-21 01:41:54 +0000219 raise DistutilsGetoptError, \
220 ("invalid long option name '%s' " +
221 "(must be letters, numbers, hyphens only") % long
222
Greg Ward071ed762000-09-26 02:12:31 +0000223 self.attr_name[long] = self.get_attr_name(long)
Greg Wardffc10d92000-04-21 01:41:54 +0000224 if short:
Greg Ward071ed762000-09-26 02:12:31 +0000225 self.short_opts.append(short)
Greg Wardffc10d92000-04-21 01:41:54 +0000226 self.short2long[short[0]] = long
227
228 # for option_table
229
230 # _grok_option_table()
231
232
233 def getopt (self, args=None, object=None):
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000234 """Parse command-line options in args. Store as attributes on object.
235
236 If 'args' is None or not supplied, uses 'sys.argv[1:]'. If
237 'object' is None or not supplied, creates a new OptionDummy
238 object, stores option values there, and returns a tuple (args,
239 object). If 'object' is supplied, it is modified in place and
240 'getopt()' just returns 'args'; in both cases, the returned
241 'args' is a modified copy of the passed-in 'args' list, which
242 is left untouched.
Greg Ward071ed762000-09-26 02:12:31 +0000243 """
Greg Wardffc10d92000-04-21 01:41:54 +0000244 if args is None:
245 args = sys.argv[1:]
246 if object is None:
Greg Ward981f7362000-05-23 03:53:10 +0000247 object = OptionDummy()
Greg Wardffc10d92000-04-21 01:41:54 +0000248 created_object = 1
249 else:
250 created_object = 0
251
252 self._grok_option_table()
253
Greg Ward071ed762000-09-26 02:12:31 +0000254 short_opts = string.join(self.short_opts)
Greg Wardffc10d92000-04-21 01:41:54 +0000255 try:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000256 opts, args = getopt.getopt(args, short_opts, self.long_opts)
Greg Wardffc10d92000-04-21 01:41:54 +0000257 except getopt.error, msg:
258 raise DistutilsArgError, msg
259
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000260 for opt, val in opts:
Greg Ward071ed762000-09-26 02:12:31 +0000261 if len(opt) == 2 and opt[0] == '-': # it's a short option
Greg Wardffc10d92000-04-21 01:41:54 +0000262 opt = self.short2long[opt[1]]
Greg Wardffc10d92000-04-21 01:41:54 +0000263 else:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000264 assert len(opt) > 2 and opt[:2] == '--'
265 opt = opt[2:]
Greg Wardffc10d92000-04-21 01:41:54 +0000266
Greg Ward1e7b5092000-04-21 04:22:01 +0000267 alias = self.alias.get(opt)
268 if alias:
269 opt = alias
270
Greg Wardffc10d92000-04-21 01:41:54 +0000271 if not self.takes_arg[opt]: # boolean option?
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000272 assert val == '', "boolean option can't have value"
Greg Ward071ed762000-09-26 02:12:31 +0000273 alias = self.negative_alias.get(opt)
Greg Wardffc10d92000-04-21 01:41:54 +0000274 if alias:
275 opt = alias
276 val = 0
277 else:
278 val = 1
279
280 attr = self.attr_name[opt]
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000281 # The only repeating option at the moment is 'verbose'.
282 # It has a negative option -q quiet, which should set verbose = 0.
283 if val and self.repeat.get(attr) is not None:
284 val = getattr(object, attr, 0) + 1
Greg Ward071ed762000-09-26 02:12:31 +0000285 setattr(object, attr, val)
286 self.option_order.append((opt, val))
Greg Wardffc10d92000-04-21 01:41:54 +0000287
288 # for opts
Greg Wardffc10d92000-04-21 01:41:54 +0000289 if created_object:
Jeremy Hyltoncd8a1142002-06-04 20:14:43 +0000290 return args, object
Greg Wardffc10d92000-04-21 01:41:54 +0000291 else:
292 return args
293
294 # getopt()
295
296
Greg Ward283c7452000-04-21 02:09:26 +0000297 def get_option_order (self):
Greg Wardffc10d92000-04-21 01:41:54 +0000298 """Returns the list of (option, value) tuples processed by the
Greg Ward283c7452000-04-21 02:09:26 +0000299 previous run of 'getopt()'. Raises RuntimeError if
Greg Ward071ed762000-09-26 02:12:31 +0000300 'getopt()' hasn't been called yet.
301 """
Greg Wardffc10d92000-04-21 01:41:54 +0000302 if self.option_order is None:
Greg Ward283c7452000-04-21 02:09:26 +0000303 raise RuntimeError, "'getopt()' hasn't been called yet"
Greg Wardffc10d92000-04-21 01:41:54 +0000304 else:
305 return self.option_order
306
Greg Ward283c7452000-04-21 02:09:26 +0000307
Greg Ward66bf4462000-04-23 02:50:45 +0000308 def generate_help (self, header=None):
Greg Ward283c7452000-04-21 02:09:26 +0000309 """Generate help text (a list of strings, one per suggested line of
Greg Ward071ed762000-09-26 02:12:31 +0000310 output) from the option table for this FancyGetopt object.
311 """
Greg Ward283c7452000-04-21 02:09:26 +0000312 # Blithely assume the option table is good: probably wouldn't call
313 # 'generate_help()' unless you've already called 'getopt()'.
314
315 # First pass: determine maximum length of long option names
316 max_opt = 0
317 for option in self.option_table:
318 long = option[0]
319 short = option[1]
Greg Ward071ed762000-09-26 02:12:31 +0000320 l = len(long)
Greg Ward283c7452000-04-21 02:09:26 +0000321 if long[-1] == '=':
322 l = l - 1
323 if short is not None:
324 l = l + 5 # " (-x)" where short == 'x'
325 if l > max_opt:
326 max_opt = l
327
328 opt_width = max_opt + 2 + 2 + 2 # room for indent + dashes + gutter
329
330 # Typical help block looks like this:
331 # --foo controls foonabulation
332 # Help block for longest option looks like this:
333 # --flimflam set the flim-flam level
334 # and with wrapped text:
335 # --flimflam set the flim-flam level (must be between
336 # 0 and 100, except on Tuesdays)
337 # Options with short names will have the short name shown (but
338 # it doesn't contribute to max_opt):
339 # --foo (-f) controls foonabulation
340 # If adding the short option would make the left column too wide,
341 # we push the explanation off to the next line
342 # --flimflam (-l)
343 # set the flim-flam level
344 # Important parameters:
345 # - 2 spaces before option block start lines
346 # - 2 dashes for each long option name
347 # - min. 2 spaces between option and explanation (gutter)
348 # - 5 characters (incl. space) for short option name
349
350 # Now generate lines of help text. (If 80 columns were good enough
351 # for Jesus, then 78 columns are good enough for me!)
352 line_width = 78
353 text_width = line_width - opt_width
354 big_indent = ' ' * opt_width
355 if header:
356 lines = [header]
357 else:
358 lines = ['Option summary:']
359
Jeremy Hylton40ebbef2002-06-04 21:10:35 +0000360 for option in self.option_table:
Jeremy Hylton8f787bf2002-06-04 21:11:56 +0000361 long, short, help = option[:3]
Greg Ward071ed762000-09-26 02:12:31 +0000362 text = wrap_text(help, text_width)
Greg Ward283c7452000-04-21 02:09:26 +0000363 if long[-1] == '=':
364 long = long[0:-1]
365
366 # Case 1: no short option at all (makes life easy)
367 if short is None:
368 if text:
Greg Ward071ed762000-09-26 02:12:31 +0000369 lines.append(" --%-*s %s" % (max_opt, long, text[0]))
Greg Ward283c7452000-04-21 02:09:26 +0000370 else:
Greg Ward071ed762000-09-26 02:12:31 +0000371 lines.append(" --%-*s " % (max_opt, long))
Greg Ward283c7452000-04-21 02:09:26 +0000372
Greg Ward283c7452000-04-21 02:09:26 +0000373 # Case 2: we have a short option, so we have to include it
374 # just after the long option
375 else:
376 opt_names = "%s (-%s)" % (long, short)
377 if text:
Greg Ward071ed762000-09-26 02:12:31 +0000378 lines.append(" --%-*s %s" %
379 (max_opt, opt_names, text[0]))
Greg Ward283c7452000-04-21 02:09:26 +0000380 else:
Greg Ward071ed762000-09-26 02:12:31 +0000381 lines.append(" --%-*s" % opt_names)
Greg Ward283c7452000-04-21 02:09:26 +0000382
Greg Ward373dbfa2000-06-08 00:35:33 +0000383 for l in text[1:]:
Greg Ward071ed762000-09-26 02:12:31 +0000384 lines.append(big_indent + l)
Greg Ward373dbfa2000-06-08 00:35:33 +0000385
Greg Ward283c7452000-04-21 02:09:26 +0000386 # for self.option_table
387
388 return lines
389
390 # generate_help ()
391
Greg Ward66bf4462000-04-23 02:50:45 +0000392 def print_help (self, header=None, file=None):
Greg Ward283c7452000-04-21 02:09:26 +0000393 if file is None:
394 file = sys.stdout
Greg Ward071ed762000-09-26 02:12:31 +0000395 for line in self.generate_help(header):
396 file.write(line + "\n")
Greg Ward283c7452000-04-21 02:09:26 +0000397
Greg Wardffc10d92000-04-21 01:41:54 +0000398# class FancyGetopt
399
Greg Ward2689e3d1999-03-22 14:52:19 +0000400
Greg Ward44f8e4e1999-12-12 16:54:55 +0000401def fancy_getopt (options, negative_opt, object, args):
Greg Ward071ed762000-09-26 02:12:31 +0000402 parser = FancyGetopt(options)
403 parser.set_negative_aliases(negative_opt)
404 return parser.getopt(args, object)
Greg Wardffc10d92000-04-21 01:41:54 +0000405
406
Greg Ward071ed762000-09-26 02:12:31 +0000407WS_TRANS = string.maketrans(string.whitespace, ' ' * len(string.whitespace))
Greg Ward44f8e4e1999-12-12 16:54:55 +0000408
409def wrap_text (text, width):
Greg Ward46a69b92000-08-30 17:16:27 +0000410 """wrap_text(text : string, width : int) -> [string]
411
412 Split 'text' into multiple lines of no more than 'width' characters
413 each, and return the list of strings that results.
414 """
Greg Ward44f8e4e1999-12-12 16:54:55 +0000415
416 if text is None:
417 return []
Greg Ward071ed762000-09-26 02:12:31 +0000418 if len(text) <= width:
Greg Ward44f8e4e1999-12-12 16:54:55 +0000419 return [text]
420
Greg Ward071ed762000-09-26 02:12:31 +0000421 text = string.expandtabs(text)
422 text = string.translate(text, WS_TRANS)
423 chunks = re.split(r'( +|-+)', text)
424 chunks = filter(None, chunks) # ' - ' results in empty strings
Greg Ward44f8e4e1999-12-12 16:54:55 +0000425 lines = []
426
427 while chunks:
428
429 cur_line = [] # list of chunks (to-be-joined)
430 cur_len = 0 # length of current line
431
432 while chunks:
Greg Ward071ed762000-09-26 02:12:31 +0000433 l = len(chunks[0])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000434 if cur_len + l <= width: # can squeeze (at least) this chunk in
Greg Ward071ed762000-09-26 02:12:31 +0000435 cur_line.append(chunks[0])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000436 del chunks[0]
437 cur_len = cur_len + l
438 else: # this line is full
439 # drop last chunk if all space
440 if cur_line and cur_line[-1][0] == ' ':
441 del cur_line[-1]
442 break
443
444 if chunks: # any chunks left to process?
445
446 # if the current line is still empty, then we had a single
447 # chunk that's too big too fit on a line -- so we break
448 # down and break it up at the line width
449 if cur_len == 0:
Greg Ward071ed762000-09-26 02:12:31 +0000450 cur_line.append(chunks[0][0:width])
Greg Ward44f8e4e1999-12-12 16:54:55 +0000451 chunks[0] = chunks[0][width:]
452
453 # all-whitespace chunks at the end of a line can be discarded
454 # (and we know from the re.split above that if a chunk has
455 # *any* whitespace, it is *all* whitespace)
456 if chunks[0][0] == ' ':
457 del chunks[0]
458
459 # and store this line in the list-of-all-lines -- as a single
460 # string, of course!
Greg Ward071ed762000-09-26 02:12:31 +0000461 lines.append(string.join(cur_line, ''))
Greg Ward44f8e4e1999-12-12 16:54:55 +0000462
463 # while chunks
464
465 return lines
466
467# wrap_text ()
Greg Ward68ded6e2000-09-25 01:58:31 +0000468
469
470def translate_longopt (opt):
471 """Convert a long option name to a valid Python identifier by
472 changing "-" to "_".
473 """
474 return string.translate(opt, longopt_xlate)
Fred Drakeb94b8492001-12-06 20:51:35 +0000475
Greg Ward44f8e4e1999-12-12 16:54:55 +0000476
Greg Wardffc10d92000-04-21 01:41:54 +0000477class OptionDummy:
478 """Dummy class just used as a place to hold command-line option
479 values as instance attributes."""
Greg Ward3c67b1d2000-05-23 01:44:20 +0000480
481 def __init__ (self, options=[]):
482 """Create a new OptionDummy instance. The attributes listed in
483 'options' will be initialized to None."""
484 for opt in options:
485 setattr(self, opt, None)
486
487# class OptionDummy
Fred Drakeb94b8492001-12-06 20:51:35 +0000488
Greg Ward44f8e4e1999-12-12 16:54:55 +0000489
490if __name__ == "__main__":
491 text = """\
492Tra-la-la, supercalifragilisticexpialidocious.
493How *do* you spell that odd word, anyways?
494(Someone ask Mary -- she'll know [or she'll
495say, "How should I know?"].)"""
496
497 for w in (10, 20, 30, 40):
498 print "width: %d" % w
Greg Ward071ed762000-09-26 02:12:31 +0000499 print string.join(wrap_text(text, w), "\n")
Greg Ward44f8e4e1999-12-12 16:54:55 +0000500 print