annotate Lib/optparse.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents a9ee869cae40
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
1 """A powerful, extensible, and easy-to-use option parser.
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 By Greg Ward <gward@python.net>
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
5 Originally distributed as Optik.
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
6
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
7 For support, use the optik-users@lists.sourceforge.net mailing list
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
8 (http://lists.sourceforge.net/lists/listinfo/optik-users).
53523
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
9
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
10 Simple usage example:
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
11
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
12 from optparse import OptionParser
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
13
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
14 parser = OptionParser()
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
15 parser.add_option("-f", "--file", dest="filename",
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
16 help="write report to FILE", metavar="FILE")
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
17 parser.add_option("-q", "--quiet",
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
18 action="store_false", dest="verbose", default=True,
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
19 help="don't print status messages to stdout")
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
20
ea4e192d9414 Merged revisions 71540,71563 via svnmerge from
Georg Brandl <georg@python.org>
parents: 52892
diff changeset
21 (options, args) = parser.parse_args()
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
24 __version__ = "1.5.3"
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
25
29021
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
26 __all__ = ['Option',
52892
792bb885029f Merged revisions 70712,70714,70764-70765,70769-70771,70773,70776-70777,70788-70789,70824,70828,70832,70836,70842,70851,70855,70857,70866-70872,70883,70885,70893-70894,70896-70897,70903,70905-70907,70915,70927,70933,70951,70960,70962-70964,70998,71001,71006,71008,71010-71011,71019,71037,71056,71094,71101-71103,71106,71119,71123,71149-71150,71203,71212,71214-71217,71221,71240 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 51776
diff changeset
27 'make_option',
29021
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
28 'SUPPRESS_HELP',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
29 'SUPPRESS_USAGE',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
30 'Values',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
31 'OptionContainer',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
32 'OptionGroup',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
33 'OptionParser',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
34 'HelpFormatter',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
35 'IndentedHelpFormatter',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
36 'TitledHelpFormatter',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
37 'OptParseError',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
38 'OptionError',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
39 'OptionConflictError',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
40 'OptionValueError',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
41 'BadOptionError']
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
42
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 __copyright__ = """
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
44 Copyright (c) 2001-2006 Gregory P. Ward. All rights reserved.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
45 Copyright (c) 2002-2006 Python Software Foundation. All rights reserved.
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 Redistribution and use in source and binary forms, with or without
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 modification, are permitted provided that the following conditions are
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 met:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 * Redistributions of source code must retain the above copyright
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 notice, this list of conditions and the following disclaimer.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 * Redistributions in binary form must reproduce the above copyright
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 notice, this list of conditions and the following disclaimer in the
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 documentation and/or other materials provided with the distribution.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 * Neither the name of the author nor the names of its
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 contributors may be used to endorse or promote products derived from
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 this software without specific prior written permission.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 import sys, os
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 import textwrap
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
77
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
78 def _repr(self):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
79 return "<%s at 0x%x: %s>" % (self.__class__.__name__, id(self), self)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
80
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
81
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
82 # This file was generated from:
39442
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
83 # Id: option_parser.py 527 2006-07-23 15:21:30Z greg
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
84 # Id: option.py 522 2006-06-11 16:22:03Z gward
654c380cf8b9 Merged revisions 46753-51188 via svnmerge from
Thomas Wouters <thomas@python.org>
parents: 38669
diff changeset
85 # Id: help.py 527 2006-07-23 15:21:30Z greg
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
86 # Id: errors.py 509 2006-04-20 00:58:24Z gward
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
87
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
88 try:
68767
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
89 from gettext import gettext, ngettext
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
90 except ImportError:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
91 def gettext(message):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
92 return message
68767
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
93
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
94 def ngettext(singular, plural, n):
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
95 if n == 1:
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
96 return singular
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
97 return plural
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
98
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
99 _ = gettext
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
100
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
101
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 class OptParseError (Exception):
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
103 def __init__(self, msg):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 self.msg = msg
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
105
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
106 def __str__(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 return self.msg
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
109
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 class OptionError (OptParseError):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 Raised if an Option instance is created with invalid or
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 inconsistent arguments.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
115
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
116 def __init__(self, msg, option):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
117 self.msg = msg
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 self.option_id = str(option)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
119
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
120 def __str__(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 if self.option_id:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 return "option %s: %s" % (self.option_id, self.msg)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 return self.msg
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 class OptionConflictError (OptionError):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 Raised if conflicting options are added to an OptionParser.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
130
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 class OptionValueError (OptParseError):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 Raised if an invalid option value is encountered on the command
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 line.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 class BadOptionError (OptParseError):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 """
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
139 Raised if an invalid option is seen on the command line.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
140 """
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
141 def __init__(self, opt_str):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
142 self.opt_str = opt_str
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
143
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
144 def __str__(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
145 return _("no such option: %s") % self.opt_str
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
146
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
147 class AmbiguousOptionError (BadOptionError):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 """
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
149 Raised if an ambiguous option is seen on the command line.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
150 """
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
151 def __init__(self, opt_str, possibilities):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
152 BadOptionError.__init__(self, opt_str)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
153 self.possibilities = possibilities
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
154
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
155 def __str__(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
156 return (_("ambiguous option: %s (%s?)")
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
157 % (self.opt_str, ", ".join(self.possibilities)))
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
158
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
159
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 class HelpFormatter:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
161
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 Abstract base class for formatting option help. OptionParser
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 instances should use one of the HelpFormatter subclasses for
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 formatting help; by default IndentedHelpFormatter is used.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
166
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 Instance attributes:
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
168 parser : OptionParser
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
169 the controlling OptionParser instance
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 indent_increment : int
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 the number of columns to indent per nesting level
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 max_help_position : int
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 the maximum starting column for option help text
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 help_position : int
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 the calculated starting column for option help text;
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 initially the same as the maximum
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 width : int
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
178 total number of columns for output (pass None to constructor for
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
179 this value to be taken from the $COLUMNS environment variable)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 level : int
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 current indentation level
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 current_indent : int
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 current indentation level (in columns)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 help_width : int
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 number of columns available for option help text (calculated)
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
186 default_tag : str
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
187 text to replace with each option's default value, "%default"
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
188 by default. Set to false value to disable default value expansion.
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
189 option_strings : { Option : str }
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
190 maps Option instances to the snippet of help text explaining
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
191 the syntax of that option, e.g. "-h, --help" or
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
192 "-fFILE, --file=FILE"
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
193 _short_opt_fmt : str
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
194 format string controlling how short options with values are
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
195 printed in help text. Must be either "%s%s" ("-fFILE") or
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
196 "%s %s" ("-f FILE"), because those are the two syntaxes that
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
197 Optik supports.
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
198 _long_opt_fmt : str
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
199 similar but for long options; must be either "%s %s" ("--file FILE")
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
200 or "%s=%s" ("--file=FILE").
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
203 NO_DEFAULT_VALUE = "none"
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
204
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
205 def __init__(self,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
206 indent_increment,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
207 max_help_position,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
208 width,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
209 short_first):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
210 self.parser = None
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
211 self.indent_increment = indent_increment
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 self.help_position = self.max_help_position = max_help_position
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
213 if width is None:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
214 try:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
215 width = int(os.environ['COLUMNS'])
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
216 except (KeyError, ValueError):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
217 width = 80
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
218 width -= 2
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 self.width = width
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 self.current_indent = 0
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 self.level = 0
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
222 self.help_width = None # computed later
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
223 self.short_first = short_first
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
224 self.default_tag = "%default"
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
225 self.option_strings = {}
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
226 self._short_opt_fmt = "%s %s"
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
227 self._long_opt_fmt = "%s=%s"
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
228
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
229 def set_parser(self, parser):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
230 self.parser = parser
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
231
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
232 def set_short_opt_delimiter(self, delim):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
233 if delim not in ("", " "):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
234 raise ValueError(
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
235 "invalid metavar delimiter for short options: %r" % delim)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
236 self._short_opt_fmt = "%s" + delim + "%s"
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
237
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
238 def set_long_opt_delimiter(self, delim):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
239 if delim not in ("=", " "):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
240 raise ValueError(
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
241 "invalid metavar delimiter for long options: %r" % delim)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
242 self._long_opt_fmt = "%s" + delim + "%s"
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
243
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
244 def indent(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 self.current_indent += self.indent_increment
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 self.level += 1
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
247
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
248 def dedent(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 self.current_indent -= self.indent_increment
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
250 assert self.current_indent >= 0, "Indent decreased below 0."
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 self.level -= 1
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
252
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
253 def format_usage(self, usage):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42552
diff changeset
254 raise NotImplementedError("subclasses must implement")
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
256 def format_heading(self, heading):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42552
diff changeset
257 raise NotImplementedError("subclasses must implement")
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
258
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
259 def _format_text(self, text):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
260 """
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
261 Format a paragraph of free-form text for inclusion in the
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
262 help output at the current indentation level.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
263 """
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
264 text_width = self.width - self.current_indent
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 indent = " "*self.current_indent
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
266 return textwrap.fill(text,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
267 text_width,
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 initial_indent=indent,
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
269 subsequent_indent=indent)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
270
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
271 def format_description(self, description):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
272 if description:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
273 return self._format_text(description) + "\n"
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
274 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
275 return ""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
276
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
277 def format_epilog(self, epilog):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
278 if epilog:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
279 return "\n" + self._format_text(epilog) + "\n"
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
280 else:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
281 return ""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
282
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
283
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
284 def expand_default(self, option):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
285 if self.parser is None or not self.default_tag:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
286 return option.help
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
287
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
288 default_value = self.parser.defaults.get(option.dest)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
289 if default_value is NO_DEFAULT or default_value is None:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
290 default_value = self.NO_DEFAULT_VALUE
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
291
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
292 return option.help.replace(self.default_tag, str(default_value))
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
293
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
294 def format_option(self, option):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 # The help for each option consists of two parts:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
296 # * the opt strings and metavars
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
297 # eg. ("-x", or "-fFILENAME, --file=FILENAME")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 # * the user-supplied help string
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 # eg. ("turn on expert mode", "read data from FILENAME")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 # If possible, we write both of these on the same line:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 # -x turn on expert mode
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
303 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 # But if the opt string list is too long, we put the help
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
305 # string on a second line, indented to the same column it would
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 # start in if it fit on the first line.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
307 # -fFILENAME, --file=FILENAME
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
308 # read data from FILENAME
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 result = []
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
310 opts = self.option_strings[option]
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 opt_width = self.help_position - self.current_indent - 2
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 if len(opts) > opt_width:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 opts = "%*s%s\n" % (self.current_indent, "", opts)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
314 indent_first = self.help_position
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 else: # start help on same line as opts
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
316 opts = "%*s%-*s " % (self.current_indent, "", opt_width, opts)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 indent_first = 0
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 result.append(opts)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 if option.help:
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
320 help_text = self.expand_default(option)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
321 help_lines = textwrap.wrap(help_text, self.help_width)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 result.extend(["%*s%s\n" % (self.help_position, "", line)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 for line in help_lines[1:]])
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 elif opts[-1] != "\n":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 result.append("\n")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 return "".join(result)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
328
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
329 def store_option_strings(self, parser):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 self.indent()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 max_len = 0
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 for opt in parser.option_list:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 strings = self.format_option_strings(opt)
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
334 self.option_strings[opt] = strings
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 max_len = max(max_len, len(strings) + self.current_indent)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 self.indent()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 for group in parser.option_groups:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 for opt in group.option_list:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 strings = self.format_option_strings(opt)
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
340 self.option_strings[opt] = strings
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 max_len = max(max_len, len(strings) + self.current_indent)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 self.dedent()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 self.dedent()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 self.help_position = min(max_len + 2, self.max_help_position)
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
345 self.help_width = self.width - self.help_position
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
346
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
347 def format_option_strings(self, option):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 """Return a comma-separated list of option strings & metavariables."""
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
349 if option.takes_value():
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 metavar = option.metavar or option.dest.upper()
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
351 short_opts = [self._short_opt_fmt % (sopt, metavar)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
352 for sopt in option._short_opts]
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
353 long_opts = [self._long_opt_fmt % (lopt, metavar)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
354 for lopt in option._long_opts]
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 else:
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
356 short_opts = option._short_opts
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
357 long_opts = option._long_opts
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
358
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
359 if self.short_first:
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
360 opts = short_opts + long_opts
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 else:
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
362 opts = long_opts + short_opts
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
363
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 return ", ".join(opts)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
365
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 class IndentedHelpFormatter (HelpFormatter):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
367 """Format help with indented section bodies.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
369
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
370 def __init__(self,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
371 indent_increment=2,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
372 max_help_position=24,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
373 width=None,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
374 short_first=1):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 HelpFormatter.__init__(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 self, indent_increment, max_help_position, width, short_first)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
377
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
378 def format_usage(self, usage):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
379 return _("Usage: %s\n") % usage
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
380
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
381 def format_heading(self, heading):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 return "%*s%s:\n" % (self.current_indent, "", heading)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
383
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
384
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 class TitledHelpFormatter (HelpFormatter):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 """Format help with underlined section headers.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
388
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
389 def __init__(self,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
390 indent_increment=0,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
391 max_help_position=24,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
392 width=None,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
393 short_first=0):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 HelpFormatter.__init__ (
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 self, indent_increment, max_help_position, width, short_first)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
396
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
397 def format_usage(self, usage):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
398 return "%s %s\n" % (self.format_heading(_("Usage")), usage)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
399
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
400 def format_heading(self, heading):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 return "%s\n%s\n" % (heading, "=-"[self.level] * len(heading))
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
402
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
403
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
404 def _parse_num(val, type):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
405 if val[:2].lower() == "0x": # hexadecimal
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
406 radix = 16
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
407 elif val[:2].lower() == "0b": # binary
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
408 radix = 2
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
409 val = val[2:] or "0" # have to remove "0b" prefix
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
410 elif val[:1] == "0": # octal
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
411 radix = 8
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
412 else: # decimal
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
413 radix = 10
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
414
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
415 return type(val, radix)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
416
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
417 def _parse_int(val):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
418 return _parse_num(val, int)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
419
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
420 _builtin_cvt = { "int" : (_parse_int, _("integer")),
73076
67053b135ed9 Cleanup code: remove int/long idioms and simplify a while statement.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 69841
diff changeset
421 "long" : (_parse_int, _("integer")),
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
422 "float" : (float, _("floating-point")),
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
423 "complex" : (complex, _("complex")) }
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
424
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
425 def check_builtin(option, opt, value):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 (cvt, what) = _builtin_cvt[option.type]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
427 try:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 return cvt(value)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
429 except ValueError:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
430 raise OptionValueError(
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
431 _("option %s: invalid %s value: %r") % (opt, what, value))
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
432
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 def check_choice(option, opt, value):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
434 if value in option.choices:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
435 return value
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
436 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
437 choices = ", ".join(map(repr, option.choices))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
438 raise OptionValueError(
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
439 _("option %s: invalid choice: %r (choose from %s)")
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
440 % (opt, value, choices))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
441
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 # Not supplying a default is different from a default of None,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
443 # so we need an explicit "not supplied" value.
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
444 NO_DEFAULT = ("NO", "DEFAULT")
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
445
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
446
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
447 class Option:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
448 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 Instance attributes:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
450 _short_opts : [string]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 _long_opts : [string]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
452
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 action : string
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
454 type : string
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 dest : string
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
456 default : any
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 nargs : int
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
458 const : any
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
459 choices : [string]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 callback : function
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
461 callback_args : (any*)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 callback_kwargs : { string : any }
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
463 help : string
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
464 metavar : string
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
465 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
466
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 # The list of instance attributes that may be set through
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 # keyword args to the constructor.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 ATTRS = ['action',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
470 'type',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 'dest',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
472 'default',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
473 'nargs',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
474 'const',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 'choices',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
476 'callback',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
477 'callback_args',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
478 'callback_kwargs',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
479 'help',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 'metavar']
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
481
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
482 # The set of actions allowed by option parsers. Explicitly listed
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
483 # here so the constructor can validate its arguments.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 ACTIONS = ("store",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
485 "store_const",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 "store_true",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
487 "store_false",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
488 "append",
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
489 "append_const",
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 "count",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 "callback",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
492 "help",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 "version")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
494
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 # The set of actions that involve storing a value somewhere;
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
496 # also listed just for constructor argument validation. (If
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
497 # the action is one of these, there must be a destination.)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 STORE_ACTIONS = ("store",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
499 "store_const",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
500 "store_true",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
501 "store_false",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
502 "append",
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
503 "append_const",
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 "count")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
505
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 # The set of actions for which it makes sense to supply a value
33846
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
507 # type, ie. which may consume an argument from the command line.
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
508 TYPED_ACTIONS = ("store",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
509 "append",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 "callback")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
511
33846
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
512 # The set of actions which *require* a value type, ie. that
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
513 # always consume an argument from the command line.
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
514 ALWAYS_TYPED_ACTIONS = ("store",
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
515 "append")
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
516
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
517 # The set of actions which take a 'const' attribute.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
518 CONST_ACTIONS = ("store_const",
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
519 "append_const")
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
520
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
521 # The set of known types for option parsers. Again, listed here for
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
522 # constructor argument validation.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 TYPES = ("string", "int", "long", "float", "complex", "choice")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
524
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
525 # Dictionary of argument checking functions, which convert and
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 # validate option arguments according to the option type.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
527 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 # Signature of checking functions is:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 # check(option : Option, opt : string, value : string) -> any
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
530 # where
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
531 # option is the Option instance calling the checker
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 # opt is the actual option seen on the command-line
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
533 # (eg. "-a", "--file")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 # value is the option argument seen on the command-line
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 # The return value should be in the appropriate Python type
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
537 # for option.type -- eg. an integer if option.type == "int".
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
538 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
539 # If no checker is defined for a type, arguments will be
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
540 # unchecked and remain strings.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
541 TYPE_CHECKER = { "int" : check_builtin,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
542 "long" : check_builtin,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 "float" : check_builtin,
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
544 "complex": check_builtin,
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
545 "choice" : check_choice,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
546 }
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
547
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
548
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 # CHECK_METHODS is a list of unbound method objects; they are called
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 # by the constructor, in order, after all attributes are
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 # initialized. The list is created and filled in later, after all
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
552 # the methods are actually defined. (I just put it here because I
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 # like to define and document all class attributes in the same
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 # place.) Subclasses that add another _check_*() method should
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 # define their own CHECK_METHODS list that adds their check method
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
556 # to those from this class.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
557 CHECK_METHODS = None
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
558
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
559
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 # -- Constructor/initialization methods ----------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
561
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
562 def __init__(self, *opts, **attrs):
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
563 # Set _short_opts, _long_opts attrs from 'opts' tuple.
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
564 # Have to be set now, in case no option strings are supplied.
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
565 self._short_opts = []
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
566 self._long_opts = []
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
567 opts = self._check_opt_strings(opts)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
568 self._set_opt_strings(opts)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
569
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
570 # Set all other attrs (action, type, etc.) from 'attrs' dict
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 self._set_attrs(attrs)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
572
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
573 # Check all the attributes we just set. There are lots of
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
574 # complicated interdependencies, but luckily they can be farmed
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 # out to the _check_*() methods listed in CHECK_METHODS -- which
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
576 # could be handy for subclasses! The one thing these all share
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 # is that they raise OptionError if they discover a problem.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 for checker in self.CHECK_METHODS:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
579 checker(self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
580
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
581 def _check_opt_strings(self, opts):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 # Filter out None because early versions of Optik had exactly
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 # one short option and one long option, either of which
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
584 # could be None.
42142
a53e48be6df9 Merged revisions 56125-56153 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41986
diff changeset
585 opts = [opt for opt in opts if opt]
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
586 if not opts:
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
587 raise TypeError("at least one option string must be supplied")
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 return opts
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
589
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
590 def _set_opt_strings(self, opts):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 for opt in opts:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 if len(opt) < 2:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
593 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
594 "invalid option string %r: "
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
595 "must be at least two characters long" % opt, self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 elif len(opt) == 2:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
597 if not (opt[0] == "-" and opt[1] != "-"):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
599 "invalid short option string %r: "
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 "must be of the form -x, (x any non-dash char)" % opt,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
601 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 self._short_opts.append(opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
603 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
604 if not (opt[0:2] == "--" and opt[2] != "-"):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
605 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 "invalid long option string %r: "
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
607 "must start with --, followed by non-dash" % opt,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
609 self._long_opts.append(opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
610
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
611 def _set_attrs(self, attrs):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
612 for attr in self.ATTRS:
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
613 if attr in attrs:
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 setattr(self, attr, attrs[attr])
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 del attrs[attr]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
616 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
617 if attr == 'default':
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
618 setattr(self, attr, NO_DEFAULT)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
619 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
620 setattr(self, attr, None)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
621 if attrs:
40819
5ef70c49d85a Checkin the regenerated Python-ast.c and fix test_optparse.
Georg Brandl <georg@python.org>
parents: 40775
diff changeset
622 attrs = sorted(attrs.keys())
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
623 raise OptionError(
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
624 "invalid keyword arguments: %s" % ", ".join(attrs),
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
625 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
626
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
627
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
628 # -- Constructor validation methods --------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
629
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
630 def _check_action(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
631 if self.action is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 self.action = "store"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 elif self.action not in self.ACTIONS:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 raise OptionError("invalid action: %r" % self.action, self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
635
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
636 def _check_type(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
637 if self.type is None:
33846
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
638 if self.action in self.ALWAYS_TYPED_ACTIONS:
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 if self.choices is not None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 # The "choices" attribute implies "choice" type.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
641 self.type = "choice"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
643 # No type given? "string" is the most sensible default.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
644 self.type = "string"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 else:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
646 # Allow type objects or builtin type conversion functions
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
647 # (int, str, etc.) as an alternative to their names. (The
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43577
diff changeset
648 # complicated check of builtins is only necessary for
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
649 # Python 2.1 and earlier, and is short-circuited by the
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
650 # first check on modern Pythons.)
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43577
diff changeset
651 import builtins
41986
6464cc638faa Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
652 if ( isinstance(self.type, type) or
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
653 (hasattr(self.type, "__name__") and
44098
0889daf6d0d9 #1535: rename __builtin__ module to builtins.
Georg Brandl <georg@python.org>
parents: 43577
diff changeset
654 getattr(builtins, self.type.__name__, None) is self.type) ):
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
655 self.type = self.type.__name__
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
656
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
657 if self.type == "str":
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
658 self.type = "string"
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
659
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 if self.type not in self.TYPES:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
661 raise OptionError("invalid option type: %r" % self.type, self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 if self.action not in self.TYPED_ACTIONS:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
664 "must not supply a type for action %r" % self.action, self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
665
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
666 def _check_choice(self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
667 if self.type == "choice":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 if self.choices is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
669 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
670 "must supply a list of choices for type 'choice'", self)
41986
6464cc638faa Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
671 elif not isinstance(self.choices, (tuple, list)):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 "choices must be a list of strings ('%s' supplied)"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 % str(type(self.choices)).split("'")[1], self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
675 elif self.choices is not None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 "must not supply choices for type %r" % self.type, self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
678
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
679 def _check_dest(self):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
680 # No destination given, and we need one for this action. The
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
681 # self.type check is for callbacks that take a value.
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
682 takes_value = (self.action in self.STORE_ACTIONS or
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
683 self.type is not None)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
684 if self.dest is None and takes_value:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
685
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
686 # Glean a destination from the first long option string,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
687 # or from the first short option string if no long options.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 if self._long_opts:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 # eg. "--foo-bar" -> "foo_bar"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 self.dest = self._long_opts[0][2:].replace('-', '_')
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
691 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 self.dest = self._short_opts[0][1]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
693
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
694 def _check_const(self):
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
695 if self.action not in self.CONST_ACTIONS and self.const is not None:
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
696 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 "'const' must not be supplied for action %r" % self.action,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
699
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
700 def _check_nargs(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
701 if self.action in self.TYPED_ACTIONS:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
702 if self.nargs is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
703 self.nargs = 1
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
704 elif self.nargs is not None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 "'nargs' must not be supplied for action %r" % self.action,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
708
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
709 def _check_callback(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 if self.action == "callback":
73165
8e57b5d8f58f Closes #13258: Use callable() built-in in the standard library.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 69839
diff changeset
711 if not callable(self.callback):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
712 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
713 "callback not callable: %r" % self.callback, self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
714 if (self.callback_args is not None and
41986
6464cc638faa Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
715 not isinstance(self.callback_args, tuple)):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
716 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
717 "callback_args, if supplied, must be a tuple: not %r"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 % self.callback_args, self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 if (self.callback_kwargs is not None and
41986
6464cc638faa Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
720 not isinstance(self.callback_kwargs, dict)):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
722 "callback_kwargs, if supplied, must be a dict: not %r"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
723 % self.callback_kwargs, self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
724 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
725 if self.callback is not None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
727 "callback supplied (%r) for non-callback option"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
728 % self.callback, self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
729 if self.callback_args is not None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
730 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
731 "callback_args supplied for non-callback option", self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
732 if self.callback_kwargs is not None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
734 "callback_kwargs supplied for non-callback option", self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
735
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
736
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
737 CHECK_METHODS = [_check_action,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
738 _check_type,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
739 _check_choice,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
740 _check_dest,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
741 _check_const,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
742 _check_nargs,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
743 _check_callback]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
744
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
745
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
746 # -- Miscellaneous methods -----------------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
747
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
748 def __str__(self):
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
749 return "/".join(self._short_opts + self._long_opts)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
750
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
751 __repr__ = _repr
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
752
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
753 def takes_value(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
754 return self.type is not None
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
755
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
756 def get_opt_string(self):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
757 if self._long_opts:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
758 return self._long_opts[0]
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
759 else:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
760 return self._short_opts[0]
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
761
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
762
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
763 # -- Processing methods --------------------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
764
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
765 def check_value(self, opt, value):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
766 checker = self.TYPE_CHECKER.get(self.type)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
767 if checker is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 return value
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
769 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 return checker(self, opt, value)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
771
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
772 def convert_value(self, opt, value):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
773 if value is not None:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
774 if self.nargs == 1:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
775 return self.check_value(opt, value)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
776 else:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
777 return tuple([self.check_value(opt, v) for v in value])
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
778
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
779 def process(self, opt, value, values, parser):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
780
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
781 # First, convert the value(s) to the right type. Howl if any
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
782 # value(s) are bogus.
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
783 value = self.convert_value(opt, value)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
784
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 # And then take whatever action is expected of us.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 # This is a separate method to make life easier for
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
787 # subclasses to add new actions.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
788 return self.take_action(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
789 self.action, self.dest, opt, value, values, parser)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
790
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
791 def take_action(self, action, dest, opt, value, values, parser):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
792 if action == "store":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
793 setattr(values, dest, value)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 elif action == "store_const":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
795 setattr(values, dest, self.const)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
796 elif action == "store_true":
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
797 setattr(values, dest, True)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 elif action == "store_false":
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
799 setattr(values, dest, False)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
800 elif action == "append":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
801 values.ensure_value(dest, []).append(value)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
802 elif action == "append_const":
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
803 values.ensure_value(dest, []).append(self.const)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
804 elif action == "count":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 setattr(values, dest, values.ensure_value(dest, 0) + 1)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
806 elif action == "callback":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
807 args = self.callback_args or ()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
808 kwargs = self.callback_kwargs or {}
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 self.callback(self, opt, value, parser, *args, **kwargs)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 elif action == "help":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
811 parser.print_help()
33846
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
812 parser.exit()
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
813 elif action == "version":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
814 parser.print_version()
33846
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
815 parser.exit()
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 else:
49908
ed8adfef26c6 Merged revisions 67348,67355,67359,67362,67364-67365,67367-67368,67398,67423-67424,67432,67440-67441,67444-67445,67454-67455,67457-67458 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 49493
diff changeset
817 raise ValueError("unknown action %r" % self.action)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
818
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 return 1
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
820
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
821 # class Option
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
822
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
823
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
824 SUPPRESS_HELP = "SUPPRESS"+"HELP"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
825 SUPPRESS_USAGE = "SUPPRESS"+"USAGE"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
826
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 class Values:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
828
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
829 def __init__(self, defaults=None):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
830 if defaults:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
831 for (attr, val) in defaults.items():
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
832 setattr(self, attr, val)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
833
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
834 def __str__(self):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
835 return str(self.__dict__)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
836
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
837 __repr__ = _repr
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
838
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39589
diff changeset
839 def __eq__(self, other):
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
840 if isinstance(other, Values):
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39589
diff changeset
841 return self.__dict__ == other.__dict__
41986
6464cc638faa Merged revisions 55795-55816 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41969
diff changeset
842 elif isinstance(other, dict):
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39589
diff changeset
843 return self.__dict__ == other
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
844 else:
39644
13d5e5f0e993 Restructure comparison dramatically. There is no longer a default
Guido van Rossum <guido@python.org>
parents: 39589
diff changeset
845 return NotImplemented
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
846
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
847 def _update_careful(self, dict):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
848 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 Update the option values from an arbitrary dictionary, but only
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
850 use keys from dict that already have a corresponding attribute
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
851 in self. Any keys in dict without a corresponding attribute
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
852 are silently ignored.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
853 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
854 for attr in dir(self):
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
855 if attr in dict:
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
856 dval = dict[attr]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
857 if dval is not None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
858 setattr(self, attr, dval)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
859
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
860 def _update_loose(self, dict):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
861 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
862 Update the option values from an arbitrary dictionary,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
863 using all keys from the dictionary regardless of whether
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
864 they have a corresponding attribute in self or not.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
865 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
866 self.__dict__.update(dict)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
867
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
868 def _update(self, dict, mode):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
869 if mode == "careful":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
870 self._update_careful(dict)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
871 elif mode == "loose":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
872 self._update_loose(dict)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 else:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42552
diff changeset
874 raise ValueError("invalid update mode: %r" % mode)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
875
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
876 def read_module(self, modname, mode="careful"):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
877 __import__(modname)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 mod = sys.modules[modname]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
879 self._update(vars(mod), mode)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
880
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
881 def read_file(self, filename, mode="careful"):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
882 vars = {}
42552
ac192f89c109 Kill execfile(), use exec() instead
Neal Norwitz <nnorwitz@gmail.com>
parents: 42501
diff changeset
883 exec(open(filename).read(), vars)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
884 self._update(vars, mode)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
885
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
886 def ensure_value(self, attr, value):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
887 if not hasattr(self, attr) or getattr(self, attr) is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
888 setattr(self, attr, value)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
889 return getattr(self, attr)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
890
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
891
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
892 class OptionContainer:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
893
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
894 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
895 Abstract base class.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
896
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
897 Class attributes:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
898 standard_option_list : [Option]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
899 list of standard options that will be accepted by all instances
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
900 of this parser class (intended to be overridden by subclasses).
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
901
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
902 Instance attributes:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
903 option_list : [Option]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
904 the list of Option objects contained by this OptionContainer
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
905 _short_opt : { string : Option }
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
906 dictionary mapping short option strings, eg. "-f" or "-X",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
907 to the Option instances that implement them. If an Option
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
908 has multiple short option strings, it will appears in this
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
909 dictionary multiple times. [1]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
910 _long_opt : { string : Option }
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
911 dictionary mapping long option strings, eg. "--file" or
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
912 "--exclude", to the Option instances that implement them.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
913 Again, a given Option can occur multiple times in this
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
914 dictionary. [1]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
915 defaults : { string : any }
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
916 dictionary mapping option destination names to default
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
917 values for each destination [1]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
918
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
919 [1] These mappings are common to (shared by) all components of the
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
920 controlling OptionParser, where they are initially created.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
921
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
922 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
923
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
924 def __init__(self, option_class, conflict_handler, description):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
925 # Initialize the option list and related data structures.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
926 # This method must be provided by subclasses, and it must
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
927 # initialize at least the following instance attributes:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
928 # option_list, _short_opt, _long_opt, defaults.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
929 self._create_option_list()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
930
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
931 self.option_class = option_class
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
932 self.set_conflict_handler(conflict_handler)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
933 self.set_description(description)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
934
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
935 def _create_option_mappings(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
936 # For use by OptionParser constructor -- create the master
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
937 # option mappings used by this OptionParser and all
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
938 # OptionGroups that it owns.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
939 self._short_opt = {} # single letter -> Option instance
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
940 self._long_opt = {} # long option -> Option instance
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 self.defaults = {} # maps option dest -> default value
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
942
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
943
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
944 def _share_option_mappings(self, parser):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
945 # For use by OptionGroup constructor -- use shared option
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
946 # mappings from the OptionParser that owns this OptionGroup.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
947 self._short_opt = parser._short_opt
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
948 self._long_opt = parser._long_opt
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
949 self.defaults = parser.defaults
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
950
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
951 def set_conflict_handler(self, handler):
33846
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
952 if handler not in ("error", "resolve"):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42552
diff changeset
953 raise ValueError("invalid conflict_resolution value %r" % handler)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
954 self.conflict_handler = handler
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
955
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
956 def set_description(self, description):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
957 self.description = description
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
958
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
959 def get_description(self):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
960 return self.description
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
961
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
962
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
963 def destroy(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
964 """see OptionParser.destroy()."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
965 del self._short_opt
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
966 del self._long_opt
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
967 del self.defaults
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
968
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
969
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
970 # -- Option-adding methods -----------------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
971
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
972 def _check_conflict(self, option):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 conflict_opts = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
974 for opt in option._short_opts:
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
975 if opt in self._short_opt:
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
976 conflict_opts.append((opt, self._short_opt[opt]))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
977 for opt in option._long_opts:
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
978 if opt in self._long_opt:
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
979 conflict_opts.append((opt, self._long_opt[opt]))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
980
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 if conflict_opts:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
982 handler = self.conflict_handler
33846
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
983 if handler == "error":
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
984 raise OptionConflictError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
985 "conflicting option string(s): %s"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
986 % ", ".join([co[0] for co in conflict_opts]),
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
987 option)
33846
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
988 elif handler == "resolve":
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
989 for (opt, c_option) in conflict_opts:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
990 if opt.startswith("--"):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
991 c_option._long_opts.remove(opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
992 del self._long_opt[opt]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
993 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
994 c_option._short_opts.remove(opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 del self._short_opt[opt]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
996 if not (c_option._short_opts or c_option._long_opts):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 c_option.container.option_list.remove(c_option)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
998
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
999 def add_option(self, *args, **kwargs):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1000 """add_option(Option)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1001 add_option(opt_str, ..., kwarg=val, ...)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1002 """
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43020
diff changeset
1003 if isinstance(args[0], str):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 option = self.option_class(*args, **kwargs)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1005 elif len(args) == 1 and not kwargs:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1006 option = args[0]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1007 if not isinstance(option, Option):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42552
diff changeset
1008 raise TypeError("not an Option instance: %r" % option)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1009 else:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42552
diff changeset
1010 raise TypeError("invalid arguments")
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1011
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1012 self._check_conflict(option)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1013
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1014 self.option_list.append(option)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1015 option.container = self
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1016 for opt in option._short_opts:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1017 self._short_opt[opt] = option
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1018 for opt in option._long_opts:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1019 self._long_opt[opt] = option
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1020
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1021 if option.dest is not None: # option has a dest, we need a default
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1022 if option.default is not NO_DEFAULT:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 self.defaults[option.dest] = option.default
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1024 elif option.dest not in self.defaults:
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1025 self.defaults[option.dest] = None
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1026
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027 return option
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1028
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1029 def add_options(self, option_list):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1030 for option in option_list:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1031 self.add_option(option)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1032
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 # -- Option query/removal methods ----------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1034
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1035 def get_option(self, opt_str):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 return (self._short_opt.get(opt_str) or
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1037 self._long_opt.get(opt_str))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1038
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1039 def has_option(self, opt_str):
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1040 return (opt_str in self._short_opt or
39589
d240ce5696f9 More has_key() fixes.
Guido van Rossum <guido@python.org>
parents: 39575
diff changeset
1041 opt_str in self._long_opt)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1043 def remove_option(self, opt_str):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1044 option = self._short_opt.get(opt_str)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1045 if option is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1046 option = self._long_opt.get(opt_str)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1047 if option is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1048 raise ValueError("no such option %r" % opt_str)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1049
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1050 for opt in option._short_opts:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1051 del self._short_opt[opt]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1052 for opt in option._long_opts:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 del self._long_opt[opt]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054 option.container.option_list.remove(option)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1055
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1056
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1057 # -- Help-formatting methods ---------------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1058
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1059 def format_option_help(self, formatter):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1060 if not self.option_list:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1061 return ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1062 result = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1063 for option in self.option_list:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1064 if not option.help is SUPPRESS_HELP:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1065 result.append(formatter.format_option(option))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1066 return "".join(result)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1067
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1068 def format_description(self, formatter):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1069 return formatter.format_description(self.get_description())
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1070
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1071 def format_help(self, formatter):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1072 result = []
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1073 if self.description:
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1074 result.append(self.format_description(formatter))
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1075 if self.option_list:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1076 result.append(self.format_option_help(formatter))
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1077 return "\n".join(result)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1078
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1079
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1080 class OptionGroup (OptionContainer):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1081
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1082 def __init__(self, parser, title, description=None):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1083 self.parser = parser
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1084 OptionContainer.__init__(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1085 self, parser.option_class, parser.conflict_handler, description)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1086 self.title = title
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1087
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1088 def _create_option_list(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1089 self.option_list = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1090 self._share_option_mappings(self.parser)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1091
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1092 def set_title(self, title):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1093 self.title = title
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1094
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1095 def destroy(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1096 """see OptionParser.destroy()."""
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1097 OptionContainer.destroy(self)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1098 del self.option_list
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1099
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1100 # -- Help-formatting methods ---------------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1101
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1102 def format_help(self, formatter):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 result = formatter.format_heading(self.title)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1104 formatter.indent()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1105 result += OptionContainer.format_help(self, formatter)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1106 formatter.dedent()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1107 return result
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1108
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1109
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1110 class OptionParser (OptionContainer):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1111
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1112 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1113 Class attributes:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1114 standard_option_list : [Option]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1115 list of standard options that will be accepted by all instances
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1116 of this parser class (intended to be overridden by subclasses).
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1117
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1118 Instance attributes:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1119 usage : string
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1120 a usage string for your program. Before it is displayed
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1121 to the user, "%prog" will be expanded to the name of
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
1122 your program (self.prog or os.path.basename(sys.argv[0])).
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
1123 prog : string
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
1124 the name of the current program (to override
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
1125 os.path.basename(sys.argv[0])).
69839
f559b88bcaa0 #8158: add missing 'description' description to optparse docstring.
R David Murray <rdmurray@bitdance.com>
parents: 53523
diff changeset
1126 description : string
f559b88bcaa0 #8158: add missing 'description' description to optparse docstring.
R David Murray <rdmurray@bitdance.com>
parents: 53523
diff changeset
1127 A paragraph of text giving a brief overview of your program.
f559b88bcaa0 #8158: add missing 'description' description to optparse docstring.
R David Murray <rdmurray@bitdance.com>
parents: 53523
diff changeset
1128 optparse reformats this paragraph to fit the current terminal
f559b88bcaa0 #8158: add missing 'description' description to optparse docstring.
R David Murray <rdmurray@bitdance.com>
parents: 53523
diff changeset
1129 width and prints it when the user requests help (after usage,
f559b88bcaa0 #8158: add missing 'description' description to optparse docstring.
R David Murray <rdmurray@bitdance.com>
parents: 53523
diff changeset
1130 but before the list of options).
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1131 epilog : string
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1132 paragraph of help text to print after option help
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1133
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1134 option_groups : [OptionGroup]
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1135 list of option groups in this parser (option groups are
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1136 irrelevant for parsing the command-line, but very useful
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1137 for generating help)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1138
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1139 allow_interspersed_args : bool = true
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1140 if true, positional arguments may be interspersed with options.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1141 Assuming -a and -b each take a single argument, the command-line
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1142 -ablah foo bar -bboo baz
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1143 will be interpreted the same as
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1144 -ablah -bboo -- foo bar baz
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1145 If this flag were false, that command line would be interpreted as
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1146 -ablah -- foo bar -bboo baz
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1147 -- ie. we stop processing options as soon as we see the first
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1148 non-option argument. (This is the tradition followed by
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1149 Python's getopt module, Perl's Getopt::Std, and other argument-
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1150 parsing libraries, but it is generally annoying to users.)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1151
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1152 process_default_values : bool = true
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1153 if true, option default values are processed similarly to option
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1154 values from the command line: that is, they are passed to the
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1155 type-checking function for the option's type (as long as the
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1156 default value is a string). (This really only matters if you
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1157 have defined custom types; see SF bug #955889.) Set it to false
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1158 to restore the behaviour of Optik 1.4.1 and earlier.
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1159
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1160 rargs : [string]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1161 the argument list currently being parsed. Only set when
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1162 parse_args() is active, and continually trimmed down as
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1163 we consume arguments. Mainly there for the benefit of
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1164 callback options.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1165 largs : [string]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1166 the list of leftover arguments that we have skipped while
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1167 parsing options. If allow_interspersed_args is false, this
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1168 list is always empty.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169 values : Values
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1170 the set of option values currently being accumulated. Only
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1171 set when parse_args() is active. Also mainly for callbacks.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1172
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1173 Because of the 'rargs', 'largs', and 'values' attributes,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1174 OptionParser is not thread-safe. If, for some perverse reason, you
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1175 need to parse command-line arguments simultaneously in different
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1176 threads, use different OptionParser instances.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1177
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1178 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1180 standard_option_list = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1181
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1182 def __init__(self,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1183 usage=None,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1184 option_list=None,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1185 option_class=Option,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1186 version=None,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1187 conflict_handler="error",
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1188 description=None,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1189 formatter=None,
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1190 add_help_option=True,
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1191 prog=None,
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1192 epilog=None):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1193 OptionContainer.__init__(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1194 self, option_class, conflict_handler, description)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1195 self.set_usage(usage)
28705
be14970a9955 Update to Optik 1.4.1; here are the relevant bits of the change log:
Greg Ward <gward@python.net>
parents: 27091
diff changeset
1196 self.prog = prog
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1197 self.version = version
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1198 self.allow_interspersed_args = True
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1199 self.process_default_values = True
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200 if formatter is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201 formatter = IndentedHelpFormatter()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1202 self.formatter = formatter
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1203 self.formatter.set_parser(self)
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1204 self.epilog = epilog
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1205
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1206 # Populate the option list; initial sources are the
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1207 # standard_option_list class attribute, the 'option_list'
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1208 # argument, and (if applicable) the _add_version_option() and
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1209 # _add_help_option() methods.
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1210 self._populate_option_list(option_list,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1211 add_help=add_help_option)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1212
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1213 self._init_parsing_state()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1214
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1215
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1216 def destroy(self):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1217 """
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1218 Declare that you are done with this OptionParser. This cleans up
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1219 reference cycles so the OptionParser (and all objects referenced by
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1220 it) can be garbage-collected promptly. After calling destroy(), the
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1221 OptionParser is unusable.
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1222 """
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1223 OptionContainer.destroy(self)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1224 for group in self.option_groups:
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1225 group.destroy()
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1226 del self.option_list
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1227 del self.option_groups
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1228 del self.formatter
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1229
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1230
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1231 # -- Private methods -----------------------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1232 # (used by our or OptionContainer's constructor)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1233
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1234 def _create_option_list(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1235 self.option_list = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1236 self.option_groups = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1237 self._create_option_mappings()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1238
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1239 def _add_help_option(self):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1240 self.add_option("-h", "--help",
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1241 action="help",
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1242 help=_("show this help message and exit"))
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1243
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1244 def _add_version_option(self):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1245 self.add_option("--version",
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1246 action="version",
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1247 help=_("show program's version number and exit"))
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1248
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1249 def _populate_option_list(self, option_list, add_help=True):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1250 if self.standard_option_list:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1251 self.add_options(self.standard_option_list)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1252 if option_list:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1253 self.add_options(option_list)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1254 if self.version:
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1255 self._add_version_option()
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1256 if add_help:
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1257 self._add_help_option()
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1258
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1259 def _init_parsing_state(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1260 # These are set in parse_args() for the convenience of callbacks.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1261 self.rargs = None
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1262 self.largs = None
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1263 self.values = None
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1264
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1265
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1266 # -- Simple modifier methods ---------------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1267
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1268 def set_usage(self, usage):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1269 if usage is None:
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1270 self.usage = _("%prog [options]")
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1271 elif usage is SUPPRESS_USAGE:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1272 self.usage = None
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1273 # For backwards compatibility with Optik 1.3 and earlier.
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1274 elif usage.lower().startswith("usage: "):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1275 self.usage = usage[7:]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1276 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1277 self.usage = usage
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1278
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1279 def enable_interspersed_args(self):
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
1280 """Set parsing to not stop on the first non-option, allowing
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
1281 interspersing switches with command arguments. This is the
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
1282 default behavior. See also disable_interspersed_args() and the
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
1283 class documentation description of the attribute
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
1284 allow_interspersed_args."""
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1285 self.allow_interspersed_args = True
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1286
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1287 def disable_interspersed_args(self):
49493
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
1288 """Set parsing to stop on the first non-option. Use this if
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
1289 you have a command processor which runs another command that
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
1290 has options of its own and you want to make sure these options
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
1291 don't get confused.
bdf4da5d04ad merge from trunk
Benjamin Peterson <benjamin@python.org>
parents: 44098
diff changeset
1292 """
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1293 self.allow_interspersed_args = False
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1294
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1295 def set_process_default_values(self, process):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1296 self.process_default_values = process
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1297
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1298 def set_default(self, dest, value):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1299 self.defaults[dest] = value
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1300
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1301 def set_defaults(self, **kwargs):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1302 self.defaults.update(kwargs)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1303
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1304 def _get_all_options(self):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1305 options = self.option_list[:]
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1306 for group in self.option_groups:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1307 options.extend(group.option_list)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1308 return options
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1309
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1310 def get_default_values(self):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1311 if not self.process_default_values:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1312 # Old, pre-Optik 1.5 behaviour.
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1313 return Values(self.defaults)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1314
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1315 defaults = self.defaults.copy()
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1316 for option in self._get_all_options():
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1317 default = defaults.get(option.dest)
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43020
diff changeset
1318 if isinstance(default, str):
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1319 opt_str = option.get_opt_string()
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1320 defaults[option.dest] = option.check_value(opt_str, default)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1321
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1322 return Values(defaults)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1323
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1324
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1325 # -- OptionGroup methods -------------------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1327 def add_option_group(self, *args, **kwargs):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1328 # XXX lots of overlap with OptionContainer.add_option()
43577
ace45b361e87 Patch# 1258 by Christian Heimes: kill basestring.
Guido van Rossum <guido@python.org>
parents: 43020
diff changeset
1329 if isinstance(args[0], str):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1330 group = OptionGroup(self, *args, **kwargs)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1331 elif len(args) == 1 and not kwargs:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1332 group = args[0]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1333 if not isinstance(group, OptionGroup):
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42552
diff changeset
1334 raise TypeError("not an OptionGroup instance: %r" % group)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1335 if group.parser is not self:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42552
diff changeset
1336 raise ValueError("invalid OptionGroup (wrong parser)")
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1337 else:
43020
907a45df1295 Raise statement normalization in Lib/.
Collin Winter <collinw@gmail.com>
parents: 42552
diff changeset
1338 raise TypeError("invalid arguments")
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1339
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1340 self.option_groups.append(group)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1341 return group
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1343 def get_option_group(self, opt_str):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1344 option = (self._short_opt.get(opt_str) or
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1345 self._long_opt.get(opt_str))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1346 if option and option.container is not self:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1347 return option.container
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1348 return None
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1349
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1350
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1351 # -- Option-parsing methods ----------------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1352
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1353 def _get_args(self, args):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1354 if args is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1355 return sys.argv[1:]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1356 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1357 return args[:] # don't modify caller's list
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1358
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1359 def parse_args(self, args=None, values=None):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1360 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1361 parse_args(args : [string] = sys.argv[1:],
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1362 values : Values = None)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1363 -> (values : Values, args : [string])
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1364
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1365 Parse the command-line options found in 'args' (default:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1366 sys.argv[1:]). Any errors result in a call to 'error()', which
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1367 by default prints the usage message to stderr and calls
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1368 sys.exit() with an error message. On success returns a pair
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1369 (values, args) where 'values' is an Values instance (with all
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1370 your option values) and 'args' is the list of arguments left
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1371 over after parsing options.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1372 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1373 rargs = self._get_args(args)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1374 if values is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1375 values = self.get_default_values()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1376
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1377 # Store the halves of the argument list as attributes for the
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1378 # convenience of callbacks:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1379 # rargs
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1380 # the rest of the command-line (the "r" stands for
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1381 # "remaining" or "right-hand")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1382 # largs
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1383 # the leftover arguments -- ie. what's left after removing
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1384 # options and their arguments (the "l" stands for "leftover"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1385 # or "left-hand")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1386 self.rargs = rargs
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1387 self.largs = largs = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1388 self.values = values
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1389
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1390 try:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1391 stop = self._process_args(largs, rargs, values)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 39644
diff changeset
1392 except (BadOptionError, OptionValueError) as err:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1393 self.error(str(err))
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1394
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1395 args = largs + rargs
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1396 return self.check_values(values, args)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1397
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1398 def check_values(self, values, args):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1399 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1400 check_values(values : Values, args : [string])
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1401 -> (values : Values, args : [string])
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1402
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1403 Check that the supplied option values and leftover arguments are
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404 valid. Returns the option values and leftover arguments
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1405 (possibly adjusted, possibly completely new -- whatever you
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1406 like). Default implementation just returns the passed-in
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1407 values; subclasses may override as desired.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1408 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1409 return (values, args)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1410
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1411 def _process_args(self, largs, rargs, values):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1412 """_process_args(largs : [string],
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1413 rargs : [string],
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1414 values : Values)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1415
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1416 Process command-line arguments and populate 'values', consuming
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1417 options and arguments from 'rargs'. If 'allow_interspersed_args' is
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1418 false, stop at the first non-option argument. If true, accumulate any
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1419 interspersed non-option arguments in 'largs'.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1420 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1421 while rargs:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1422 arg = rargs[0]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1423 # We handle bare "--" explicitly, and bare "-" is handled by the
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1424 # standard arg handler since the short arg case ensures that the
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1425 # len of the opt string is greater than 1.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1426 if arg == "--":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1427 del rargs[0]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1428 return
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1429 elif arg[0:2] == "--":
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1430 # process a single long option (possibly with value(s))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1431 self._process_long_opt(rargs, values)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1432 elif arg[:1] == "-" and len(arg) > 1:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1433 # process a cluster of short options (possibly with
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1434 # value(s) for the last one only)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1435 self._process_short_opts(rargs, values)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1436 elif self.allow_interspersed_args:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1437 largs.append(arg)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1438 del rargs[0]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1439 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1440 return # stop now, leave this arg in rargs
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1441
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1442 # Say this is the original argument list:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1443 # [arg0, arg1, ..., arg(i-1), arg(i), arg(i+1), ..., arg(N-1)]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1444 # ^
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1445 # (we are about to process arg(i)).
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1446 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1447 # Then rargs is [arg(i), ..., arg(N-1)] and largs is a *subset* of
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1448 # [arg0, ..., arg(i-1)] (any options and their arguments will have
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1449 # been removed from largs).
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1450 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1451 # The while loop will usually consume 1 or more arguments per pass.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1452 # If it consumes 1 (eg. arg is an option that takes no arguments),
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1453 # then after _process_arg() is done the situation is:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1454 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1455 # largs = subset of [arg0, ..., arg(i)]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1456 # rargs = [arg(i+1), ..., arg(N-1)]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1457 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1458 # If allow_interspersed_args is false, largs will always be
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1459 # *empty* -- still a subset of [arg0, ..., arg(i-1)], but
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1460 # not a very interesting subset!
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1461
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1462 def _match_long_opt(self, opt):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1463 """_match_long_opt(opt : string) -> string
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1464
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1465 Determine which long option string 'opt' matches, ie. which one
85221
e07f104133d5 #18705: fix a number of typos. Patch by Févry Thibault.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 73166
diff changeset
1466 it is an unambiguous abbreviation for. Raises BadOptionError if
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1467 'opt' doesn't unambiguously match any long option string.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1468 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1469 return _match_abbrev(opt, self._long_opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1470
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1471 def _process_long_opt(self, rargs, values):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1472 arg = rargs.pop(0)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1473
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1474 # Value explicitly attached to arg? Pretend it's the next
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1475 # argument.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1476 if "=" in arg:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1477 (opt, next_arg) = arg.split("=", 1)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1478 rargs.insert(0, next_arg)
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1479 had_explicit_value = True
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1480 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1481 opt = arg
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1482 had_explicit_value = False
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1483
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1484 opt = self._match_long_opt(opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1485 option = self._long_opt[opt]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1486 if option.takes_value():
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1487 nargs = option.nargs
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1488 if len(rargs) < nargs:
68767
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
1489 self.error(ngettext(
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
1490 "%(option)s option requires %(number)d argument",
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
1491 "%(option)s option requires %(number)d arguments",
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
1492 nargs) % {"option": opt, "number": nargs})
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1493 elif nargs == 1:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1494 value = rargs.pop(0)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1495 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1496 value = tuple(rargs[0:nargs])
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1497 del rargs[0:nargs]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1498
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1499 elif had_explicit_value:
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1500 self.error(_("%s option does not take a value") % opt)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1501
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1502 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1503 value = None
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1504
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1505 option.process(opt, value, values, self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1506
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1507 def _process_short_opts(self, rargs, values):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1508 arg = rargs.pop(0)
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1509 stop = False
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1510 i = 1
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1511 for ch in arg[1:]:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1512 opt = "-" + ch
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1513 option = self._short_opt.get(opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1514 i += 1 # we have consumed a character
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1515
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1516 if not option:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1517 raise BadOptionError(opt)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1518 if option.takes_value():
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1519 # Any characters left in arg? Pretend they're the
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1520 # next arg, and stop consuming characters of arg.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1521 if i < len(arg):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1522 rargs.insert(0, arg[i:])
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1523 stop = True
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1524
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1525 nargs = option.nargs
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1526 if len(rargs) < nargs:
68767
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
1527 self.error(ngettext(
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
1528 "%(option)s option requires %(number)d argument",
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
1529 "%(option)s option requires %(number)d arguments",
4a5782a2b074 Use proper gettext plural forms in optparse (closes #4391).
Éric Araujo <merwok@netwok.org>
parents: 53523
diff changeset
1530 nargs) % {"option": opt, "number": nargs})
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1531 elif nargs == 1:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1532 value = rargs.pop(0)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1533 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1534 value = tuple(rargs[0:nargs])
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1535 del rargs[0:nargs]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1536
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1537 else: # option doesn't take a value
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1538 value = None
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1539
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1540 option.process(opt, value, values, self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1541
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1542 if stop:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1543 break
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1544
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1545
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1546 # -- Feedback methods ----------------------------------------------
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1547
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1548 def get_prog_name(self):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1549 if self.prog is None:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1550 return os.path.basename(sys.argv[0])
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1551 else:
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1552 return self.prog
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1553
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1554 def expand_prog_name(self, s):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1555 return s.replace("%prog", self.get_prog_name())
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1556
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1557 def get_description(self):
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1558 return self.expand_prog_name(self.description)
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1559
33846
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
1560 def exit(self, status=0, msg=None):
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
1561 if msg:
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
1562 sys.stderr.write(msg)
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
1563 sys.exit(status)
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
1564
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1565 def error(self, msg):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1566 """error(msg : string)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1567
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1568 Print a usage message incorporating 'msg' to stderr and exit.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1569 If you override this in a subclass, it should not return -- it
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1570 should either exit or raise an exception.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1571 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1572 self.print_usage(sys.stderr)
33846
79bcad14e96a Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents: 33772
diff changeset
1573 self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1574
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1575 def get_usage(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1576 if self.usage:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1577 return self.formatter.format_usage(
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1578 self.expand_prog_name(self.usage))
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1579 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1580 return ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1581
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1582 def print_usage(self, file=None):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1583 """print_usage(file : file = stdout)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1584
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1585 Print the usage message for the current program (self.usage) to
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49908
diff changeset
1586 'file' (default stdout). Any occurrence of the string "%prog" in
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1587 self.usage is replaced with the name of the current program
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1588 (basename of sys.argv[0]). Does nothing if self.usage is empty
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1589 or not defined.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1590 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1591 if self.usage:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
1592 print(self.get_usage(), file=file)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1593
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1594 def get_version(self):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1595 if self.version:
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1596 return self.expand_prog_name(self.version)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1597 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1598 return ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1599
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1600 def print_version(self, file=None):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1601 """print_version(file : file = stdout)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1602
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1603 Print the version message for this program (self.version) to
51776
d06a3ebf306f Merged revisions 69846 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 49908
diff changeset
1604 'file' (default stdout). As with print_usage(), any occurrence
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1605 of "%prog" in self.version is replaced by the current program's
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1606 name. Does nothing if self.version is empty or undefined.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1607 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1608 if self.version:
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
1609 print(self.get_version(), file=file)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1610
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1611 def format_option_help(self, formatter=None):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1612 if formatter is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1613 formatter = self.formatter
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1614 formatter.store_option_strings(self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1615 result = []
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1616 result.append(formatter.format_heading(_("Options")))
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1617 formatter.indent()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1618 if self.option_list:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1619 result.append(OptionContainer.format_option_help(self, formatter))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1620 result.append("\n")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1621 for group in self.option_groups:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1622 result.append(group.format_help(formatter))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1623 result.append("\n")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1624 formatter.dedent()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1625 # Drop the last "\n", or the header if no options or option groups:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1626 return "".join(result[:-1])
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1627
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1628 def format_epilog(self, formatter):
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1629 return formatter.format_epilog(self.epilog)
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1630
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1631 def format_help(self, formatter=None):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1632 if formatter is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1633 formatter = self.formatter
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1634 result = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1635 if self.usage:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1636 result.append(self.get_usage() + "\n")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1637 if self.description:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1638 result.append(self.format_description(formatter) + "\n")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1639 result.append(self.format_option_help(formatter))
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1640 result.append(self.format_epilog(formatter))
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1641 return "".join(result)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1642
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1643 def print_help(self, file=None):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1644 """print_help(file : file = stdout)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1645
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1646 Print an extended help message, listing all options and any
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1647 help text provided with them, to 'file' (default stdout).
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1648 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1649 if file is None:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1650 file = sys.stdout
42501
f6069f9dd52d SF patch# 1770008 by Christian Heimes (plus some extras).
Guido van Rossum <guido@python.org>
parents: 42142
diff changeset
1651 file.write(self.format_help())
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1652
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1653 # class OptionParser
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1654
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1655
32870
195655299ed3 Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents: 31858
diff changeset
1656 def _match_abbrev(s, wordmap):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1657 """_match_abbrev(s : string, wordmap : {string : Option}) -> string
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1658
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1659 Return the string key in 'wordmap' for which 's' is an unambiguous
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1660 abbreviation. If 's' is found to be ambiguous or doesn't match any of
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1661 'words', raise BadOptionError.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1662 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1663 # Is there an exact match?
39575
63237fcefd99 Get rid of dict.has_key(). Boy this has a lot of repercussions!
Guido van Rossum <guido@python.org>
parents: 39442
diff changeset
1664 if s in wordmap:
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1665 return s
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1666 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1667 # Isolate all words with s as a prefix.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1668 possibilities = [word for word in wordmap.keys()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1669 if word.startswith(s)]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1670 # No exact match, so there had better be just one possibility.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1671 if len(possibilities) == 1:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1672 return possibilities[0]
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1673 elif not possibilities:
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1674 raise BadOptionError(s)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1675 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1676 # More than one possible completion: ambiguous prefix.
38669
19fd6141940d Partially merge trunk into p3yk. The removal of Mac/Tools is confusing svn
Thomas Wouters <thomas@python.org>
parents: 38453
diff changeset
1677 possibilities.sort()
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 34551
diff changeset
1678 raise AmbiguousOptionError(s, possibilities)
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1679
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1680
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1681 # Some day, there might be many Option classes. As of Optik 1.3, the
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1682 # preferred way to instantiate Options is indirectly, via make_option(),
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1683 # which will become a factory function when there are many Option
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1684 # classes.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1685 make_option = Option