annotate Lib/optparse.py @ 32273:95287cddbc1e legacy-trunk

add -L flag to cause leaks(1) command to run just before exit
author Skip Montanaro <skip@pobox.com>
date Sun, 06 Jun 2004 15:53:18 +0000
parents 0d8ee28bf6f0
children 195655299ed3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
29021
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
19 __version__ = "1.4.1+"
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 'STD_HELP_OPTION',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
25 'STD_VERSION_OPTION',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
26 'Values',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
27 'OptionContainer',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
28 'OptionGroup',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
29 'OptionParser',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
30 'HelpFormatter',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
31 'IndentedHelpFormatter',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
32 'TitledHelpFormatter',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
33 'OptParseError',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
34 'OptionError',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
35 'OptionConflictError',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
36 'OptionValueError',
2f652a59ebb0 Update to Optik 1.4.1+: adds __all__ (Optik SF bug #728563).
Greg Ward <gward@python.net>
parents: 28774
diff changeset
37 '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
38
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 __copyright__ = """
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
40 Copyright (c) 2001-2003 Gregory P. Ward. 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
41
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 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
43 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
44 met:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
45
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 * 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
47 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
48
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 * 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
50 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
51 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
52
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 * 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
54 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
55 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
56
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 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
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 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
67 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
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
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 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
71 import types
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 import textwrap
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
73
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
74 class OptParseError (Exception):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 def __init__ (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
76 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
77
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 def __str__ (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 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
80
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
81
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 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
83 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 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
85 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
86 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 def __init__ (self, msg, option):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 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
90 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
91
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 def __str__ (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 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
94 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
95 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 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
97
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 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
99 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 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
101 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
102
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 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
104 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 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
106 line.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
108
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 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
110 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 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
112 """
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
113
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
114
26224
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 HelpFormatter:
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 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
118 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
119 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
120 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
121
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 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
123 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
124 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
125 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
126 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
127 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
128 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
129 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
130 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
131 total number of columns for output
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 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
133 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
134 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
135 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
136 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
137 number of columns available for option help text (calculated)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 def __init__ (self,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 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
142 max_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
143 width,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 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
145 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
146 self.help_position = self.max_help_position = max_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
147 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
148 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
149 self.level = 0
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 self.help_width = width - max_help_position
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
151 self.short_first = short_first
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 def indent (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 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
155 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
156
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 def dedent (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 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
159 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
160 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
161
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 def format_usage (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
163 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
164
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 def format_heading (self, heading):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 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
167
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 def format_description (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
169 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
170 indent = " "*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
171 return textwrap.fill(description, desc_width,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 initial_indent=indent,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 subsequent_indent=indent)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
174
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 def format_option (self, option):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 # 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
177 # * 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
178 # 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
179 # * 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
180 # 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
181 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 # 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
183 # -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
184 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 # 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
186 # 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
187 # 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
188 # -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
189 # 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
190 result = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 opts = option.option_strings
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 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
193 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
194 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
195 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
196 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
197 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
198 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
199 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
200 if option.help:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 help_lines = textwrap.wrap(option.help, self.help_width)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 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
203 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
204 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
205 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
206 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
207 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
208
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 def store_option_strings (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
210 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
211 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
212 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
213 strings = self.format_option_strings(opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 opt.option_strings = strings
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 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
216 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
217 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
218 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
219 strings = self.format_option_strings(opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 opt.option_strings = strings
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
221 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
222 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
223 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
224 self.help_position = min(max_len + 2, self.max_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
225
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 def format_option_strings (self, option):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 """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
228 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
229 metavar = option.metavar or option.dest.upper()
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
230 short_opts = [sopt + metavar for sopt in 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
231 long_opts = [lopt + "=" + metavar 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
232 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
233 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
234 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
235
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
236 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
237 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
238 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
239 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
240
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 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
242
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 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
244 """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
245 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
246
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 def __init__ (self,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 indent_increment=2,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 max_help_position=24,
31424
8f47281fea7b Backporting revision 1.6:
Barry Warsaw <barry@python.org>
parents: 29021
diff changeset
250 width=79,
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 short_first=1):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 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
253 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
254
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 def format_usage (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
256 return "usage: %s\n" % usage
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
257
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 def format_heading (self, heading):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 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
260
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
261
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 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
263 """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
264 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
265
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 def __init__ (self,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 indent_increment=0,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 max_help_position=24,
31424
8f47281fea7b Backporting revision 1.6:
Barry Warsaw <barry@python.org>
parents: 29021
diff changeset
269 width=79,
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 short_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
271 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
272 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
273
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
274 def format_usage (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
275 return "%s %s\n" % (self.format_heading("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
276
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 def format_heading (self, heading):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 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
279
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
280
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 _builtin_cvt = { "int" : (int, "integer"),
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 "long" : (long, "long integer"),
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 "float" : (float, "floating-point"),
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 "complex" : (complex, "complex") }
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
285
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
286 def check_builtin (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
287 (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
288 try:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
289 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
290 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
291 raise OptionValueError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
292 #"%s: invalid %s argument %r" % (opt, what, value))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 "option %s: invalid %s value: %r" % (opt, what, value))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
294
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
295 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
296 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
297 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
298 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
299 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
300 raise OptionValueError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
301 "option %s: invalid choice: %r (choose from %s)"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
302 % (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
303
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
304 # 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
305 # so we need an explicit "not supplied" value.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
306 NO_DEFAULT = "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
307
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
308
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
309 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
310 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 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
312 _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
313 _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
314
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 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
316 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
317 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
318 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
319 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
320 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
321 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
322 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
323 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
324 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
325 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
326 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
327 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
328
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 # 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
330 # 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
331 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
332 'type',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 'dest',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 'default',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 'nargs',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
336 'const',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
337 'choices',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 'callback',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 '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
340 '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
341 'help',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 'metavar']
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
343
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
344 # 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
345 # 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
346 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
347 "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
348 "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
349 "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
350 "append",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 "count",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 "callback",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 "help",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 "version")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
355
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 # 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
357 # 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
358 # 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
359 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
360 "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
361 "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
362 "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
363 "append",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
364 "count")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
365
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
366 # The set of actions for which it makes sense to supply 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
367 # type, ie. where we expect an argument to this option.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 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
369 "append",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 "callback")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
371
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 # 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
373 # 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
374 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
375
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 # 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
377 # 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
378 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 # 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
380 # 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
381 # where
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 # 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
383 # 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
384 # (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
385 # 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
386 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 # 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
388 # 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
389 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 # 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
391 # 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
392 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
393 "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
394 "float" : 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
395 "complex" : 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
396 "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
397 }
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
398
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
399
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 # 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
401 # 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
402 # 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
403 # 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
404 # 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
405 # 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
406 # 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
407 # 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
408 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
409
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
410
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 # -- 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
412
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 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
414 # 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
415 # 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
416 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
417 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
418 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
419 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
420
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 # 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
422 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
423
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 # 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
425 # 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
426 # 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
427 # 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
428 # 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
429 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
430 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
431
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
432 def _check_opt_strings (self, opts):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
433 # 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
434 # 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
435 # 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
436 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
437 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
438 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
439 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
440
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
441 def _set_opt_strings (self, opts):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 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
443 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
444 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
445 "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
446 "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
447 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
448 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
449 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
450 "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
451 "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
452 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
453 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
454 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
455 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
456 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
457 "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
458 "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
459 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 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
461
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
462 def _set_attrs (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
463 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
464 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
465 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
466 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
467 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
468 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
469 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
470 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
471 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
472 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
473 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
474 "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
475 self)
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 # -- 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
479
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
480 def _check_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
481 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
482 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
483 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
484 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
485
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
486 def _check_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
487 if self.type 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
488 # XXX should factor out another class attr here: list of
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
489 # actions that *require* a type
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
490 if self.action in ("store", "append"):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
491 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
492 # 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
493 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
494 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
495 # 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
496 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
497 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
498 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
499 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
500 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
501 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
502 "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
503
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
504 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
505 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
506 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
507 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
508 "must supply a list of choices for type '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
509 elif type(self.choices) not in (types.TupleType, types.ListType):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
510 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
511 "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
512 % 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
513 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
514 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
515 "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
516
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
517 def _check_dest (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
518 if self.action in self.STORE_ACTIONS and self.dest 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
519 # No destination given, and we need one for this action.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
520 # 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
521 # 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
522 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
523 # 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
524 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
525 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
526 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
527
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
528 def _check_const (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 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
530 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
531 "'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
532 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
533
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
534 def _check_nargs (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
535 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
536 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
537 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
538 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
539 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
540 "'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
541 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
542
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
543 def _check_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
544 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
545 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
546 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
547 "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
548 if (self.callback_args is not None and
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
549 type(self.callback_args) is not types.TupleType):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
550 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
551 "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
552 % 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
553 if (self.callback_kwargs is not None and
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
554 type(self.callback_kwargs) is not types.DictType):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
555 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
556 "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
557 % 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
558 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
559 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
560 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
561 "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
562 % 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
563 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
564 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
565 "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
566 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
567 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
568 "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
569
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
570
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
571 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
572 _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
573 _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
574 _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
575 _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
576 _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
577 _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
578
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
579
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
580 # -- 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
581
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
582 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
583 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
584
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
585 def takes_value (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 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
587
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
588
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
589 # -- 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
590
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
591 def check_value (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
592 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
593 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
594 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
595 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
596 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
597
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 def process (self, 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
599
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
600 # 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
601 # value(s) are bogus.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
602 if value 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
603 if 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
604 value = self.check_value(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
605 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 value = tuple([self.check_value(opt, v) for v in value])
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
607
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
608 # 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
609 # 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
610 # 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
611 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
612 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
613
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
614 def take_action (self, action, 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
615 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
616 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
617 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
618 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
619 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
620 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
621 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
622 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
623 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
624 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
625 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
626 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
627 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
628 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
629 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
630 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
631 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
632 parser.print_help()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
633 sys.exit(0)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 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
635 parser.print_version()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
636 sys.exit(0)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
637 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
638 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
639
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
640 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
641
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
642 # 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
643
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
644
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
645 def get_prog_name ():
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
646 return os.path.basename(sys.argv[0])
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
647
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
648
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
649 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
650 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
651
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
652 STD_HELP_OPTION = Option("-h", "--help",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
653 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
654 help="show this help message 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
655 STD_VERSION_OPTION = Option("--version",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
656 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
657 help="show program's version number 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
658
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
659
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
660 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
661
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
662 def __init__ (self, defaults=None):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 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
664 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
665 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
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 def __repr__ (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 return ("<%s at 0x%x: %r>"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
669 % (self.__class__.__name__, id(self), self.__dict__))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
670
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
671 def _update_careful (self, dict):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
672 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
673 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
674 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
675 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
676 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
677 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
678 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
679 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
680 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
681 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
682 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
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 def _update_loose (self, dict):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
685 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
686 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
687 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
688 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
689 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
690 self.__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
691
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
692 def _update (self, dict, mode):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 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
694 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
695 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
696 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
697 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
698 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
699
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
700 def read_module (self, modname, 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
701 __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
702 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
703 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
704
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
705 def read_file (self, filename, 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
706 vars = {}
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
707 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
708 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
709
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
710 def ensure_value (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
711 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
712 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
713 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
714
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
715
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
716 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
717
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
718 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
719 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
720
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
721 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
722 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
723 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
724 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
725
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
726 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
727 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
728 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
729 _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
730 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
731 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
732 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
733 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
734 _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
735 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
736 "--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
737 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
738 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
739 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
740 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
741 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
742
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
743 [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
744 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
745
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
746 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
747
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
748 def __init__ (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
749 # 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
750 # 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
751 # 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
752 # 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
753 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
754
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
755 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
756 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
757 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
758
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
759 def _create_option_mappings (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
760 # 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
761 # 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
762 # 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
763 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
764 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
765 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
766
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
767
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
768 def _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
769 # 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
770 # 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
771 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
772 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
773 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
774
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
775 def set_conflict_handler (self, handler):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 if handler not in ("ignore", "error", "resolve"):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
777 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
778 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
779
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
780 def set_description (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
781 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
782
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
783
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
784 # -- 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
785
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
786 def _check_conflict (self, option):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
787 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
788 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
789 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
790 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
791 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
792 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
793 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
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 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
796 handler = self.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
797 if handler == "ignore": # behaviour for Optik 1.0, 1.1
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
798 pass
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
799 elif handler == "error": # new in 1.2
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
800 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
801 "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
802 % ", ".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
803 option)
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 handler == "resolve": # new in 1.2
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 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
806 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
807 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
808 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
809 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
810 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
811 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
812 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
813 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
814
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
815 def add_option (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
816 """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
817 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
818 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
819 if type(args[0]) is types.StringType:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
820 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
821 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
822 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
823 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
824 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
825 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
826 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
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 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
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 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
831 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
832 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
833 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
834 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
835 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
836
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
837 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
838 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
839 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
840 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
841 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
842
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
843 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
844
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
845 def add_options (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
846 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
847 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
848
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
849 # -- 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
850
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
851 def get_option (self, 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
852 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
853 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
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 def has_option (self, 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
856 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
857 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
858
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
859 def remove_option (self, 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
860 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
861 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
862 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
863 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
864 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
865
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
866 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
867 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
868 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
869 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
870 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
871
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
872
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
873 # -- 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
874
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
875 def 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
876 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
877 return ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 result = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
879 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
880 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
881 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
882 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
883
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
884 def format_description (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
885 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
886 return formatter.format_description(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
887 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
888 return ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
889
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
890 def 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
891 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
892 desc = 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
893 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
894 desc = ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
895 return desc + 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
896
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
897
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
898 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
899
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
900 def __init__ (self, parser, title, description=None):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
901 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
902 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
903 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
904 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
905
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
906 def _create_option_list (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
907 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
908 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
909
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
910 def set_title (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
911 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
912
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
913 # -- 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
914
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
915 def 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
916 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
917 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
918 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
919 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
920 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
921
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
922
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
923 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
924
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
925 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
926 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
927 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
928 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
929 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
930
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
931 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
932 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
933 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
934 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
935 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
936 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
937 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
938 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
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 allow_interspersed_args : boolean = true
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 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
942 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
943 -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
944 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
945 -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
946 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
947 -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
948 -- 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
949 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
950 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
951 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
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 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
954 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
955 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
956 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
957 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
958 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
959 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
960 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
961 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
962 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
963 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
964 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
965
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
966 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
967 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
968 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
969 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
970
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
972
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 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
974
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
975 def __init__ (self,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
976 usage=None,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
977 option_list=None,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
978 option_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
979 version=None,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
980 conflict_handler="error",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 description=None,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
982 formatter=None,
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
983 add_help_option=1,
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
984 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
985 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
986 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
987 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
988 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
989 self.version = version
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
990 self.allow_interspersed_args = 1
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
991 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
992 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
993 self.formatter = formatter
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
994
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 # 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
996 # standard_option_list class attribute, the '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
997 # argument, and the STD_VERSION_OPTION (if 'version' supplied)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
998 # and STD_HELP_OPTION globals.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
999 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
1000 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
1001
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1002 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
1003
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 # -- 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
1005 # (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
1006
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1007 def _create_option_list (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1008 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
1009 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
1010 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
1011
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1012 def _populate_option_list (self, option_list, add_help=1):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1013 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
1014 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
1015 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
1016 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
1017 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
1018 self.add_option(STD_VERSION_OPTION)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1019 if add_help:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1020 self.add_option(STD_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
1021
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1022 def _init_parsing_state (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 # 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
1024 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
1025 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
1026 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
1027
31858
0d8ee28bf6f0 Fix support for the "prog" keyword to the OptionParser constructor, as well
Fred Drake <fdrake@acm.org>
parents: 31745
diff changeset
1028 def _get_prog_name(self):
0d8ee28bf6f0 Fix support for the "prog" keyword to the OptionParser constructor, as well
Fred Drake <fdrake@acm.org>
parents: 31745
diff changeset
1029 if self.prog:
0d8ee28bf6f0 Fix support for the "prog" keyword to the OptionParser constructor, as well
Fred Drake <fdrake@acm.org>
parents: 31745
diff changeset
1030 return self.prog
0d8ee28bf6f0 Fix support for the "prog" keyword to the OptionParser constructor, as well
Fred Drake <fdrake@acm.org>
parents: 31745
diff changeset
1031 else:
0d8ee28bf6f0 Fix support for the "prog" keyword to the OptionParser constructor, as well
Fred Drake <fdrake@acm.org>
parents: 31745
diff changeset
1032 return get_prog_name()
26224
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 # -- 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
1035
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 def set_usage (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
1037 if usage 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
1038 self.usage = "%prog [options]"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 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
1040 self.usage = None
31745
8bf163d06d29 [Patch #900071] Be case-insensitive when removing 'usage:' string
Andrew M. Kuchling <amk@amk.ca>
parents: 31424
diff changeset
1041 elif usage.lower().startswith("usage: "):
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042 # for backwards compatibility with Optik 1.3 and earlier
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1043 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
1044 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1045 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
1046
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1047 def enable_interspersed_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
1048 self.allow_interspersed_args = 1
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1049
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1050 def disable_interspersed_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
1051 self.allow_interspersed_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
1052
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 def set_default (self, 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
1054 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
1055
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1056 def set_defaults (self, **kwargs):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1057 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
1058
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 def get_default_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
1060 return Values(self.defaults)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1061
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1062
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1063 # -- 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
1064
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1065 def add_option_group (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
1066 # XXX lots of overlap with OptionContainer.add_option()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1067 if type(args[0]) is types.StringType:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1068 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
1069 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
1070 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
1071 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
1072 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
1073 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
1074 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
1075 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1076 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
1077
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1078 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
1079 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
1080
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1081 def get_option_group (self, 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
1082 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
1083 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
1084 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
1085 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
1086 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
1087
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1088
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1089 # -- 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
1090
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1091 def _get_args (self, args):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1092 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
1093 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
1094 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 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
1096
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1097 def parse_args (self, args=None, 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
1098 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1099 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
1100 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
1101 -> (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
1102
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1103 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
1104 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
1105 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
1106 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
1107 (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
1108 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
1109 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
1110 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1111 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
1112 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
1113 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
1114
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1115 # 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
1116 # 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
1117 # rargs
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1118 # 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
1119 # "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
1120 # largs
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1121 # 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
1122 # 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
1123 # 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
1124 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
1125 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
1126 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
1127
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1128 try:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1129 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
1130 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
1131 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
1132
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1133 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
1134 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
1135
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1136 def check_values (self, 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
1137 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1138 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
1139 -> (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
1140
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1141 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
1142 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
1143 (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
1144 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
1145 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
1146 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1147 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
1148
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1149 def _process_args (self, 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
1150 """_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
1151 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
1152 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
1153
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1154 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
1155 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
1156 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
1157 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
1158 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1159 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
1160 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
1161 # 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
1162 # 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
1163 # 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
1164 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
1165 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
1166 return
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 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
1168 # 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
1169 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
1170 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
1171 # 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
1172 # 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
1173 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
1174 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
1175 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
1176 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
1177 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1178 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
1179
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1180 # 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
1181 # [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
1182 # ^
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1183 # (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
1184 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1185 # 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
1186 # [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
1187 # 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
1188 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1189 # 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
1190 # 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
1191 # 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
1192 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1193 # 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
1194 # 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
1195 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1196 # 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
1197 # *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
1198 # 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
1199
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200 def _match_long_opt (self, opt):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201 """_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
1202
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1203 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
1204 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
1205 '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
1206 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1207 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
1208
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1209 def _process_long_opt (self, 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
1210 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
1211
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1212 # 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
1213 # argument.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1214 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
1215 (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
1216 rargs.insert(0, next_arg)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1217 had_explicit_value = 1
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1218 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1219 opt = arg
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1220 had_explicit_value = 0
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1221
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1222 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
1223 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
1224 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
1225 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
1226 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
1227 if 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
1228 self.error("%s option requires a value" % opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1230 self.error("%s option requires %d values"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1231 % (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
1232 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
1233 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
1234 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1235 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
1236 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
1237
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1238 elif had_explicit_value:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1239 self.error("%s option does not take a value" % opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1240
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1241 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1242 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
1243
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1244 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
1245
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1246 def _process_short_opts (self, 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
1247 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
1248 stop = 0
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1249 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
1250 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
1251 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
1252 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
1253 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
1254
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1255 if not option:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1256 self.error("no such option: %s" % opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1257 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
1258 # 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
1259 # 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
1260 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
1261 rargs.insert(0, 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
1262 stop = 1
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1263
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1264 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
1265 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
1266 if 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
1267 self.error("%s option requires a value" % opt)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1268 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1269 self.error("%s option requires %s values"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1270 % (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
1271 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
1272 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
1273 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1274 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
1275 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
1276
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1277 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
1278 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
1279
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1280 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
1281
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1282 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
1283 break
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1284
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 # -- 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
1287
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1288 def error (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
1289 """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
1290
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1291 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
1292 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
1293 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
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 self.print_usage(sys.stderr)
31858
0d8ee28bf6f0 Fix support for the "prog" keyword to the OptionParser constructor, as well
Fred Drake <fdrake@acm.org>
parents: 31745
diff changeset
1296 sys.exit("%s: error: %s" % (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
1297
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1298 def get_usage (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1299 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
1300 return self.formatter.format_usage(
31858
0d8ee28bf6f0 Fix support for the "prog" keyword to the OptionParser constructor, as well
Fred Drake <fdrake@acm.org>
parents: 31745
diff changeset
1301 self.usage.replace("%prog", self._get_prog_name()))
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1302 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1303 return ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1304
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1305 def print_usage (self, file=None):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1306 """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
1307
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1308 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
1309 '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
1310 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
1311 (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
1312 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
1313 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1314 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
1315 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
1316
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1317 def get_version (self):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1318 if self.version:
31858
0d8ee28bf6f0 Fix support for the "prog" keyword to the OptionParser constructor, as well
Fred Drake <fdrake@acm.org>
parents: 31745
diff changeset
1319 return self.version.replace("%prog", self._get_prog_name())
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1320 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1321 return ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1322
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1323 def print_version (self, file=None):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1324 """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
1325
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326 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
1327 '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
1328 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
1329 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
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 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
1332 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
1333
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1334 def format_option_help (self, formatter=None):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1335 if 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
1336 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
1337 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
1338 result = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1339 result.append(formatter.format_heading("options"))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1340 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
1341 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
1342 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
1343 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
1344 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
1345 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
1346 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
1347 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
1348 # 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
1349 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
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 def format_help (self, formatter=None):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1352 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
1353 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
1354 result = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1355 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
1356 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
1357 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
1358 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
1359 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
1360 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
1361
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1362 def print_help (self, file=None):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1363 """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
1364
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1365 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
1366 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
1367 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1368 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
1369 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
1370 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
1371
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1372 # 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
1373
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1374
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1375 def _match_abbrev (s, wordmap):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1376 """_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
1377
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1378 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
1379 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
1380 '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
1381 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1382 # 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
1383 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
1384 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
1385 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1386 # 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
1387 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
1388 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
1389 # 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
1390 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
1391 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
1392 elif not possibilities:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1393 raise BadOptionError("no such option: %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
1394 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1395 # More than one possible completion: ambiguous prefix.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1396 raise BadOptionError("ambiguous option: %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
1397 % (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
1398
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1399
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1400 # 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
1401 # 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
1402 # 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
1403 # classes.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404 make_option = Option