annotate Lib/distutils/fancy_getopt.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 3e2b7965ada9
children
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
57847
3e2b7965ada9 massive import cleaning in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
13 import sys
3e2b7965ada9 massive import cleaning in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
14 import string
3e2b7965ada9 massive import cleaning in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
15 import re
10435
324450ea5450 First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff changeset
16 import getopt
57847
3e2b7965ada9 massive import cleaning in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
17 from distutils.errors import DistutilsGetoptError, DistutilsArgError
10435
324450ea5450 First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff changeset
18
324450ea5450 First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff changeset
19 # 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
20 # 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
21 # 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
22 # 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
23 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
24 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
25
dfd2c7d34d44 Hacked to support the notion of "negative alias" options, to handle
Greg Ward <gward@python.net>
parents: 11194
diff changeset
26 # For recognizing "negative alias" options, eg. "quiet=!verbose"
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
27 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
28
10435
324450ea5450 First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff changeset
29 # 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
30 # (for use as attributes of some object).
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
31 longopt_xlate = string.maketrans('-', '_')
10435
324450ea5450 First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff changeset
32
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
33 class FancyGetopt:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
34 """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
35 handy extra functionality:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
36 * 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
37 * 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
38 from them
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
39 * 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
40 * 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
41 --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
42 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
43 """
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
44
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
45 def __init__ (self, option_table=None):
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
46
32903
33b8ce66202e - fix description of option table entries
Fred Drake <fdrake@acm.org>
parents: 31515
diff changeset
47 # 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
48 # tuples may have 3 or four values:
33b8ce66202e - fix description of option table entries
Fred Drake <fdrake@acm.org>
parents: 31515
diff changeset
49 # (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
50 # 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
51 # 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
52 # 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
53 # 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
54 # must have long options.
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
55 self.option_table = option_table
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
56
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
57 # '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
58 # table (ie. those 3-tuples).
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
59 self.option_index = {}
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
60 if self.option_table:
12426
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
61 self._build_index()
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
62
12429
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
63 # 'alias' records (duh) alias options; {'foo': 'bar'} means
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
64 # --foo is an alias for --bar
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
65 self.alias = {}
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
66
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
67 # '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
68 # opposite of some other option
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
69 self.negative_alias = {}
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19114
diff changeset
70
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
71 # 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
72 # 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
73 # 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
74 # isn't necessarily the final word.
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
75 self.short_opts = []
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
76 self.long_opts = []
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
77 self.short2long = {}
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
78 self.attr_name = {}
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
79 self.takes_arg = {}
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
80
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
81 # 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
82 # 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
83 # 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
84 self.option_order = []
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
85
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
86 # __init__ ()
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19114
diff changeset
87
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
88
12426
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
89 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
90 self.option_index.clear()
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
91 for option in self.option_table:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
92 self.option_index[option[0]] = option
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
93
12426
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
94 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
95 self.option_table = option_table
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
96 self._build_index()
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
97
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
98 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
99 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
100 raise DistutilsGetoptError, \
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
101 "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
102 else:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
103 option = (long_option, short_option, help_string)
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
104 self.option_table.append(option)
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
105 self.option_index[long_option] = option
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
106
12428
6b2b9a23a41f Added 'has_option()', 'get_attr_name()' methods.
Greg Ward <gward@python.net>
parents: 12426
diff changeset
107
6b2b9a23a41f Added 'has_option()', 'get_attr_name()' methods.
Greg Ward <gward@python.net>
parents: 12426
diff changeset
108 def has_option (self, long_option):
6b2b9a23a41f Added 'has_option()', 'get_attr_name()' methods.
Greg Ward <gward@python.net>
parents: 12426
diff changeset
109 """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
110 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
111 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
112
6b2b9a23a41f Added 'has_option()', 'get_attr_name()' methods.
Greg Ward <gward@python.net>
parents: 12426
diff changeset
113 def get_attr_name (self, long_option):
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19114
diff changeset
114 """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
115 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
116 to underscores."""
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
117 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
118
6b2b9a23a41f Added 'has_option()', 'get_attr_name()' methods.
Greg Ward <gward@python.net>
parents: 12426
diff changeset
119
12429
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
120 def _check_alias_dict (self, aliases, what):
57847
3e2b7965ada9 massive import cleaning in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
121 assert isinstance(aliases, dict)
12429
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
122 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
123 if alias not in self.option_index:
12429
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
124 raise DistutilsGetoptError, \
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
125 ("invalid %s '%s': "
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
126 "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
127 if opt not in self.option_index:
12429
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
128 raise DistutilsGetoptError, \
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
129 ("invalid %s '%s': "
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
130 "aliased option '%s' not defined") % (what, alias, opt)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19114
diff changeset
131
12429
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
132 def set_aliases (self, alias):
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
133 """Set the aliases for this option parser."""
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
134 self._check_alias_dict(alias, "alias")
12429
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
135 self.alias = alias
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
136
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
137 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
138 """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
139 '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
140 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
141 in the option table."""
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
142 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
143 self.negative_alias = negative_alias
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
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
146 def _grok_option_table (self):
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
147 """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
148 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
149 worthwhile.
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
150 """
13879
0f68018cce58 Fix so we clear or reinitialize various data structures before populating
Greg Ward <gward@python.net>
parents: 12890
diff changeset
151 self.long_opts = []
0f68018cce58 Fix so we clear or reinitialize various data structures before populating
Greg Ward <gward@python.net>
parents: 12890
diff changeset
152 self.short_opts = []
0f68018cce58 Fix so we clear or reinitialize various data structures before populating
Greg Ward <gward@python.net>
parents: 12890
diff changeset
153 self.short2long.clear()
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
154 self.repeat = {}
13879
0f68018cce58 Fix so we clear or reinitialize various data structures before populating
Greg Ward <gward@python.net>
parents: 12890
diff changeset
155
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
156 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
157 if len(option) == 3:
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
158 long, short, help = option
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
159 repeat = 0
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
160 elif len(option) == 4:
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
161 long, short, help, repeat = option
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
162 else:
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
163 # 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
164 # assert that it is correct
32903
33b8ce66202e - fix description of option table entries
Fred Drake <fdrake@acm.org>
parents: 31515
diff changeset
165 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
166
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
167 # Type- and value-check the option names
57847
3e2b7965ada9 massive import cleaning in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
168 if not isinstance(long, str) or len(long) < 2:
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
169 raise DistutilsGetoptError, \
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
170 ("invalid long option '%s': "
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
171 "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
172
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
173 if (not ((short is None) or
57847
3e2b7965ada9 massive import cleaning in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
174 (isinstance(short, str) and len(short) == 1))):
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
175 raise DistutilsGetoptError, \
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
176 ("invalid short option '%s': "
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
177 "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
178
23670
c3dba91afa6d Set repeat metadata for an option based on repeat local var not
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23668
diff changeset
179 self.repeat[long] = repeat
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
180 self.long_opts.append(long)
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
181
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
182 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
183 if short: short = short + ':'
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
184 long = long[0:-1]
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
185 self.takes_arg[long] = 1
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
186 else:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
187
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
188 # 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
189 # "quiet" == "!verbose")?
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
190 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
191 if alias_to is not None:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
192 if self.takes_arg[alias_to]:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
193 raise DistutilsGetoptError, \
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
194 ("invalid negative alias '%s': "
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
195 "aliased option '%s' takes a value") % \
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
196 (long, alias_to)
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
197
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
198 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
199 self.takes_arg[long] = 0
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
200
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
201 else:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
202 self.takes_arg[long] = 0
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
203
12429
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
204 # 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
205 # 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
206 alias_to = self.alias.get(long)
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
207 if alias_to is not None:
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
208 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
209 raise DistutilsGetoptError, \
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
210 ("invalid alias '%s': inconsistent with "
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
211 "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
212 "the other doesn't") % (long, alias_to)
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
213
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
214
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
215 # 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
216 # 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
217 # 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
218 # '='.
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
219 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
220 raise DistutilsGetoptError, \
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
221 ("invalid long option name '%s' " +
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
222 "(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
223
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
224 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
225 if short:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
226 self.short_opts.append(short)
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
227 self.short2long[short[0]] = long
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
228
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
229 # for option_table
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
230
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
231 # _grok_option_table()
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
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
234 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
235 """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
236
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
237 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
238 '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
239 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
240 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
241 '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
242 '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
243 is left untouched.
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
244 """
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
245 if args is None:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
246 args = sys.argv[1:]
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
247 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
248 object = OptionDummy()
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
249 created_object = 1
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
250 else:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
251 created_object = 0
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
252
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
253 self._grok_option_table()
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
254
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
255 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
256 try:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
257 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
258 except getopt.error, msg:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
259 raise DistutilsArgError, msg
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
260
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
261 for opt, val in opts:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
262 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
263 opt = self.short2long[opt[1]]
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
264 else:
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
265 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
266 opt = opt[2:]
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
267
12429
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
268 alias = self.alias.get(opt)
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
269 if alias:
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
270 opt = alias
d9400c476b9d Added the capability for alias options.
Greg Ward <gward@python.net>
parents: 12428
diff changeset
271
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
272 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
273 assert val == '', "boolean option can't have value"
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
274 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
275 if alias:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
276 opt = alias
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
277 val = 0
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
278 else:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
279 val = 1
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
280
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
281 attr = self.attr_name[opt]
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
282 # 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
283 # 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
284 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
285 val = getattr(object, attr, 0) + 1
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
286 setattr(object, attr, val)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
287 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
288
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
289 # for opts
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
290 if created_object:
23668
ca87119255eb Make setup.py less chatty by default.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 21267
diff changeset
291 return args, object
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
292 else:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
293 return args
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
294
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
295 # getopt()
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
296
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
297
12426
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
298 def get_option_order (self):
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
299 """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
300 previous run of 'getopt()'. Raises RuntimeError if
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
301 'getopt()' hasn't been called yet.
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
302 """
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
303 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
304 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
305 else:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
306 return self.option_order
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
307
12426
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
308
12461
fd0e5daef86a Patch from Harry Henry Gebel: fix two stupid bugs in help-printing stuff.
Greg Ward <gward@python.net>
parents: 12429
diff changeset
309 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
310 """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
311 output) from the option table for this FancyGetopt object.
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
312 """
12426
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
313 # 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
314 # '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
315
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
316 # 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
317 max_opt = 0
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
318 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
319 long = option[0]
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
320 short = option[1]
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
321 l = len(long)
12426
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
322 if long[-1] == '=':
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
323 l = l - 1
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
324 if short is not None:
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
325 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
326 if l > max_opt:
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
327 max_opt = l
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
328
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
329 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
330
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
331 # 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
332 # --foo controls foonabulation
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
333 # 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
334 # --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
335 # and with wrapped text:
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
336 # --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
337 # 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
338 # 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
339 # 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
340 # --foo (-f) controls foonabulation
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
341 # 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
342 # 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
343 # --flimflam (-l)
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
344 # set the flim-flam level
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
345 # Important parameters:
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
346 # - 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
347 # - 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
348 # - 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
349 # - 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
350
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
351 # 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
352 # 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
353 line_width = 78
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
354 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
355 big_indent = ' ' * opt_width
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
356 if header:
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
357 lines = [header]
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
358 else:
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
359 lines = ['Option summary:']
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
360
23685
8a9a3aff32fc Track extra arg to option_table to all uses of it
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23673
diff changeset
361 for option in self.option_table:
23686
14938e76276a Test changes before checking them in.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23685
diff changeset
362 long, short, help = option[:3]
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
363 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
364 if long[-1] == '=':
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
365 long = long[0:-1]
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
366
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
367 # 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
368 if short is None:
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
369 if text:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
370 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
371 else:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
372 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
373
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
374 # 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
375 # just after the long option
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
376 else:
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
377 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
378 if text:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
379 lines.append(" --%-*s %s" %
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
380 (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
381 else:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
382 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
383
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
384 for l in text[1:]:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
385 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
386
12426
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
387 # for self.option_table
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
388
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
389 return lines
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
390
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
391 # generate_help ()
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
392
12461
fd0e5daef86a Patch from Harry Henry Gebel: fix two stupid bugs in help-printing stuff.
Greg Ward <gward@python.net>
parents: 12429
diff changeset
393 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
394 if file is None:
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
395 file = sys.stdout
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
396 for line in self.generate_help(header):
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
397 file.write(line + "\n")
12426
e87e6bdfcbdc Made 'generate_help()' and 'print_help()' methods of FancyGetopt.
Greg Ward <gward@python.net>
parents: 12425
diff changeset
398
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
399 # class FancyGetopt
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
400
10435
324450ea5450 First checkin of real Distutils code.
Greg Ward <gward@python.net>
parents:
diff changeset
401
11542
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
402 def fancy_getopt (options, negative_opt, object, args):
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
403 parser = FancyGetopt(options)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
404 parser.set_negative_aliases(negative_opt)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
405 return parser.getopt(args, object)
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
406
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
407
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
408 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
409
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
410 def wrap_text (text, width):
14266
d3f92f3fc0b0 Added docstring for 'wrap()' function.
Greg Ward <gward@python.net>
parents: 13879
diff changeset
411 """wrap_text(text : string, width : int) -> [string]
d3f92f3fc0b0 Added docstring for 'wrap()' function.
Greg Ward <gward@python.net>
parents: 13879
diff changeset
412
d3f92f3fc0b0 Added docstring for 'wrap()' function.
Greg Ward <gward@python.net>
parents: 13879
diff changeset
413 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
414 each, and return the list of strings that results.
d3f92f3fc0b0 Added docstring for 'wrap()' function.
Greg Ward <gward@python.net>
parents: 13879
diff changeset
415 """
11542
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
416
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
417 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
418 return []
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
419 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
420 return [text]
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
421
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
422 text = string.expandtabs(text)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
423 text = string.translate(text, WS_TRANS)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
424 chunks = re.split(r'( +|-+)', text)
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
425 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
426 lines = []
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
427
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
428 while chunks:
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
429
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
430 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
431 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
432
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
433 while chunks:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
434 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
435 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
436 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
437 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
438 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
439 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
440 # 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
441 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
442 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
443 break
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
444
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
445 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
446
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
447 # 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
448 # 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
449 # 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
450 if cur_len == 0:
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
451 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
452 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
453
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
454 # 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
455 # (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
456 # *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
457 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
458 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
459
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
460 # 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
461 # string, of course!
14863
c3f0d6f19f7b Standardize whitespace in function calls.
Greg Ward <gward@python.net>
parents: 14833
diff changeset
462 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
463
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
464 # while chunks
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
465
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
466 return lines
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
467
14833
ee813e843f3e Added 'translate_longopt()' function.
Greg Ward <gward@python.net>
parents: 14266
diff changeset
468
57847
3e2b7965ada9 massive import cleaning in Distutils
Tarek Ziadé <ziade.tarek@gmail.com>
parents: 51047
diff changeset
469 def translate_longopt(opt):
14833
ee813e843f3e Added 'translate_longopt()' function.
Greg Ward <gward@python.net>
parents: 14266
diff changeset
470 """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
471 changing "-" to "_".
ee813e843f3e Added 'translate_longopt()' function.
Greg Ward <gward@python.net>
parents: 14266
diff changeset
472 """
ee813e843f3e Added 'translate_longopt()' function.
Greg Ward <gward@python.net>
parents: 14266
diff changeset
473 return string.translate(opt, longopt_xlate)
21267
7a7f557b4d54 Whitespace normalization.
Fred Drake <fdrake@acm.org>
parents: 19114
diff changeset
474
11542
18fc08fc9fd4 Added support for printing out help text from option table: 'print_help()',
Greg Ward <gward@python.net>
parents: 11361
diff changeset
475
12424
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
476 class OptionDummy:
0ae63523d504 Hefty refactoring: converted 'fancy_getopt()' function into FancyGetopt
Greg Ward <gward@python.net>
parents: 11897
diff changeset
477 """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
478 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
479
4fd845922c49 OptionDummy now has a constructor that takes a list of options: each string
Greg Ward <gward@python.net>
parents: 12461
diff changeset
480 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
481 """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
482 '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
483 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
484 setattr(self, opt, None)