annotate Lib/distutils/fancy_getopt.py @ 54294:dba767bc3155 legacy-trunk

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