Mercurial > cpython-lru_cache-weakref
annotate Lib/optparse.py @ 37850:ebe59cd18273
Use explicit relative import for an, ehm, relative import.
| author | Thomas Wouters <thomas@python.org> |
|---|---|
| date | Tue, 18 Apr 2006 21:41:36 +0000 |
| parents | 028f92e44b48 |
| children | 88da84776119 29ae5e0fc348 |
| rev | line source |
|---|---|
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 """optparse - a powerful, extensible, and easy-to-use option parser. |
|
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 |
|
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
|
5 Originally distributed as Optik; see http://optik.sourceforge.net/ . |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
29021
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
7 If you have problems with this module, please do not file bugs, |
|
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
|
8 patches, or feature requests with Python; instead, use Optik's |
|
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
|
9 SourceForge project page: |
|
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
|
10 http://sourceforge.net/projects/optik |
|
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
|
11 |
|
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
|
12 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
|
13 (http://lists.sourceforge.net/lists/listinfo/optik-users). |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 |
|
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
|
16 # Python developers: please do not make changes to this file, since |
|
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
|
17 # it is automatically generated from the Optik source code. |
|
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
|
18 |
|
33846
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
19 __version__ = "1.5a2" |
|
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
|
20 |
|
29021
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
21 __all__ = ['Option', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
22 'SUPPRESS_HELP', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
23 'SUPPRESS_USAGE', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
24 'Values', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
25 'OptionContainer', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
26 'OptionGroup', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
27 'OptionParser', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
28 'HelpFormatter', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
29 'IndentedHelpFormatter', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
30 'TitledHelpFormatter', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
31 'OptParseError', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
32 'OptionError', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
33 'OptionConflictError', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
34 'OptionValueError', |
|
2f652a59ebb0
Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents:
28774
diff
changeset
|
35 '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
|
36 |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 __copyright__ = """ |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
38 Copyright (c) 2001-2004 Gregory P. Ward. All rights reserved. |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
39 Copyright (c) 2002-2004 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
|
40 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
41 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
|
42 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
|
43 met: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
44 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
45 * 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
|
46 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
|
47 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
48 * 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
|
49 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
|
50 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
|
51 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
52 * 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
|
53 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
|
54 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
|
55 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 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
|
67 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
68 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
69 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
|
70 import textwrap |
|
34125
1a938f456053
Change code in setup.py for parsing LDFLAGS and CPPFLAGS to use optparse
Brett Cannon <bcannon@gmail.com>
parents:
33846
diff
changeset
|
71 try: |
|
1a938f456053
Change code in setup.py for parsing LDFLAGS and CPPFLAGS to use optparse
Brett Cannon <bcannon@gmail.com>
parents:
33846
diff
changeset
|
72 from gettext import gettext as _ |
|
1a938f456053
Change code in setup.py for parsing LDFLAGS and CPPFLAGS to use optparse
Brett Cannon <bcannon@gmail.com>
parents:
33846
diff
changeset
|
73 except ImportError: |
|
1a938f456053
Change code in setup.py for parsing LDFLAGS and CPPFLAGS to use optparse
Brett Cannon <bcannon@gmail.com>
parents:
33846
diff
changeset
|
74 _ = lambda arg: arg |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
75 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
76 def _repr(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
77 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
|
78 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
79 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
80 # This file was generated from: |
|
33846
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
81 # Id: option_parser.py 421 2004-10-26 00:45:16Z greg |
|
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
82 # Id: option.py 422 2004-10-26 00:53:47Z greg |
|
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
83 # Id: help.py 367 2004-07-24 23:21:21Z gward |
|
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
84 # Id: errors.py 367 2004-07-24 23:21:21Z gward |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
85 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
86 class OptParseError (Exception): |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
87 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
|
88 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
|
89 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
90 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
|
91 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
|
92 |
|
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
|
93 |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
94 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
|
95 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
96 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
|
97 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
|
98 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
99 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
100 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
|
101 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
|
102 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
|
103 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
104 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
|
105 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
|
106 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
|
107 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
108 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
|
109 |
|
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 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
|
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 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
|
113 """ |
|
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 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
|
116 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
117 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
|
118 line. |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
119 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
120 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
121 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
|
122 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
123 Raised if an invalid or ambiguous option is 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
|
124 """ |
|
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
|
125 |
|
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
|
126 |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
127 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
|
128 |
|
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 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
|
131 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
|
132 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
|
133 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
134 Instance attributes: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
135 parser : OptionParser |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
136 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
|
137 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
|
138 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
|
139 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
|
140 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
|
141 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
|
142 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
|
143 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
|
144 width : int |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
145 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 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
|
151 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
|
152 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
|
153 default_tag : str |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
154 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
|
155 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
|
156 option_strings : { Option : str } |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
157 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
|
158 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
|
159 "-fFILE, --file=FILE" |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
160 _short_opt_fmt : str |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
161 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
|
162 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
|
163 "%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
|
164 Optik supports. |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
165 _long_opt_fmt : str |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
166 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
|
167 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
|
168 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
169 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
170 NO_DEFAULT_VALUE = "none" |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
171 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
172 def __init__(self, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
173 indent_increment, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
174 max_help_position, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
175 width, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
176 short_first): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
177 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
|
178 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
|
179 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
|
180 if width is None: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
181 try: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
182 width = int(os.environ['COLUMNS']) |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
183 except (KeyError, ValueError): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
184 width = 80 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
185 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
|
186 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
|
187 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
|
188 self.level = 0 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
189 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
|
190 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
|
191 self.default_tag = "%default" |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
192 self.option_strings = {} |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
193 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
|
194 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
|
195 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
196 def set_parser(self, parser): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
197 self.parser = parser |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
198 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
199 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
|
200 if delim not in ("", " "): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
201 raise ValueError( |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
202 "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
|
203 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
|
204 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
205 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
|
206 if delim not in ("=", " "): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
207 raise ValueError( |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
208 "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
|
209 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
|
210 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
211 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
|
212 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
|
213 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
|
214 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
215 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
|
216 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
|
217 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
|
218 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
|
219 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
220 def format_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
|
221 raise NotImplementedError, "subclasses must implement" |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
222 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
223 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
|
224 raise NotImplementedError, "subclasses must implement" |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
225 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
226 def format_description(self, description): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
227 if not description: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
228 return "" |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
229 desc_width = self.width - 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
|
230 indent = " "*self.current_indent |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
231 return textwrap.fill(description, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
232 desc_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
|
233 initial_indent=indent, |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
234 subsequent_indent=indent) + "\n" |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
235 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
236 def expand_default(self, option): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
237 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
|
238 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
|
239 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
240 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
|
241 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
|
242 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
|
243 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
244 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
|
245 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
246 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
|
247 # 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
|
248 # * 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
|
249 # 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
|
250 # * 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
|
251 # 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
|
252 # |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
253 # 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
|
254 # -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
|
255 # |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
256 # 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
|
257 # 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
|
258 # 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
|
259 # -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
|
260 # 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
|
261 result = [] |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
262 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
|
263 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
|
264 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
|
265 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
|
266 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
|
267 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
|
268 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
|
269 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
|
270 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
|
271 if option.help: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
272 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
|
273 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
|
274 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
|
275 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
|
276 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
|
277 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
|
278 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
|
279 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
|
280 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
281 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
|
282 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
|
283 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
|
284 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
|
285 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
|
286 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
|
287 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
|
288 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
|
289 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
|
290 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
|
291 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
|
292 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
|
293 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
|
294 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
|
295 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
|
296 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
|
297 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
|
298 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
299 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
|
300 """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
|
301 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
|
302 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
|
303 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
|
304 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
|
305 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
|
306 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
|
307 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
|
308 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
|
309 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
|
310 |
|
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
|
311 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
|
312 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
|
313 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
|
314 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
|
315 |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
316 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
|
317 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
318 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
|
319 """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
|
320 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
321 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
322 def __init__(self, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
323 indent_increment=2, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
324 max_help_position=24, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
325 width=None, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
326 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
|
327 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
|
328 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
|
329 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
330 def format_usage(self, usage): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
331 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
|
332 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
333 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
|
334 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
|
335 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
336 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
337 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
|
338 """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
|
339 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
340 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
341 def __init__(self, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
342 indent_increment=0, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
343 max_help_position=24, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
344 width=None, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
345 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
|
346 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
|
347 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
|
348 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
349 def format_usage(self, usage): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
350 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
|
351 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
352 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
|
353 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
|
354 |
|
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
|
355 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
356 _builtin_cvt = { "int" : (int, _("integer")), |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
357 "long" : (long, _("long integer")), |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
358 "float" : (float, _("floating-point")), |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
359 "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
|
360 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
361 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
|
362 (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
|
363 try: |
|
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 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
|
365 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
|
366 raise OptionValueError( |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
367 _("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
|
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 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
|
370 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
|
371 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
|
372 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
373 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
|
374 raise OptionValueError( |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
375 _("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
|
376 % (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
|
377 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
378 # 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
|
379 # 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
|
380 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
|
381 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
382 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
383 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
|
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 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
|
386 _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
|
387 _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
|
388 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
389 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
|
390 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
|
391 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
|
392 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
|
393 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
|
394 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
|
395 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
|
396 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
|
397 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
|
398 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
|
399 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
|
400 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
|
401 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
402 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
403 # 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
|
404 # 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
|
405 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
|
406 'type', |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
407 'dest', |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
408 'default', |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
409 'nargs', |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
410 'const', |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
411 'choices', |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
412 'callback', |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
413 '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
|
414 '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
|
415 'help', |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
416 'metavar'] |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
417 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
418 # 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
|
419 # 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
|
420 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
|
421 "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
|
422 "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
|
423 "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
|
424 "append", |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
425 "count", |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
426 "callback", |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
427 "help", |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
428 "version") |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
429 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
430 # 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
|
431 # 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
|
432 # 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
|
433 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
|
434 "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
|
435 "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
|
436 "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
|
437 "append", |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
438 "count") |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
439 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
440 # 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
|
441 # 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
|
442 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
|
443 "append", |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
444 "callback") |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
445 |
|
33846
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
446 # 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
|
447 # 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
|
448 ALWAYS_TYPED_ACTIONS = ("store", |
|
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
449 "append") |
|
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
450 |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
451 # 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
|
452 # 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
|
453 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
|
454 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
455 # 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
|
456 # 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
|
457 # |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
458 # 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
|
459 # 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
|
460 # where |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
461 # 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
|
462 # 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
|
463 # (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
|
464 # 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
|
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 # 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
|
467 # 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
|
468 # |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
469 # 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
|
470 # 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
|
471 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
|
472 "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
|
473 "float" : check_builtin, |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
474 "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
|
475 "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
|
476 } |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
477 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
478 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
479 # 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
|
480 # 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
|
481 # 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
|
482 # 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
|
483 # 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
|
484 # 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
|
485 # 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
|
486 # 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
|
487 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
|
488 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
489 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
490 # -- 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
|
491 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
492 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
|
493 # 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
|
494 # 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
|
495 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
|
496 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
|
497 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
|
498 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
|
499 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
500 # 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
|
501 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
|
502 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
503 # 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
|
504 # 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
|
505 # 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
|
506 # 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
|
507 # 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
|
508 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
|
509 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
|
510 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
511 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
|
512 # 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
|
513 # 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
|
514 # could be None. |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
515 opts = filter(None, opts) |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
516 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
|
517 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
|
518 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
|
519 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
520 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
|
521 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
|
522 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
|
523 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
|
524 "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
|
525 "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
|
526 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
|
527 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
|
528 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
|
529 "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
|
530 "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
|
531 self) |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
532 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
|
533 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
534 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
|
535 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
|
536 "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
|
537 "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
|
538 self) |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
539 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
|
540 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
541 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
|
542 for attr in self.ATTRS: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
543 if attrs.has_key(attr): |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
544 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
|
545 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
|
546 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
547 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
|
548 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
|
549 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
550 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
|
551 if attrs: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
552 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
|
553 "invalid keyword arguments: %s" % ", ".join(attrs.keys()), |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
554 self) |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
555 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
556 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
557 # -- 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
|
558 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
559 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
|
560 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
|
561 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
|
562 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
|
563 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
|
564 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
565 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
|
566 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
|
567 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
|
568 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
|
569 # 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
|
570 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
|
571 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
572 # 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
|
573 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
|
574 else: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
575 # Allow type objects as an alternative to their names. |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
576 if type(self.type) is type: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
577 self.type = self.type.__name__ |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
578 if self.type == "str": |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
579 self.type = "string" |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
580 |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
581 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
|
582 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
|
583 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
|
584 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
|
585 "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
|
586 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
587 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
|
588 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
|
589 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
|
590 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
|
591 "must supply a list of choices for type 'choice'", self) |
|
34551
028f92e44b48
Reduce the usage of the types module.
Raymond Hettinger <python@rcn.com>
parents:
34125
diff
changeset
|
592 elif type(self.choices) not in (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
|
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 "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
|
595 % 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
|
596 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
|
597 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
|
598 "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
|
599 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
600 def _check_dest(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
601 # 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
|
602 # 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
|
603 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
|
604 self.type is not None) |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
605 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
|
606 |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
607 # 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
|
608 # 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
|
609 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
|
610 # 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
|
611 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
|
612 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
613 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
|
614 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
615 def _check_const(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
|
616 if self.action != "store_const" and self.const 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
|
617 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
|
618 "'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
|
619 self) |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
620 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
621 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
|
622 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
|
623 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
|
624 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
|
625 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
|
626 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
|
627 "'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
|
628 self) |
|
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_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
|
631 if self.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
|
632 if not callable(self.callback): |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
633 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
|
634 "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
|
635 if (self.callback_args is not None and |
|
34551
028f92e44b48
Reduce the usage of the types module.
Raymond Hettinger <python@rcn.com>
parents:
34125
diff
changeset
|
636 type(self.callback_args) is not 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
|
637 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
|
638 "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
|
639 % 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
|
640 if (self.callback_kwargs is not None and |
|
34551
028f92e44b48
Reduce the usage of the types module.
Raymond Hettinger <python@rcn.com>
parents:
34125
diff
changeset
|
641 type(self.callback_kwargs) is not 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
|
642 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
|
643 "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
|
644 % 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
|
645 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
646 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
|
647 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
|
648 "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
|
649 % 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
|
650 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
|
651 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
|
652 "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
|
653 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
|
654 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
|
655 "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
|
656 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
657 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
658 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
|
659 _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
|
660 _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
|
661 _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
|
662 _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
|
663 _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
|
664 _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
|
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 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
667 # -- 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
|
668 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
669 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
|
670 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
|
671 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
672 __repr__ = _repr |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
673 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
674 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
|
675 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
|
676 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
677 def get_opt_string(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
678 if self._long_opts: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
679 return self._long_opts[0] |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
680 else: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
681 return self._short_opts[0] |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
682 |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
683 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
684 # -- 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
|
685 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
686 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
|
687 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
|
688 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
|
689 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
|
690 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
691 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
|
692 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
693 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
|
694 if value is not None: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
695 if self.nargs == 1: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
696 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
|
697 else: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
698 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
|
699 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
700 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
|
701 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
702 # 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
|
703 # value(s) are bogus. |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
704 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
|
705 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
706 # 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
|
707 # 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
|
708 # 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
|
709 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
|
710 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
|
711 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
712 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
|
713 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
|
714 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
|
715 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
|
716 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
|
717 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
|
718 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
|
719 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
|
720 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
|
721 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
|
722 values.ensure_value(dest, []).append(value) |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
723 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
|
724 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
|
725 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
|
726 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
|
727 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
|
728 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
|
729 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
|
730 parser.print_help() |
|
33846
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
731 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
|
732 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
|
733 parser.print_version() |
|
33846
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
734 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
|
735 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
736 raise RuntimeError, "unknown 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
|
737 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
738 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
|
739 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
740 # 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
|
741 |
|
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
|
742 |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
743 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
|
744 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
|
745 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
746 # For compatibility with Python 2.2 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
747 try: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
748 True, False |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
749 except NameError: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
750 (True, False) = (1, 0) |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
751 try: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
752 basestring |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
753 except NameError: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
754 basestring = (str, unicode) |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
755 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
756 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
757 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
|
758 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
759 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
|
760 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
|
761 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
|
762 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
|
763 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
764 def __str__(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
765 return str(self.__dict__) |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
766 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
767 __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
|
768 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
769 def __eq__(self, other): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
770 if isinstance(other, Values): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
771 return self.__dict__ == other.__dict__ |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
772 elif isinstance(other, dict): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
773 return self.__dict__ == other |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
774 else: |
|
33772
ba75fda9220c
Use proper value for False
Neal Norwitz <nnorwitz@gmail.com>
parents:
32870
diff
changeset
|
775 return False |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
776 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
777 def __ne__(self, other): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
778 return not (self == other) |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
779 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
780 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
|
781 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
782 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
|
783 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
|
784 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
|
785 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
|
786 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
787 for attr in dir(self): |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
788 if dict.has_key(attr): |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
789 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
|
790 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
|
791 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
|
792 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
793 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
|
794 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
795 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
|
796 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
|
797 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
|
798 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
799 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
|
800 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
801 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
|
802 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
|
803 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
|
804 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
|
805 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
|
806 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
807 raise ValueError, "invalid update mode: %r" % mode |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
808 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
809 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
|
810 __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
|
811 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
|
812 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
|
813 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
814 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
|
815 vars = {} |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
816 execfile(filename, vars) |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
817 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
|
818 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
819 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
|
820 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
|
821 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
|
822 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
|
823 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
824 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
825 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
|
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 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
828 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
|
829 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
830 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
|
831 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
|
832 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
|
833 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
|
834 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
835 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
|
836 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
|
837 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
|
838 _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
|
839 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
|
840 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
|
841 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
|
842 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
|
843 _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
|
844 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
|
845 "--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
|
846 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
|
847 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
|
848 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
|
849 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
|
850 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
|
851 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
852 [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
|
853 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
|
854 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
855 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
856 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
857 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
|
858 # 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
|
859 # 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
|
860 # 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
|
861 # 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
|
862 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
|
863 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
864 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
|
865 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
|
866 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
|
867 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
868 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
|
869 # 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
|
870 # 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
|
871 # 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
|
872 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
|
873 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
|
874 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
|
875 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
876 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
877 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
|
878 # 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
|
879 # 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
|
880 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
|
881 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
|
882 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
|
883 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
884 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
|
885 if handler not in ("error", "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
|
886 raise ValueError, "invalid conflict_resolution value %r" % handler |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
887 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
|
888 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
889 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
|
890 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
|
891 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
892 def get_description(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
893 return self.description |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
894 |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
895 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
896 # -- 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
|
897 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
898 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
|
899 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
|
900 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
|
901 if self._short_opt.has_key(opt): |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
902 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
|
903 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
|
904 if self._long_opt.has_key(opt): |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
905 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
|
906 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
907 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
|
908 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
|
909 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
|
910 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
|
911 "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
|
912 % ", ".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
|
913 option) |
|
33846
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
914 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
|
915 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
|
916 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
|
917 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
|
918 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
|
919 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
920 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
|
921 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
|
922 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
|
923 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
|
924 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
925 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
|
926 """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
|
927 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
|
928 """ |
|
34551
028f92e44b48
Reduce the usage of the types module.
Raymond Hettinger <python@rcn.com>
parents:
34125
diff
changeset
|
929 if type(args[0]) is 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
|
930 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
|
931 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
|
932 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
|
933 if not isinstance(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
|
934 raise TypeError, "not an Option instance: %r" % option |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
935 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
936 raise TypeError, "invalid arguments" |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
937 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
938 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
|
939 |
|
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.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
|
941 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
|
942 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
|
943 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
|
944 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
|
945 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
|
946 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
947 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
|
948 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
|
949 self.defaults[option.dest] = option.default |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
950 elif not self.defaults.has_key(option.dest): |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
951 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
|
952 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
953 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
|
954 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
955 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
|
956 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
|
957 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
|
958 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
959 # -- 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
|
960 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
961 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
|
962 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
|
963 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
|
964 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
965 def has_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
|
966 return (self._short_opt.has_key(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
|
967 self._long_opt.has_key(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
|
968 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
969 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
|
970 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
|
971 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
|
972 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
|
973 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
|
974 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
|
975 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
976 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
|
977 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
|
978 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
|
979 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
|
980 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
|
981 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
982 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
983 # -- 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
|
984 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
985 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
|
986 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
|
987 return "" |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
988 result = [] |
|
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 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
|
990 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
|
991 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
|
992 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
|
993 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
994 def format_description(self, formatter): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
995 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
|
996 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
997 def format_help(self, formatter): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
998 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
|
999 if self.description: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1000 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
|
1001 if self.option_list: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1002 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
|
1003 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
|
1004 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1005 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1006 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
|
1007 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1008 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
|
1009 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
|
1010 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
|
1011 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
|
1012 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
|
1013 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1014 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
|
1015 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
|
1016 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
|
1017 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1018 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
|
1019 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
|
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 # -- 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
|
1022 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1023 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
|
1024 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
|
1025 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
|
1026 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
|
1027 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
|
1028 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
|
1029 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1030 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1031 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
|
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 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1034 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
|
1035 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
|
1036 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
|
1037 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
|
1038 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1039 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
|
1040 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
|
1041 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
|
1042 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
|
1043 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
|
1044 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
|
1045 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
|
1046 os.path.basename(sys.argv[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
|
1047 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1048 option_groups : [OptionGroup] |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1049 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
|
1050 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
|
1051 for generating help) |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1052 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1053 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
|
1054 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
|
1055 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
|
1056 -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
|
1057 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
|
1058 -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
|
1059 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
|
1060 -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
|
1061 -- 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
|
1062 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
|
1063 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
|
1064 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
|
1065 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1066 process_default_values : bool = true |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1067 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
|
1068 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
|
1069 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
|
1070 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
|
1071 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
|
1072 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
|
1073 |
|
26224
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1074 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
|
1075 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
|
1076 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
|
1077 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
|
1078 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
|
1079 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
|
1080 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
|
1081 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
|
1082 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
|
1083 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
|
1084 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
|
1085 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
|
1086 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1087 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
|
1088 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
|
1089 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
|
1090 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
|
1091 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1092 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1093 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1094 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
|
1095 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1096 def __init__(self, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1097 usage=None, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1098 option_list=None, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1099 option_class=Option, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1100 version=None, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1101 conflict_handler="error", |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1102 description=None, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1103 formatter=None, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1104 add_help_option=True, |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1105 prog=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
|
1106 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
|
1107 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
|
1108 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
|
1109 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
|
1110 self.version = version |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1111 self.allow_interspersed_args = True |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1112 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
|
1113 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
|
1114 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
|
1115 self.formatter = formatter |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1116 self.formatter.set_parser(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
|
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 # 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
|
1119 # 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
|
1120 # 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
|
1121 # _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
|
1122 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
|
1123 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
|
1124 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1125 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
|
1126 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1127 # -- 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
|
1128 # (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
|
1129 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1130 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
|
1131 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
|
1132 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
|
1133 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
|
1134 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1135 def _add_help_option(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1136 self.add_option("-h", "--help", |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1137 action="help", |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1138 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
|
1139 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1140 def _add_version_option(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1141 self.add_option("--version", |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1142 action="version", |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1143 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
|
1144 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1145 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
|
1146 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
|
1147 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
|
1148 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
|
1149 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
|
1150 if self.version: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1151 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
|
1152 if add_help: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1153 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
|
1154 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1155 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
|
1156 # 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
|
1157 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
|
1158 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
|
1159 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
|
1160 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1161 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1162 # -- 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
|
1163 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1164 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
|
1165 if usage is None: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1166 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
|
1167 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
|
1168 self.usage = None |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1169 # For backwards compatibility with Optik 1.3 and earlier. |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1170 elif usage.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
|
1171 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
|
1172 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1173 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
|
1174 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1175 def enable_interspersed_args(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1176 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
|
1177 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1178 def disable_interspersed_args(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1179 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
|
1180 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1181 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
|
1182 self.process_default_values = process |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1183 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1184 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
|
1185 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
|
1186 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1187 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
|
1188 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
|
1189 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1190 def _get_all_options(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1191 options = self.option_list[:] |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1192 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
|
1193 options.extend(group.option_list) |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1194 return options |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1195 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1196 def get_default_values(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1197 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
|
1198 # 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
|
1199 return Values(self.defaults) |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1200 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1201 defaults = self.defaults.copy() |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1202 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
|
1203 default = defaults.get(option.dest) |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1204 if isinstance(default, basestring): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1205 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
|
1206 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
|
1207 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1208 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
|
1209 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1210 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1211 # -- 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
|
1212 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1213 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
|
1214 # XXX lots of overlap with OptionContainer.add_option() |
|
34551
028f92e44b48
Reduce the usage of the types module.
Raymond Hettinger <python@rcn.com>
parents:
34125
diff
changeset
|
1215 if type(args[0]) is 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
|
1216 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
|
1217 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
|
1218 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
|
1219 if not isinstance(group, OptionGroup): |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1220 raise TypeError, "not an OptionGroup instance: %r" % group |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1221 if group.parser 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
|
1222 raise ValueError, "invalid OptionGroup (wrong parser)" |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1223 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1224 raise TypeError, "invalid arguments" |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1225 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1226 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
|
1227 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
|
1228 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1229 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
|
1230 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
|
1231 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
|
1232 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
|
1233 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
|
1234 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
|
1235 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1236 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1237 # -- 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
|
1238 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1239 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
|
1240 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
|
1241 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
|
1242 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1243 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
|
1244 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1245 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
|
1246 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1247 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
|
1248 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
|
1249 -> (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
|
1250 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1251 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
|
1252 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
|
1253 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
|
1254 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
|
1255 (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
|
1256 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
|
1257 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
|
1258 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1259 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
|
1260 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
|
1261 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
|
1262 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1263 # 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
|
1264 # 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
|
1265 # rargs |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1266 # 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
|
1267 # "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
|
1268 # largs |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1269 # 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
|
1270 # 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
|
1271 # 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
|
1272 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
|
1273 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
|
1274 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
|
1275 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1276 try: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1277 stop = self._process_args(largs, 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
|
1278 except (BadOptionError, OptionValueError), err: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1279 self.error(err.msg) |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1280 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1281 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
|
1282 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
|
1283 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1284 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
|
1285 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1286 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
|
1287 -> (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
|
1288 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1289 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
|
1290 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
|
1291 (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
|
1292 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
|
1293 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
|
1294 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1295 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
|
1296 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1297 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
|
1298 """_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
|
1299 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
|
1300 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
|
1301 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1302 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
|
1303 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
|
1304 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
|
1305 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
|
1306 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1307 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
|
1308 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
|
1309 # 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
|
1310 # 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
|
1311 # 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
|
1312 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
|
1313 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
|
1314 return |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1315 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
|
1316 # 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
|
1317 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
|
1318 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
|
1319 # 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
|
1320 # 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
|
1321 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
|
1322 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
|
1323 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
|
1324 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
|
1325 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1326 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
|
1327 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1328 # 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
|
1329 # [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
|
1330 # ^ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1331 # (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
|
1332 # |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1333 # 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
|
1334 # [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
|
1335 # 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
|
1336 # |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1337 # 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
|
1338 # 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
|
1339 # 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
|
1340 # |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1341 # 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
|
1342 # 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
|
1343 # |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1344 # 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
|
1345 # *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
|
1346 # 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
|
1347 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1348 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
|
1349 """_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
|
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 Determine which long option string 'opt' matches, ie. which one |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1352 it is an unambiguous abbrevation for. Raises BadOptionError if |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1353 '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
|
1354 """ |
|
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 _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
|
1356 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1357 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
|
1358 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
|
1359 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1360 # 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
|
1361 # argument. |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1362 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
|
1363 (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
|
1364 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
|
1365 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
|
1366 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1367 opt = arg |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1368 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
|
1369 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1370 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
|
1371 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
|
1372 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
|
1373 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
|
1374 if len(rargs) < nargs: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1375 if nargs == 1: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1376 self.error(_("%s option requires an argument") % 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
|
1377 else: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1378 self.error(_("%s option requires %d 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
|
1379 % (opt, nargs)) |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1380 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
|
1381 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
|
1382 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1383 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
|
1384 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
|
1385 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1386 elif had_explicit_value: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1387 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
|
1388 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1389 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1390 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
|
1391 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1392 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
|
1393 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1394 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
|
1395 arg = rargs.pop(0) |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1396 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
|
1397 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
|
1398 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
|
1399 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
|
1400 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
|
1401 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
|
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 if not option: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1404 self.error(_("no such option: %s") % 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
|
1405 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
|
1406 # 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
|
1407 # 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
|
1408 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
|
1409 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
|
1410 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
|
1411 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1412 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
|
1413 if len(rargs) < nargs: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1414 if nargs == 1: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1415 self.error(_("%s option requires an argument") % 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
|
1416 else: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1417 self.error(_("%s option requires %d 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
|
1418 % (opt, nargs)) |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1419 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
|
1420 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
|
1421 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1422 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
|
1423 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
|
1424 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1425 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
|
1426 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
|
1427 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1428 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
|
1429 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1430 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
|
1431 break |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1432 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1433 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1434 # -- 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
|
1435 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1436 def get_prog_name(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1437 if self.prog is None: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1438 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
|
1439 else: |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1440 return self.prog |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1441 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1442 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
|
1443 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
|
1444 |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1445 def get_description(self): |
|
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1446 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
|
1447 |
|
33846
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
1448 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
|
1449 if msg: |
|
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
1450 sys.stderr.write(msg) |
|
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
1451 sys.exit(status) |
|
79bcad14e96a
Update optparse module and test suite to Optik 1.5a2.
Greg Ward <gward@python.net>
parents:
33772
diff
changeset
|
1452 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1453 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
|
1454 """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
|
1455 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1456 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
|
1457 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
|
1458 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
|
1459 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1460 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
|
1461 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
|
1462 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1463 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
|
1464 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
|
1465 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
|
1466 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
|
1467 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1468 return "" |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1469 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1470 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
|
1471 """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
|
1472 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1473 Print the usage message for the current program (self.usage) to |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1474 'file' (default stdout). Any occurence of the string "%prog" in |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1475 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
|
1476 (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
|
1477 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
|
1478 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1479 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
|
1480 print >>file, self.get_usage() |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1481 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1482 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
|
1483 if self.version: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1484 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
|
1485 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1486 return "" |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1487 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1488 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
|
1489 """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
|
1490 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1491 Print the version message for this program (self.version) to |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1492 'file' (default stdout). As with print_usage(), any occurence |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1493 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
|
1494 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
|
1495 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1496 if self.version: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1497 print >>file, self.get_version() |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1498 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1499 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
|
1500 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
|
1501 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
|
1502 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
|
1503 result = [] |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1504 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
|
1505 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
|
1506 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
|
1507 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
|
1508 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
|
1509 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
|
1510 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
|
1511 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
|
1512 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
|
1513 # 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
|
1514 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
|
1515 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1516 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
|
1517 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
|
1518 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
|
1519 result = [] |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1520 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
|
1521 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
|
1522 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
|
1523 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
|
1524 result.append(self.format_option_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
|
1525 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
|
1526 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1527 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
|
1528 """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
|
1529 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1530 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
|
1531 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
|
1532 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1533 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
|
1534 file = sys.stdout |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1535 file.write(self.format_help()) |
|
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 # 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
|
1538 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1539 |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1540 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
|
1541 """_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
|
1542 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1543 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
|
1544 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
|
1545 '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
|
1546 """ |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1547 # Is there an exact match? |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1548 if wordmap.has_key(s): |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1549 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
|
1550 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1551 # 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
|
1552 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
|
1553 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
|
1554 # 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
|
1555 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
|
1556 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
|
1557 elif not possibilities: |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1558 raise BadOptionError(_("no such option: %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
|
1559 else: |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1560 # More than one possible completion: ambiguous prefix. |
|
32870
195655299ed3
Upgrade optparse module and tests to Optik 1.5a1:
Greg Ward <gward@python.net>
parents:
31858
diff
changeset
|
1561 raise BadOptionError(_("ambiguous option: %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
|
1562 % (s, ", ".join(possibilities))) |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1563 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1564 |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1565 # 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
|
1566 # 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
|
1567 # 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
|
1568 # classes. |
|
adb93f4e9780
Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1569 make_option = Option |
