<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<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>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<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>
<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>
<!--End of Navigation Panel-->
<H3><A NAME=
"SECTION0082122000000000000000"></A><A NAME=
"optparse-store-action"></A>
6.21.2.2 The store action
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.
<div class=
"verbatim"><pre>
parser.add_option(
"-f",
"--file",
action=
"store",
type=
"string",
dest=
"filename")
Now let's make up a fake command line and ask
<tt class=
"module">optparse
</tt> to parse it:
<div class=
"verbatim"><pre>
(options, args) = parser.parse_args(args)
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
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")
Note that this option has no long option string, which is perfectly
acceptable. Also, there's no explicit action, since the default is
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"])
will print
<code>"42"</code>.
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
<div class=
"verbatim"><pre>
parser.add_option(
"-f",
"--file",
dest=
"filename")
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>.
<tt class=
"module">optparse
</tt> also includes built-in
<code>long
</code> and
<code>complex
</code> types. Adding
types is covered in section
<A HREF=
"#optparse-extending"><tex2html_cross_ref_visible_mark></A>, Extending
<tt class=
"module">optparse
</tt>.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<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>
<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>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.