Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="optparse-other-methods.html" /> | |
13 | <link rel="prev" href="optparse-querying-manipulating-option-parser.html" /> | |
14 | <link rel="parent" href="optparse-reference-guide.html" /> | |
15 | <link rel="next" href="optparse-other-methods.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.21.3.9 Conflicts between options</title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="6.21.3.8 Querying and manipulating" | |
25 | href="optparse-querying-manipulating-option-parser.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="6.21.3 Reference Guide" | |
28 | href="optparse-reference-guide.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="6.21.3.10 Other methods" | |
31 | href="optparse-other-methods.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="optparse-querying-manipulating-option-parser.html">6.21.3.8 Querying and manipulating</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="optparse-reference-guide.html">6.21.3 Reference Guide</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="optparse-other-methods.html">6.21.3.10 Other methods</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H3><A NAME="SECTION0082139000000000000000"></A><A NAME="optparse-conflicts-between-options"></A> | |
56 | <BR> | |
57 | 6.21.3.9 Conflicts between options | |
58 | </H3> | |
59 | ||
60 | <P> | |
61 | If you're not careful, it's easy to define options with conflicting | |
62 | option strings: | |
63 | <div class="verbatim"><pre> | |
64 | parser.add_option("-n", "--dry-run", ...) | |
65 | [...] | |
66 | parser.add_option("-n", "--noisy", ...) | |
67 | </pre></div> | |
68 | ||
69 | <P> | |
70 | (This is particularly true if you've defined your own OptionParser | |
71 | subclass with some standard options.) | |
72 | ||
73 | <P> | |
74 | Every time you add an option, <tt class="module">optparse</tt> checks for conflicts with existing | |
75 | options. If it finds any, it invokes the current conflict-handling | |
76 | mechanism. You can set the conflict-handling mechanism either in the | |
77 | constructor: | |
78 | <div class="verbatim"><pre> | |
79 | parser = OptionParser(..., conflict_handler=handler) | |
80 | </pre></div> | |
81 | ||
82 | <P> | |
83 | or with a separate call: | |
84 | <div class="verbatim"><pre> | |
85 | parser.set_conflict_handler(handler) | |
86 | </pre></div> | |
87 | ||
88 | <P> | |
89 | The available conflict handlers are: | |
90 | <BLOCKQUOTE> | |
91 | </BLOCKQUOTE><DL> | |
92 | <DT><STRONG><code>error</code> (default)</STRONG></DT> | |
93 | <DD>assume option conflicts are a programming error and raise | |
94 | OptionConflictError | |
95 | </DD> | |
96 | <DT><STRONG><code>resolve</code></STRONG></DT> | |
97 | <DD>resolve option conflicts intelligently (see below) | |
98 | </DD> | |
99 | </DL><BLOCKQUOTE> | |
100 | </BLOCKQUOTE> | |
101 | ||
102 | <P> | |
103 | As an example, let's define an OptionParser that resolves conflicts | |
104 | intelligently and add conflicting options to it: | |
105 | <div class="verbatim"><pre> | |
106 | parser = OptionParser(conflict_handler="resolve") | |
107 | parser.add_option("-n", "--dry-run", ..., help="do no harm") | |
108 | parser.add_option("-n", "--noisy", ..., help="be noisy") | |
109 | </pre></div> | |
110 | ||
111 | <P> | |
112 | At this point, <tt class="module">optparse</tt> detects that a previously-added option is already | |
113 | using the <code>"-n"</code> option string. Since <code>conflict_handler</code> is | |
114 | <code>"resolve"</code>, it resolves the situation by removing <code>"-n"</code> from the | |
115 | earlier option's list of option strings. Now <code>"-dry-run"</code> is the | |
116 | only way for the user to activate that option. If the user asks for | |
117 | help, the help message will reflect that: | |
118 | <div class="verbatim"><pre> | |
119 | options: | |
120 | --dry-run do no harm | |
121 | [...] | |
122 | -n, --noisy be noisy | |
123 | </pre></div> | |
124 | ||
125 | <P> | |
126 | It's possible to whittle away the option strings for a previously-added | |
127 | option until there are none left, and the user has no way of invoking | |
128 | that option from the command-line. In that case, <tt class="module">optparse</tt> removes that | |
129 | option completely, so it doesn't show up in help text or anywhere else. | |
130 | Carrying on with our existing OptionParser: | |
131 | <div class="verbatim"><pre> | |
132 | parser.add_option("--dry-run", ..., help="new dry-run option") | |
133 | </pre></div> | |
134 | ||
135 | <P> | |
136 | At this point, the original <b class="programopt">-n/-dry-run</b> option is no longer | |
137 | accessible, so <tt class="module">optparse</tt> removes it, leaving this help text: | |
138 | <div class="verbatim"><pre> | |
139 | options: | |
140 | [...] | |
141 | -n, --noisy be noisy | |
142 | --dry-run new dry-run option | |
143 | </pre></div> | |
144 | ||
145 | <P> | |
146 | ||
147 | <DIV CLASS="navigation"> | |
148 | <div class='online-navigation'> | |
149 | <p></p><hr /> | |
150 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
151 | <tr> | |
152 | <td class='online-navigation'><a rel="prev" title="6.21.3.8 Querying and manipulating" | |
153 | href="optparse-querying-manipulating-option-parser.html"><img src='../icons/previous.png' | |
154 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
155 | <td class='online-navigation'><a rel="parent" title="6.21.3 Reference Guide" | |
156 | href="optparse-reference-guide.html"><img src='../icons/up.png' | |
157 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
158 | <td class='online-navigation'><a rel="next" title="6.21.3.10 Other methods" | |
159 | href="optparse-other-methods.html"><img src='../icons/next.png' | |
160 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
161 | <td align="center" width="100%">Python Library Reference</td> | |
162 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
163 | href="contents.html"><img src='../icons/contents.png' | |
164 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
165 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
166 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
167 | <td class='online-navigation'><a rel="index" title="Index" | |
168 | href="genindex.html"><img src='../icons/index.png' | |
169 | border='0' height='32' alt='Index' width='32' /></A></td> | |
170 | </tr></table> | |
171 | <div class='online-navigation'> | |
172 | <b class="navlabel">Previous:</b> | |
173 | <a class="sectref" rel="prev" href="optparse-querying-manipulating-option-parser.html">6.21.3.8 Querying and manipulating</A> | |
174 | <b class="navlabel">Up:</b> | |
175 | <a class="sectref" rel="parent" href="optparse-reference-guide.html">6.21.3 Reference Guide</A> | |
176 | <b class="navlabel">Next:</b> | |
177 | <a class="sectref" rel="next" href="optparse-other-methods.html">6.21.3.10 Other methods</A> | |
178 | </div> | |
179 | </div> | |
180 | <hr /> | |
181 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
182 | </DIV> | |
183 | <!--End of Navigation Panel--> | |
184 | <ADDRESS> | |
185 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
186 | </ADDRESS> | |
187 | </BODY> | |
188 | </HTML> |