<!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-option-attributes.html" />
<link rel=
"prev" href=
"optparse-defining-options.html" />
<link rel=
"parent" href=
"optparse-reference-guide.html" />
<link rel=
"next" href=
"optparse-option-attributes.html" />
<meta name='aesop' content='information'
/>
<title>6.21.3.4 Standard option actions
</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.3.3 Defining options"
href=
"optparse-defining-options.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.3 Reference Guide"
href=
"optparse-reference-guide.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.3.5 Option attributes"
href=
"optparse-option-attributes.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-defining-options.html">6.21.3.3 Defining options
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"optparse-reference-guide.html">6.21.3 Reference Guide
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"optparse-option-attributes.html">6.21.3.5 Option attributes
</A>
<!--End of Navigation Panel-->
<H3><A NAME=
"SECTION0082134000000000000000"></A><A NAME=
"optparse-standard-option-actions"></A>
6.21.3.4 Standard option actions
The various option actions all have slightly different requirements and
effects. Most actions have several relevant option attributes which you
may specify to guide
<tt class=
"module">optparse
</tt>'s behaviour; a few have required attributes,
which you must specify for any option using that action.
<code>store
</code> [relevant:
<tt class=
"member">type
</tt>,
<tt class=
"member">dest
</tt>,
<code>nargs
</code>,
<code>choices
</code>]
The option must be followed by an argument, which is
converted to a value according to
<tt class=
"member">type
</tt> and stored in
<tt class=
"member">dest
</tt>. If
<code>nargs
</code> > 1, multiple arguments will be consumed
from the command line; all will be converted according to
<tt class=
"member">type
</tt> and stored to
<tt class=
"member">dest
</tt> as a tuple. See the ``Option
If
<code>choices
</code> is supplied (a list or tuple of strings), the type
defaults to
<code>choice
</code>.
If
<tt class=
"member">type
</tt> is not supplied, it defaults to
<code>string
</code>.
If
<tt class=
"member">dest
</tt> is not supplied,
<tt class=
"module">optparse
</tt> derives a destination from the
first long option string (e.g.,
<code>"-foo-bar"</code> implies
<code>foo_bar
</code>).
If there are no long option strings,
<tt class=
"module">optparse
</tt> derives a destination from
the first short option string (e.g.,
<code>"-f"</code> implies
<code>f
</code>).
<div class=
"verbatim"><pre>
parser.add_option(
"-p",
type=
"float", nargs=
3,
dest=
"point")
As it parses the command line
<div class=
"verbatim"><pre>
-f foo.txt -p
1 -
3.5 4 -fbar.txt
<tt class=
"module">optparse
</tt> will set
<div class=
"verbatim"><pre>
options.point = (
1.0, -
3.5,
4.0)
<code>store_const
</code> [required:
<code>const
</code>; relevant:
<tt class=
"member">dest
</tt>]
The value
<code>const
</code> is stored in
<tt class=
"member">dest
</tt>.
<div class=
"verbatim"><pre>
parser.add_option(
"-q",
"--quiet",
action=
"store_const", const=
0,
dest=
"verbose")
parser.add_option(
"-v",
"--verbose",
action=
"store_const", const=
1,
dest=
"verbose")
parser.add_option(
"--noisy",
action=
"store_const", const=
2,
dest=
"verbose")
If
<code>"-noisy"</code> is seen,
<tt class=
"module">optparse
</tt> will set
<div class=
"verbatim"><pre>
<code>store_true
</code> [relevant:
<tt class=
"member">dest
</tt>]
A special case of
<code>store_const
</code> that stores a true value
to
<tt class=
"member">dest
</tt>.
<code>store_false
</code> [relevant:
<tt class=
"member">dest
</tt>]
Like
<code>store_true
</code>, but stores a false value.
<div class=
"verbatim"><pre>
parser.add_option(
"--clobber",
action=
"store_true",
dest=
"clobber")
parser.add_option(
"--no-clobber",
action=
"store_false",
dest=
"clobber")
<code>append
</code> [relevant:
<tt class=
"member">type
</tt>,
<tt class=
"member">dest
</tt>,
<code>nargs
</code>,
<code>choices
</code>]
The option must be followed by an argument, which is appended to the
list in
<tt class=
"member">dest
</tt>. If no default value for
<tt class=
"member">dest
</tt> is supplied, an
empty list is automatically created when
<tt class=
"module">optparse
</tt> first encounters this
option on the command-line. If
<code>nargs
</code> > 1, multiple arguments are
consumed, and a tuple of length
<code>nargs
</code> is appended to
<tt class=
"member">dest
</tt>.
The defaults for
<tt class=
"member">type
</tt> and
<tt class=
"member">dest
</tt> are the same as for the
<code>store
</code> action.
<div class=
"verbatim"><pre>
parser.add_option(
"-t",
"--tracks",
action=
"append",
type=
"int")
If
<code>"-t3"</code> is seen on the command-line,
<tt class=
"module">optparse
</tt> does the equivalent of:
<div class=
"verbatim"><pre>
options.tracks.append(int(
"3"))
If, a little later on,
<code>"-tracks=4"</code> is seen, it does:
<div class=
"verbatim"><pre>
options.tracks.append(int(
"4"))
<code>count
</code> [relevant:
<tt class=
"member">dest
</tt>]
Increment the integer stored at
<tt class=
"member">dest
</tt>. If no default value is
supplied,
<tt class=
"member">dest
</tt> is set to zero before being incremented the first
<div class=
"verbatim"><pre>
parser.add_option(
"-v",
action=
"count",
dest=
"verbosity")
The first time
<code>"-v"</code> is seen on the command line,
<tt class=
"module">optparse
</tt> does the
<div class=
"verbatim"><pre>
Every subsequent occurrence of
<code>"-v"</code> results in
<div class=
"verbatim"><pre>
<code>callback
</code> [required:
<code>callback
</code>;
relevant:
<tt class=
"member">type
</tt>,
<code>nargs
</code>,
<code>callback_args
</code>,
<code>callback_kwargs
</code>]
Call the function specified by
<code>callback
</code>, which is called as
<div class=
"verbatim"><pre>
func(option, opt_str, value, parser, *args, **kwargs)
See section
<A href=
"optparse-option-callbacks.html#optparse-option-callbacks">6.21.4</A>, Option Callbacks for more detail.
<tt class=
"member">help
</tt>
Prints a complete help message for all the options in the
current option parser. The help message is constructed from
the
<code>usage
</code> string passed to OptionParser's constructor and
the
<tt class=
"member">help
</tt> string passed to every option.
If no
<tt class=
"member">help
</tt> string is supplied for an option, it will still be
listed in the help message. To omit an option entirely, use
the special value
<code>optparse.SUPPRESS_HELP
</code>.
<tt class=
"module">optparse
</tt> automatically adds a
<tt class=
"member">help
</tt> option to all OptionParsers, so
you do not normally need to create one.
<div class=
"verbatim"><pre>
from optparse import OptionParser, SUPPRESS_HELP
parser.add_option(
"-h",
"--help",
action=
"help"),
parser.add_option(
"-v",
action=
"store_true",
dest=
"verbose",
help=
"Be moderately verbose")
parser.add_option(
"--file",
dest=
"filename",
help=
"Input file to read data from"),
parser.add_option(
"--secret", help=SUPPRESS_HELP)
If
<tt class=
"module">optparse
</tt> sees either
<code>"-h"</code> or
<code>"-help"</code> on the command line, it
will print something like the following help message to stdout
(assuming
<code>sys.argv[
0]
</code> is
<code>"foo.py"</code>):
<div class=
"verbatim"><pre>
-h, --help Show this help message and exit
--file=FILENAME Input file to read data from
After printing the help message,
<tt class=
"module">optparse
</tt> terminates your process
with
<code>sys.exit(
0)
</code>.
Prints the version number supplied to the OptionParser to stdout and
exits. The version number is actually formatted and printed by the
<code>print_version()
</code> method of OptionParser. Generally only relevant
if the
<code>version
</code> argument is supplied to the OptionParser
constructor. As with
<tt class=
"member">help
</tt> options, you will rarely create
<code>version
</code> options, since
<tt class=
"module">optparse
</tt> automatically adds them when needed.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"6.21.3.3 Defining options"
href=
"optparse-defining-options.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.3 Reference Guide"
href=
"optparse-reference-guide.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.3.5 Option attributes"
href=
"optparse-option-attributes.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-defining-options.html">6.21.3.3 Defining options
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"optparse-reference-guide.html">6.21.3 Reference Guide
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"optparse-option-attributes.html">6.21.3.5 Option attributes
</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.