Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / optparse-standard-option-actions.html
CommitLineData
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>
576.21.3.4 Standard option actions
58</H3>
59
60<P>
61The various option actions all have slightly different requirements and
62effects. Most actions have several relevant option attributes which you
63may specify to guide <tt class="module">optparse</tt>'s behaviour; a few have required attributes,
64which 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>
71The option must be followed by an argument, which is
72converted to a value according to <tt class="member">type</tt> and stored in
73<tt class="member">dest</tt>. If <code>nargs</code> &gt; 1, multiple arguments will be consumed
74from 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
76types'' section below.
77
78<P>
79If <code>choices</code> is supplied (a list or tuple of strings), the type
80defaults to <code>choice</code>.
81
82<P>
83If <tt class="member">type</tt> is not supplied, it defaults to <code>string</code>.
84
85<P>
86If <tt class="member">dest</tt> is not supplied, <tt class="module">optparse</tt> derives a destination from the
87first long option string (e.g., <code>"-foo-bar"</code> implies <code>foo_bar</code>).
88If there are no long option strings, <tt class="module">optparse</tt> derives a destination from
89the first short option string (e.g., <code>"-f"</code> implies <code>f</code>).
90
91<P>
92Example:
93<div class="verbatim"><pre>
94parser.add_option("-f")
95parser.add_option("-p", type="float", nargs=3, dest="point")
96</pre></div>
97
98<P>
99As 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>
107options.f = "foo.txt"
108options.point = (1.0, -3.5, 4.0)
109options.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>
118The value <code>const</code> is stored in <tt class="member">dest</tt>.
119
120<P>
121Example:
122<div class="verbatim"><pre>
123parser.add_option("-q", "--quiet",
124 action="store_const", const=0, dest="verbose")
125parser.add_option("-v", "--verbose",
126 action="store_const", const=1, dest="verbose")
127parser.add_option("--noisy",
128 action="store_const", const=2, dest="verbose")
129</pre></div>
130
131<P>
132If <code>"-noisy"</code> is seen, <tt class="module">optparse</tt> will set
133<div class="verbatim"><pre>
134options.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>
143A special case of <code>store_const</code> that stores a true value
144to <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>
152Like <code>store_true</code>, but stores a false value.
153
154<P>
155Example:
156<div class="verbatim"><pre>
157parser.add_option("--clobber", action="store_true", dest="clobber")
158parser.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>
167The option must be followed by an argument, which is appended to the
168list in <tt class="member">dest</tt>. If no default value for <tt class="member">dest</tt> is supplied, an
169empty list is automatically created when <tt class="module">optparse</tt> first encounters this
170option on the command-line. If <code>nargs</code> &gt; 1, multiple arguments are
171consumed, and a tuple of length <code>nargs</code> is appended to <tt class="member">dest</tt>.
172
173<P>
174The 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>
178Example:
179<div class="verbatim"><pre>
180parser.add_option("-t", "--tracks", action="append", type="int")
181</pre></div>
182
183<P>
184If <code>"-t3"</code> is seen on the command-line, <tt class="module">optparse</tt> does the equivalent of:
185<div class="verbatim"><pre>
186options.tracks = []
187options.tracks.append(int("3"))
188</pre></div>
189
190<P>
191If, a little later on, <code>"-tracks=4"</code> is seen, it does:
192<div class="verbatim"><pre>
193options.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>
202Increment the integer stored at <tt class="member">dest</tt>. If no default value is
203supplied, <tt class="member">dest</tt> is set to zero before being incremented the first
204time.
205
206<P>
207Example:
208<div class="verbatim"><pre>
209parser.add_option("-v", action="count", dest="verbosity")
210</pre></div>
211
212<P>
213The first time <code>"-v"</code> is seen on the command line, <tt class="module">optparse</tt> does the
214equivalent of:
215<div class="verbatim"><pre>
216options.verbosity = 0
217options.verbosity += 1
218</pre></div>
219
220<P>
221Every subsequent occurrence of <code>"-v"</code> results in
222<div class="verbatim"><pre>
223options.verbosity += 1
224</pre></div>
225
226<P>
227</LI>
228<LI>
229<code>callback</code> [required: <code>callback</code>;
230relevant: <tt class="member">type</tt>, <code>nargs</code>, <code>callback_args</code>, <code>callback_kwargs</code>]
231
232<P>
233Call the function specified by <code>callback</code>, which is called as
234<div class="verbatim"><pre>
235func(option, opt_str, value, parser, *args, **kwargs)
236</pre></div>
237
238<P>
239See section&nbsp;<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>
247Prints a complete help message for all the options in the
248current option parser. The help message is constructed from
249the <code>usage</code> string passed to OptionParser's constructor and
250the <tt class="member">help</tt> string passed to every option.
251
252<P>
253If no <tt class="member">help</tt> string is supplied for an option, it will still be
254listed in the help message. To omit an option entirely, use
255the 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
259you do not normally need to create one.
260
261<P>
262Example:
263<div class="verbatim"><pre>
264from optparse import OptionParser, SUPPRESS_HELP
265
266parser = OptionParser()
267parser.add_option("-h", "--help", action="help"),
268parser.add_option("-v", action="store_true", dest="verbose",
269 help="Be moderately verbose")
270parser.add_option("--file", dest="filename",
271 help="Input file to read data from"),
272parser.add_option("--secret", help=SUPPRESS_HELP)
273</pre></div>
274
275<P>
276If <tt class="module">optparse</tt> sees either <code>"-h"</code> or <code>"-help"</code> on the command line, it
277will 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>
280usage: foo.py [options]
281
282options:
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>
289After printing the help message, <tt class="module">optparse</tt> terminates your process
290with <code>sys.exit(0)</code>.
291
292<P>
293</LI>
294<LI>
295<code>version</code>
296
297<P>
298Prints the version number supplied to the OptionParser to stdout and
299exits. The version number is actually formatted and printed by the
300<code>print_version()</code> method of OptionParser. Generally only relevant
301if the <code>version</code> argument is supplied to the OptionParser
302constructor. 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>
349See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
350</ADDRESS>
351</BODY>
352</HTML>