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-option-attributes.html" /> | |
13 | <link rel="prev" href="optparse-defining-options.html" /> | |
14 | <link rel="parent" href="optparse-reference-guide.html" /> | |
15 | <link rel="next" href="optparse-option-attributes.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>6.21.3.4 Standard option actions</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.3 Defining options" | |
25 | href="optparse-defining-options.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.5 Option attributes" | |
31 | href="optparse-option-attributes.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-defining-options.html">6.21.3.3 Defining options</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-option-attributes.html">6.21.3.5 Option attributes</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H3><A NAME="SECTION0082134000000000000000"></A><A NAME="optparse-standard-option-actions"></A> | |
56 | <BR> | |
57 | 6.21.3.4 Standard option actions | |
58 | </H3> | |
59 | ||
60 | <P> | |
61 | The various option actions all have slightly different requirements and | |
62 | effects. Most actions have several relevant option attributes which you | |
63 | may specify to guide <tt class="module">optparse</tt>'s behaviour; a few have required attributes, | |
64 | which you must specify for any option using that action. | |
65 | ||
66 | <UL> | |
67 | <LI> | |
68 | <code>store</code> [relevant: <tt class="member">type</tt>, <tt class="member">dest</tt>, <code>nargs</code>, <code>choices</code>] | |
69 | ||
70 | <P> | |
71 | The option must be followed by an argument, which is | |
72 | converted to a value according to <tt class="member">type</tt> and stored in | |
73 | <tt class="member">dest</tt>. If <code>nargs</code> > 1, multiple arguments will be consumed | |
74 | from the command line; all will be converted according to | |
75 | <tt class="member">type</tt> and stored to <tt class="member">dest</tt> as a tuple. See the ``Option | |
76 | types'' section below. | |
77 | ||
78 | <P> | |
79 | If <code>choices</code> is supplied (a list or tuple of strings), the type | |
80 | defaults to <code>choice</code>. | |
81 | ||
82 | <P> | |
83 | If <tt class="member">type</tt> is not supplied, it defaults to <code>string</code>. | |
84 | ||
85 | <P> | |
86 | If <tt class="member">dest</tt> is not supplied, <tt class="module">optparse</tt> derives a destination from the | |
87 | first long option string (e.g., <code>"-foo-bar"</code> implies <code>foo_bar</code>). | |
88 | If there are no long option strings, <tt class="module">optparse</tt> derives a destination from | |
89 | the first short option string (e.g., <code>"-f"</code> implies <code>f</code>). | |
90 | ||
91 | <P> | |
92 | Example: | |
93 | <div class="verbatim"><pre> | |
94 | parser.add_option("-f") | |
95 | parser.add_option("-p", type="float", nargs=3, dest="point") | |
96 | </pre></div> | |
97 | ||
98 | <P> | |
99 | As it parses the command line | |
100 | <div class="verbatim"><pre> | |
101 | -f foo.txt -p 1 -3.5 4 -fbar.txt | |
102 | </pre></div> | |
103 | ||
104 | <P> | |
105 | <tt class="module">optparse</tt> will set | |
106 | <div class="verbatim"><pre> | |
107 | options.f = "foo.txt" | |
108 | options.point = (1.0, -3.5, 4.0) | |
109 | options.f = "bar.txt" | |
110 | </pre></div> | |
111 | ||
112 | <P> | |
113 | </LI> | |
114 | <LI> | |
115 | <code>store_const</code> [required: <code>const</code>; relevant: <tt class="member">dest</tt>] | |
116 | ||
117 | <P> | |
118 | The value <code>const</code> is stored in <tt class="member">dest</tt>. | |
119 | ||
120 | <P> | |
121 | Example: | |
122 | <div class="verbatim"><pre> | |
123 | parser.add_option("-q", "--quiet", | |
124 | action="store_const", const=0, dest="verbose") | |
125 | parser.add_option("-v", "--verbose", | |
126 | action="store_const", const=1, dest="verbose") | |
127 | parser.add_option("--noisy", | |
128 | action="store_const", const=2, dest="verbose") | |
129 | </pre></div> | |
130 | ||
131 | <P> | |
132 | If <code>"-noisy"</code> is seen, <tt class="module">optparse</tt> will set | |
133 | <div class="verbatim"><pre> | |
134 | options.verbose = 2 | |
135 | </pre></div> | |
136 | ||
137 | <P> | |
138 | </LI> | |
139 | <LI> | |
140 | <code>store_true</code> [relevant: <tt class="member">dest</tt>] | |
141 | ||
142 | <P> | |
143 | A special case of <code>store_const</code> that stores a true value | |
144 | to <tt class="member">dest</tt>. | |
145 | ||
146 | <P> | |
147 | </LI> | |
148 | <LI> | |
149 | <code>store_false</code> [relevant: <tt class="member">dest</tt>] | |
150 | ||
151 | <P> | |
152 | Like <code>store_true</code>, but stores a false value. | |
153 | ||
154 | <P> | |
155 | Example: | |
156 | <div class="verbatim"><pre> | |
157 | parser.add_option("--clobber", action="store_true", dest="clobber") | |
158 | parser.add_option("--no-clobber", action="store_false", dest="clobber") | |
159 | </pre></div> | |
160 | ||
161 | <P> | |
162 | </LI> | |
163 | <LI> | |
164 | <code>append</code> [relevant: <tt class="member">type</tt>, <tt class="member">dest</tt>, <code>nargs</code>, <code>choices</code>] | |
165 | ||
166 | <P> | |
167 | The option must be followed by an argument, which is appended to the | |
168 | list in <tt class="member">dest</tt>. If no default value for <tt class="member">dest</tt> is supplied, an | |
169 | empty list is automatically created when <tt class="module">optparse</tt> first encounters this | |
170 | option on the command-line. If <code>nargs</code> > 1, multiple arguments are | |
171 | consumed, and a tuple of length <code>nargs</code> is appended to <tt class="member">dest</tt>. | |
172 | ||
173 | <P> | |
174 | The defaults for <tt class="member">type</tt> and <tt class="member">dest</tt> are the same as for the | |
175 | <code>store</code> action. | |
176 | ||
177 | <P> | |
178 | Example: | |
179 | <div class="verbatim"><pre> | |
180 | parser.add_option("-t", "--tracks", action="append", type="int") | |
181 | </pre></div> | |
182 | ||
183 | <P> | |
184 | If <code>"-t3"</code> is seen on the command-line, <tt class="module">optparse</tt> does the equivalent of: | |
185 | <div class="verbatim"><pre> | |
186 | options.tracks = [] | |
187 | options.tracks.append(int("3")) | |
188 | </pre></div> | |
189 | ||
190 | <P> | |
191 | If, a little later on, <code>"-tracks=4"</code> is seen, it does: | |
192 | <div class="verbatim"><pre> | |
193 | options.tracks.append(int("4")) | |
194 | </pre></div> | |
195 | ||
196 | <P> | |
197 | </LI> | |
198 | <LI> | |
199 | <code>count</code> [relevant: <tt class="member">dest</tt>] | |
200 | ||
201 | <P> | |
202 | Increment the integer stored at <tt class="member">dest</tt>. If no default value is | |
203 | supplied, <tt class="member">dest</tt> is set to zero before being incremented the first | |
204 | time. | |
205 | ||
206 | <P> | |
207 | Example: | |
208 | <div class="verbatim"><pre> | |
209 | parser.add_option("-v", action="count", dest="verbosity") | |
210 | </pre></div> | |
211 | ||
212 | <P> | |
213 | The first time <code>"-v"</code> is seen on the command line, <tt class="module">optparse</tt> does the | |
214 | equivalent of: | |
215 | <div class="verbatim"><pre> | |
216 | options.verbosity = 0 | |
217 | options.verbosity += 1 | |
218 | </pre></div> | |
219 | ||
220 | <P> | |
221 | Every subsequent occurrence of <code>"-v"</code> results in | |
222 | <div class="verbatim"><pre> | |
223 | options.verbosity += 1 | |
224 | </pre></div> | |
225 | ||
226 | <P> | |
227 | </LI> | |
228 | <LI> | |
229 | <code>callback</code> [required: <code>callback</code>; | |
230 | relevant: <tt class="member">type</tt>, <code>nargs</code>, <code>callback_args</code>, <code>callback_kwargs</code>] | |
231 | ||
232 | <P> | |
233 | Call the function specified by <code>callback</code>, which is called as | |
234 | <div class="verbatim"><pre> | |
235 | func(option, opt_str, value, parser, *args, **kwargs) | |
236 | </pre></div> | |
237 | ||
238 | <P> | |
239 | See section <A href="optparse-option-callbacks.html#optparse-option-callbacks">6.21.4</A>, Option Callbacks for more detail. | |
240 | ||
241 | <P> | |
242 | </LI> | |
243 | <LI> | |
244 | <tt class="member">help</tt> | |
245 | ||
246 | <P> | |
247 | Prints a complete help message for all the options in the | |
248 | current option parser. The help message is constructed from | |
249 | the <code>usage</code> string passed to OptionParser's constructor and | |
250 | the <tt class="member">help</tt> string passed to every option. | |
251 | ||
252 | <P> | |
253 | If no <tt class="member">help</tt> string is supplied for an option, it will still be | |
254 | listed in the help message. To omit an option entirely, use | |
255 | the special value <code>optparse.SUPPRESS_HELP</code>. | |
256 | ||
257 | <P> | |
258 | <tt class="module">optparse</tt> automatically adds a <tt class="member">help</tt> option to all OptionParsers, so | |
259 | you do not normally need to create one. | |
260 | ||
261 | <P> | |
262 | Example: | |
263 | <div class="verbatim"><pre> | |
264 | from optparse import OptionParser, SUPPRESS_HELP | |
265 | ||
266 | parser = OptionParser() | |
267 | parser.add_option("-h", "--help", action="help"), | |
268 | parser.add_option("-v", action="store_true", dest="verbose", | |
269 | help="Be moderately verbose") | |
270 | parser.add_option("--file", dest="filename", | |
271 | help="Input file to read data from"), | |
272 | parser.add_option("--secret", help=SUPPRESS_HELP) | |
273 | </pre></div> | |
274 | ||
275 | <P> | |
276 | If <tt class="module">optparse</tt> sees either <code>"-h"</code> or <code>"-help"</code> on the command line, it | |
277 | will print something like the following help message to stdout | |
278 | (assuming <code>sys.argv[0]</code> is <code>"foo.py"</code>): | |
279 | <div class="verbatim"><pre> | |
280 | usage: foo.py [options] | |
281 | ||
282 | options: | |
283 | -h, --help Show this help message and exit | |
284 | -v Be moderately verbose | |
285 | --file=FILENAME Input file to read data from | |
286 | </pre></div> | |
287 | ||
288 | <P> | |
289 | After printing the help message, <tt class="module">optparse</tt> terminates your process | |
290 | with <code>sys.exit(0)</code>. | |
291 | ||
292 | <P> | |
293 | </LI> | |
294 | <LI> | |
295 | <code>version</code> | |
296 | ||
297 | <P> | |
298 | Prints the version number supplied to the OptionParser to stdout and | |
299 | exits. The version number is actually formatted and printed by the | |
300 | <code>print_version()</code> method of OptionParser. Generally only relevant | |
301 | if the <code>version</code> argument is supplied to the OptionParser | |
302 | constructor. As with <tt class="member">help</tt> options, you will rarely create | |
303 | <code>version</code> options, since <tt class="module">optparse</tt> automatically adds them when needed. | |
304 | ||
305 | <P> | |
306 | </LI> | |
307 | </UL> | |
308 | ||
309 | <P> | |
310 | ||
311 | <DIV CLASS="navigation"> | |
312 | <div class='online-navigation'> | |
313 | <p></p><hr /> | |
314 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
315 | <tr> | |
316 | <td class='online-navigation'><a rel="prev" title="6.21.3.3 Defining options" | |
317 | href="optparse-defining-options.html"><img src='../icons/previous.png' | |
318 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
319 | <td class='online-navigation'><a rel="parent" title="6.21.3 Reference Guide" | |
320 | href="optparse-reference-guide.html"><img src='../icons/up.png' | |
321 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
322 | <td class='online-navigation'><a rel="next" title="6.21.3.5 Option attributes" | |
323 | href="optparse-option-attributes.html"><img src='../icons/next.png' | |
324 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
325 | <td align="center" width="100%">Python Library Reference</td> | |
326 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
327 | href="contents.html"><img src='../icons/contents.png' | |
328 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
329 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
330 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
331 | <td class='online-navigation'><a rel="index" title="Index" | |
332 | href="genindex.html"><img src='../icons/index.png' | |
333 | border='0' height='32' alt='Index' width='32' /></A></td> | |
334 | </tr></table> | |
335 | <div class='online-navigation'> | |
336 | <b class="navlabel">Previous:</b> | |
337 | <a class="sectref" rel="prev" href="optparse-defining-options.html">6.21.3.3 Defining options</A> | |
338 | <b class="navlabel">Up:</b> | |
339 | <a class="sectref" rel="parent" href="optparse-reference-guide.html">6.21.3 Reference Guide</A> | |
340 | <b class="navlabel">Next:</b> | |
341 | <a class="sectref" rel="next" href="optparse-option-attributes.html">6.21.3.5 Option attributes</A> | |
342 | </div> | |
343 | </div> | |
344 | <hr /> | |
345 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
346 | </DIV> | |
347 | <!--End of Navigation Panel--> | |
348 | <ADDRESS> | |
349 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
350 | </ADDRESS> | |
351 | </BODY> | |
352 | </HTML> |