annotate Lib/optparse.py @ 28646:193b8aec00ce legacy-trunk

Changes from Jonathan Riehl to allow his pgen extension (PEP 269) to work. This includes some more code that used to be part of pgen in the main parser; I'm okay with that. I'll see if the Windows build needs work next.
author Guido van Rossum <guido@python.org>
date Thu, 17 Apr 2003 14:55:42 +0000
parents 388e54747740
children be14970a9955
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
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 Originally distributed as Optik.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
6
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 See http://optik.sourceforge.net/
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 __copyright__ = """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 Copyright (c) 2001-2002 Gregory P. Ward. All rights reserved.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 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
14 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
15 met:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
16
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 * 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
18 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
19
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 * 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
21 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
22 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
23
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 * 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
25 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
26 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
27
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 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
29 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
30 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
31 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
32 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
33 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
34 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
35 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
36 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
37 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
38 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
39 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 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
42 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
43 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
44
27091
388e54747740 Tweak __version__ -- the current code is between Optik 1.4 and 1.4.1.
Greg Ward <gward@python.net>
parents: 26224
diff changeset
45 __version__ = "1.4+"
26224
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 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
48 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
49 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
50
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 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
52 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
53
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 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
55 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 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
57 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
58 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
59
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 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
61 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
62 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
63
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 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
65 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
66 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
67 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 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
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 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
71 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 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
73 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 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
76 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 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
78 line.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
80
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 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
82 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 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
84 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 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
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 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
89 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
90 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
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 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
93 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
94 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
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 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
107 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
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
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 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
111 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
112 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
113 width,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 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
115 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
116 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
117 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
118 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
119 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
120 self.help_width = width - 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
121 if 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
122 self.format_option_strings = self.format_option_strings_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
123 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
124 self.format_option_strings = self.format_option_strings_long_first
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 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
127 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
128 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
129
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 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
131 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
132 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
133 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
134
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 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
136 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
137
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 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
139 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
140
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 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
142 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
143 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
144 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
145 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
146 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
147
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 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
149 # 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
150 # * 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
151 # 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
152 # * 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
153 # 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
154 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 # 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
156 # -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
157 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 # 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
159 # 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
160 # 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
161 # -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
162 # 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
163 result = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 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
165 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
166 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
167 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
168 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
169 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
170 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
171 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
172 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
173 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
174 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
175 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
176 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
177 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
178 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
179 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
180 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
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 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
183 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
184 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
185 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
186 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
187 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
188 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
189 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
190 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
191 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
192 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
193 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
194 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
195 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
196 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
197 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
198
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 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
200 """Return a comma-separated list of option strings & metavariables."""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 raise NotImplementedError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 "abstract method: use format_option_strings_short_first or "
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 "format_option_strings_long_first instead.")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
204
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 def format_option_strings_short_first (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
206 opts = [] # list of "-a" or "--foo=FILE" strings
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 takes_value = 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
208 if 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
209 metavar = option.metavar or option.dest.upper()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 for sopt 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
211 opts.append(sopt + metavar)
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 lopt 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
213 opts.append(lopt + "=" + metavar)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 for opt in option._short_opts + 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
216 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
217 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
218
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
219 def format_option_strings_long_first (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
220 opts = [] # list of "-a" or "--foo=FILE" 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 takes_value = 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
222 if 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
223 metavar = option.metavar or option.dest.upper()
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
224 for lopt 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
225 opts.append(lopt + "=" + metavar)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
226 for sopt 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
227 opts.append(sopt + metavar)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
229 for opt in option._long_opts + 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
230 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
231 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
232
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
233
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
234 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
235 """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
236 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 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
239 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
240 max_help_position=24,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 width=80,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 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
243 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
244 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
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 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
247 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
248
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 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
250 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
251
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
252
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 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
254 """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
255 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
256
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 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
258 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
259 max_help_position=24,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 width=80,
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 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
262 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
263 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
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 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
266 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
267
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 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
269 return "%s\n%s\n" % (heading, "=-"[self.level] * len(heading))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 _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
271 "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
272 "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
273 "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
274
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 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
276 (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
277 try:
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 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
279 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
280 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
281 #"%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
282 "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
283
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 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
285 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
286 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
287 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
288 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
289 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
290 "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
291 % (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
292
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
293 # 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
294 # 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
295 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
296
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
297
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
298 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
299 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
300 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
301 _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
302 _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
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 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
305 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
306 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
307 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
308 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
309 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
310 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
311 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
312 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
313 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
314 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
315 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
316 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
317
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 # 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
319 # 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
320 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
321 'type',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 'dest',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 'default',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
324 'nargs',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 'const',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 'choices',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
327 'callback',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 '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
329 '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
330 'help',
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 'metavar']
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
332
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 # 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
334 # 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
335 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
336 "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
337 "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
338 "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
339 "append",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 "count",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 "callback",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 "help",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 "version")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
344
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 # 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
346 # 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
347 # 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
348 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
349 "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
350 "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
351 "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
352 "append",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 "count")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
354
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 # 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
356 # 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
357 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
358 "append",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 "callback")
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
360
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 # 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
362 # 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
363 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
364
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
365 # 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
366 # 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
367 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
368 # 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
369 # 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
370 # where
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 # 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
372 # 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
373 # (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
374 # 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
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 # 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
377 # 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
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 # 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
380 # 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
381 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
382 "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
383 "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
384 "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
385 "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
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
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
388
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 # 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
390 # 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
391 # 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
392 # 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
393 # 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
394 # 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
395 # 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
396 # 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
397 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
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 # -- 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
401
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 def __init__ (self, *opts, **attrs):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 # Set _short_opts, _long_opts attrs from 'opts' tuple
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 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
405 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
406
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 # 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
408 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
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 # 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
411 # 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
412 # 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
413 # 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
414 # 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
415 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
416 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
417
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 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
419 # 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
420 # 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
421 # 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
422 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
423 if not opts:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 raise OptionError("at least one option string must be supplied",
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
426 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
427
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
428 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
429 self._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
430 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
431 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
432 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
433 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
434 "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
435 "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
436 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
437 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
438 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
439 "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
440 "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
441 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
442 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
443 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
444 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
445 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
446 "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
447 "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
448 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
449 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
450
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
451 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
452 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
453 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
454 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
455 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
456 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
457 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
458 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
459 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
460 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
461 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
462 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
463 "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
464 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
465
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
466
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
467 # -- 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
468
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
469 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
470 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
471 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
472 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
473 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
474
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
475 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
476 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
477 # 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
478 # 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
479 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
480 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
481 # 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
482 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
483 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
484 # 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
485 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
486 else:
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 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
488 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
489 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
490 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
491 "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
492
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
493 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
494 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
495 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
496 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
497 "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
498 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
499 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
500 "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
501 % 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
502 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
503 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
504 "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
505
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
506 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
507 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
508 # 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
509 # 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
510 # 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
511 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
512 # 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
513 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
514 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
515 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
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_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
518 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
519 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
520 "'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
521 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
522
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
523 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
524 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
525 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
526 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
527 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
528 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
529 "'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
530 self)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
531
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
532 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
533 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
534 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
535 raise OptionError(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
536 "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
537 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
538 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
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 "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
541 % 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
542 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
543 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
544 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
545 "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
546 % 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
547 else:
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 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
549 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
550 "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
551 % 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
552 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
553 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
554 "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
555 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
556 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
557 "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
558
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
559
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
560 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
561 _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
562 _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
563 _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
564 _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
565 _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
566 _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
567
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
568
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
569 # -- 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
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 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
572 if self._short_opts or 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
573 return "/".join(self._short_opts + 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
574 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
575 raise RuntimeError, "short_opts and long_opts both empty!"
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
576
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
577 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
578 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
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
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
581 # -- 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
582
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
583 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
584 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
585 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
586 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
587 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
588 return 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
589
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
590 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
591
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
592 # 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
593 # 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
594 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
595 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
596 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
597 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
598 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
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 # 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
601 # 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
602 # 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
603 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
604 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
605
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
606 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
607 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
608 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
609 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
610 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
611 elif action == "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
612 setattr(values, dest, 1)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
613 elif action == "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
614 setattr(values, dest, 0)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
615 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
616 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
617 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
618 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
619 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
620 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
621 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
622 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
623 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
624 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
625 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
626 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
627 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
628 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
629 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
630 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
631
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
632 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
633
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
634 # 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
635 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
636 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
637
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
638
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
639 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
640 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
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 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
643 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
644 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
645 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
646 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
647 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
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
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
650 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
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 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
653 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
654 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
655 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
656
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
657 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
658 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
659 % (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
660
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
661 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
662 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
663 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
664 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
665 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
666 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
667 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
668 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
669 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
670 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
671 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
672 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
673
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
674 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
675 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
676 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
677 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
678 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
679 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
680 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
681
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
682 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
683 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
684 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
685 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
686 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
687 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
688 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
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 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
691 __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
692 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
693 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
694
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
695 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
696 vars = {}
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
697 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
698 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
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 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
701 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
702 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
703 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
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
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
706 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
707
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
708 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
709 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
710
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
711 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
712 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
713 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
714 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
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 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
717 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
718 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
719 _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
720 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
721 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
722 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
723 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
724 _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
725 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
726 "--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
727 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
728 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
729 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
730 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
731 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
732
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
733 [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
734 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
735
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
736 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
737
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
738 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
739 # 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
740 # 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
741 # 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
742 # 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
743 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
744
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
745 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
746 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
747 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
748
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
749 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
750 # 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
751 # 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
752 # 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
753 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
754 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
755 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
756
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
757
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
758 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
759 # 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
760 # 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
761 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
762 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
763 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
764
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
765 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
766 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
767 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
768 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
769
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
770 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
771 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
772
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
773
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
774 # -- 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
775
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
776 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
777 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
778 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
779 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
780 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
781 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
782 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
783 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
784
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
785 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
786 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
787 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
788 pass
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
789 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
790 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
791 "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
792 % ", ".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
793 option)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
794 elif 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
795 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
796 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
797 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
798 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
799 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
800 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
801 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
802 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
803 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
804
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
805 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
806 """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
807 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
808 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
809 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
810 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
811 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
812 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
813 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
814 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
815 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
816 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
817
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
818 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
819
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
820 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
821 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
822 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
823 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
824 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
825 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
826
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
827 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
828 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
829 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
830 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
831 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
832
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
833 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
834
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
835 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
836 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
837 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
838
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
839 # -- 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
840
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
841 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
842 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
843 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
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 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
846 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
847 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
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 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
850 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
851 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
852 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
853 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
854 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
855
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
856 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
857 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
858 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
859 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
860 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
861
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
862
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
863 # -- 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
864
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
865 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
866 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
867 return ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
868 result = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
869 for 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
870 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
871 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
872 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
873
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
874 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
875 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
876 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
877 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
878 return ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
879
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
880 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
881 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
882 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
883 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
884 desc = ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
885 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
886
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
887
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
888 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
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 __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
891 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
892 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
893 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
894 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
895
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
896 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
897 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
898 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
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 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
901 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
902
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
903 # -- 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
904
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
905 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
906 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
907 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
908 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
909 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
910 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
911
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 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
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 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
916 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
917 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
918 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
919 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
920
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
921 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
922 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
923 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
924 to the user, "%prog" will be expanded to the name of
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
925 your program (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
926
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
927 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
928 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
929 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
930 -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
931 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
932 -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
933 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
934 -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
935 -- 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
936 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
937 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
938 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
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 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
941 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
942 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
943 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
944 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
945 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
946 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
947 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
948 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
949 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
950 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
951 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
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 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
954 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
955 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
956 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
957
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
958 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
959
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
960 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
961
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
962 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
963 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
964 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
965 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
966 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
967 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
968 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
969 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
970 add_help_option=1):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 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
972 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
973 self.set_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
974 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
975 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
976 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
977 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
978 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
979
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
980 # 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
981 # 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
982 # 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
983 # 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
984 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
985 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
986
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._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
988
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
989 # -- 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
990 # (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
991
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
992 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
993 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
994 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
995 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
996
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
997 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
998 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
999 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
1000 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
1001 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
1002 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
1003 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
1004 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
1005 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
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 _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
1008 # 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
1009 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
1010 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
1011 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
1012
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1013
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1014 # -- 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
1015
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1016 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
1017 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
1018 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
1019 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
1020 self.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
1021 elif usage.startswith("usage: "):
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1022 # 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
1023 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
1024 else:
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.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
1026
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027 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
1028 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
1029
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1030 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
1031 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
1032
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 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
1034 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
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_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
1037 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
1038
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 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
1040 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
1041
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1043 # -- 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
1044
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1045 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
1046 # 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
1047 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
1048 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
1049 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
1050 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
1051 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
1052 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
1053 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
1054 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
1055 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1056 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
1057
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1058 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
1059 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
1060
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1061 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
1062 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
1063 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
1064 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
1065 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
1066 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
1067
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1068
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1069 # -- 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
1070
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1071 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
1072 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
1073 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
1074 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1075 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
1076
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1077 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
1078 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1079 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
1080 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
1081 -> (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
1082
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1083 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
1084 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
1085 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
1086 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
1087 (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
1088 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
1089 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
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 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
1092 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
1093 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
1094
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 # 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
1096 # 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
1097 # rargs
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1098 # 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
1099 # "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
1100 # largs
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1101 # 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
1102 # 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
1103 # 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
1104 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
1105 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
1106 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
1107
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1108 try:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1109 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
1110 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
1111 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
1112
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1113 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
1114 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
1115
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1116 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
1117 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1118 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
1119 -> (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
1120
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1121 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
1122 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
1123 (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
1124 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
1125 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
1126 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1127 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
1128
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1129 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
1130 """_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
1131 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
1132 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
1133
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1134 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
1135 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
1136 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
1137 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
1138 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1139 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
1140 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
1141 # 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
1142 # 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
1143 # 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
1144 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
1145 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
1146 return
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1147 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
1148 # 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
1149 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
1150 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
1151 # 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
1152 # 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
1153 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
1154 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
1155 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
1156 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
1157 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1158 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
1159
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1160 # 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
1161 # [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
1162 # ^
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1163 # (we 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
1164 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1165 # 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
1166 # [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
1167 # 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
1168 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169 # 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
1170 # 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
1171 # 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
1172 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1173 # 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
1174 # 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
1175 #
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1176 # 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
1177 # *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
1178 # 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
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 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
1181 """_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
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 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
1184 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
1185 '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
1186 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1187 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
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 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
1190 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
1191
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192 # 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
1193 # argument.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1194 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
1195 (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
1196 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
1197 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
1198 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1199 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
1200 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
1201
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1202 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
1203 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
1204 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
1205 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
1206 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
1207 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
1208 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
1209 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1210 self.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
1211 % (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
1212 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
1213 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
1214 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1215 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
1216 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
1217
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1218 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
1219 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
1220
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1221 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1222 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
1223
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1224 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
1225
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1226 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
1227 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
1228 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
1229 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
1230 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
1231 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
1232 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
1233 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
1234
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1235 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
1236 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
1237 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
1238 # 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
1239 # 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
1240 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
1241 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
1242 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
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 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
1245 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
1246 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
1247 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
1248 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1249 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
1250 % (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
1251 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
1252 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
1253 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1254 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
1255 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
1256
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1257 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
1258 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
1259
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1260 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
1261
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1262 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
1263 break
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1264
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1265
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1266 # -- 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
1267
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1268 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
1269 """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
1270
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1271 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
1272 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
1273 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
1274 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1275 self.print_usage(sys.stderr)
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1276 sys.exit("%s: error: %s" % (get_prog_name(), msg))
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1277
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1278 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
1279 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
1280 return self.formatter.format_usage(
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1281 self.usage.replace("%prog", 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
1282 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1283 return ""
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 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
1286 """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
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 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
1289 '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
1290 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
1291 (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
1292 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
1293 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1294 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
1295 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
1296
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1297 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
1298 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
1299 return self.version.replace("%prog", 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
1300 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1301 return ""
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1302
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1303 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
1304 """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
1305
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 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
1307 '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
1308 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
1309 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
1310 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1311 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
1312 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
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 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
1315 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
1316 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
1317 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
1318 result = []
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1319 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
1320 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
1321 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
1322 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
1323 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
1324 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
1325 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
1326 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
1327 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
1328 # 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
1329 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
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 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
1332 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
1333 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
1334 result = []
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 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
1336 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
1337 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
1338 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
1339 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
1340 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
1341
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1342 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
1343 """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
1344
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1345 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
1346 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
1347 """
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1348 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
1349 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
1350 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
1351
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1352 # 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
1353
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1354
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1355 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
1356 """_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
1357
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1358 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
1359 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
1360 '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
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 # 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
1363 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
1364 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
1365 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1366 # 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
1367 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
1368 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
1369 # 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
1370 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
1371 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
1372 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
1373 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
1374 else:
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1375 # 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
1376 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
1377 % (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
1378
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1379
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1380 # 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
1381 # 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
1382 # 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
1383 # classes.
adb93f4e9780 Checking in Greg Ward's Optik, as optparse.py. This is the most
Guido van Rossum <guido@python.org>
parents:
diff changeset
1384 make_option = Option