Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / optparse-store-action.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index' />
<link rel="first" href="lib.html" title='Python Library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="optparse-handling-boolean-options.html" />
<link rel="prev" href="optparse-understanding-option-actions.html" />
<link rel="parent" href="optparse-tutorial.html" />
<link rel="next" href="optparse-handling-boolean-options.html" />
<meta name='aesop' content='information' />
<title>6.21.2.2 The store action</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.21.2.1 Understanding option actions"
href="optparse-understanding-option-actions.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="6.21.2 Tutorial"
href="optparse-tutorial.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="6.21.2.3 Handling boolean (flag)"
href="optparse-handling-boolean-options.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="optparse-understanding-option-actions.html">6.21.2.1 Understanding option actions</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-tutorial.html">6.21.2 Tutorial</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-handling-boolean-options.html">6.21.2.3 Handling boolean (flag)</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H3><A NAME="SECTION0082122000000000000000"></A><A NAME="optparse-store-action"></A>
<BR>
6.21.2.2 The store action
</H3>
<P>
The most common option action is <code>store</code>, which tells <tt class="module">optparse</tt> to take
the next argument (or the remainder of the current argument), ensure
that it is of the correct type, and store it to your chosen destination.
<P>
For example:
<div class="verbatim"><pre>
parser.add_option("-f", "--file",
action="store", type="string", dest="filename")
</pre></div>
<P>
Now let's make up a fake command line and ask <tt class="module">optparse</tt> to parse it:
<div class="verbatim"><pre>
args = ["-f", "foo.txt"]
(options, args) = parser.parse_args(args)
</pre></div>
<P>
When <tt class="module">optparse</tt> sees the option string <code>"-f"</code>, it consumes the next
argument, <code>"foo.txt"</code>, and stores it in <code>options.filename</code>. So,
after this call to <tt class="method">parse_args()</tt>, <code>options.filename</code> is
<code>"foo.txt"</code>.
<P>
Some other option types supported by <tt class="module">optparse</tt> are <code>int</code> and <code>float</code>.
Here's an option that expects an integer argument:
<div class="verbatim"><pre>
parser.add_option("-n", type="int", dest="num")
</pre></div>
<P>
Note that this option has no long option string, which is perfectly
acceptable. Also, there's no explicit action, since the default is
<code>store</code>.
<P>
Let's parse another fake command-line. This time, we'll jam the option
argument right up against the option: since <code>"-n42"</code> (one argument) is
equivalent to <code>"-n 42"</code> (two arguments), the code
<div class="verbatim"><pre>
(options, args) = parser.parse_args(["-n42"])
print options.num
</pre></div>
<P>
will print <code>"42"</code>.
<P>
If you don't specify a type, <tt class="module">optparse</tt> assumes <code>string</code>. Combined with the
fact that the default action is <code>store</code>, that means our first example
can be a lot shorter:
<div class="verbatim"><pre>
parser.add_option("-f", "--file", dest="filename")
</pre></div>
<P>
If you don't supply a destination, <tt class="module">optparse</tt> figures out a sensible default
from the option strings: if the first long option string is
<code>"-foo-bar"</code>, then the default destination is <code>foo_bar</code>. If there
are no long option strings, <tt class="module">optparse</tt> looks at the first short option
string: the default destination for <code>"-f"</code> is <code>f</code>.
<P>
<tt class="module">optparse</tt> also includes built-in <code>long</code> and <code>complex</code> types. Adding
types is covered in section&nbsp;<A HREF="#optparse-extending"><tex2html_cross_ref_visible_mark></A>, Extending <tt class="module">optparse</tt>.
<P>
<DIV CLASS="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.21.2.1 Understanding option actions"
href="optparse-understanding-option-actions.html"><img src='../icons/previous.png'
border='0' height='32' alt='Previous Page' width='32' /></A></td>
<td class='online-navigation'><a rel="parent" title="6.21.2 Tutorial"
href="optparse-tutorial.html"><img src='../icons/up.png'
border='0' height='32' alt='Up One Level' width='32' /></A></td>
<td class='online-navigation'><a rel="next" title="6.21.2.3 Handling boolean (flag)"
href="optparse-handling-boolean-options.html"><img src='../icons/next.png'
border='0' height='32' alt='Next Page' width='32' /></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
href="contents.html"><img src='../icons/contents.png'
border='0' height='32' alt='Contents' width='32' /></A></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
border='0' height='32' alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
href="genindex.html"><img src='../icons/index.png'
border='0' height='32' alt='Index' width='32' /></A></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="optparse-understanding-option-actions.html">6.21.2.1 Understanding option actions</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-tutorial.html">6.21.2 Tutorial</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-handling-boolean-options.html">6.21.2.3 Handling boolean (flag)</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>