Sophie

Sophie

distrib > Mandriva > 10.1 > i586 > by-pkgid > ccf83290023404568bb21aa0163b385f > files > 854

python-docs-2.3.4-6.2.101mdk.i586.rpm

<!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.gif" />
<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-callback-error-handling.html">
<LINK rel="prev" href="optparse-defining-callback-option.html">
<LINK rel="parent" href="optparse-callback-options.html">
<LINK rel="next" href="optparse-callback-error-handling.html">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name='aesop' content='information' />
<META name="description" content="How callbacks are called">
<META name="keywords" content="lib">
<META name="resource-type" content="document">
<META name="distribution" content="global">
<title>6.20.4.2 How callbacks are called</title>
</head>
<body>
<DIV CLASS="navigation">
<div id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.20.4.1 Defining a callback" 
  href="optparse-defining-callback-option.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.20.4 Callback Options" 
  href="optparse-callback-options.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.20.4.3 Error handling" 
  href="optparse-callback-error-handling.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-defining-callback-option.html">6.20.4.1 Defining a callback</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-callback-options.html">6.20.4 Callback Options</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-callback-error-handling.html">6.20.4.3 Error handling</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->

<H3><A NAME="SECTION0082042000000000000000"><!--x--></A><A NAME="optparse-callbacks-called"><!--z--></A>
<BR>
6.20.4.2 How callbacks are called
</H3>

<P>
All callbacks are called as follows:

<P>
<div class="verbatim"><pre>
func(option, opt, value, parser, *args, **kwargs)
</pre></div>

<P>
where

<P>
<dl class="definitions">
<dt><b><a id='l2h-2028'>option</a></b></dt>
<dd>
is the <tt class="class">Option</tt> instance that's calling the callback.

<P>
<dt><b><a id='l2h-2029'>opt</a></b></dt>
<dd>
is the option string seen on the command-line that's triggering the
callback.  (If an abbreviated long option was used, <var>opt</var> will be
the full, canonical option string--for example, if the user puts
<b class="programopt">--foo</b> on the command-line as an abbreviation for
<b class="programopt">--foobar</b>, then <var>opt</var> will be
<b class="programopt">--foobar</b>.)

<P>
<dt><b><a id='l2h-2030'>value</a></b></dt>
<dd>
is the argument to this option seen on the command-line.
<tt class="module">optparse</tt> will only expect an argument if <var>type</var> is
set; the type of <var>value</var> will be the type implied by the
option's type (see&nbsp;<A href="optparse-option-types.html#optparse-option-types">6.20.3</A>, ``Option types'').  If
<var>type</var> for this option is <code>None</code> (no argument expected), then
<var>value</var> will be <code>None</code>.  If "<tt class="samp">nargs &gt; 1</tt>", <var>value</var> will
be a tuple of values of the appropriate type.

<P>
<dt><b><a id='l2h-2031'>parser</a></b></dt>
<dd>
is the <tt class="class">OptionParser</tt> instance driving the whole thing, mainly
useful because you can access some other interesting data through it,
as instance attributes:

<P>
<dl class="definitions"><dt><b><a id='l2h-2032'>parser.rargs</a></b></dt>
<dd>
the current remaining argument list, i.e. with <var>opt</var> (and
<var>value</var>, if any) removed, and only the arguments following
them still there.  Feel free to modify <tt class="member">parser.rargs</tt>,
e.g. by consuming more arguments.

<P>
<dt><b><a id='l2h-2033'>parser.largs</a></b></dt>
<dd>
the current set of leftover arguments, i.e. arguments that have been
processed but have not been consumed as options (or arguments to
options).  Feel free to modify <tt class="member">parser.largs</tt> e.g. by adding
more arguments to it.

<P>
<dt><b><a id='l2h-2034'>parser.values</a></b></dt>
<dd>
the object where option values are by default stored.  This is useful
because it lets callbacks use the same mechanism as the rest of
<tt class="module">optparse</tt> for storing option values; you don't need to mess
around with globals or closures.  You can also access the value(s) of
any options already encountered on the command-line.
</dl>

<P>
<dt><b><a id='l2h-2035'>args</a></b></dt>
<dd>
is a tuple of arbitrary positional arguments supplied via the
<var>callback</var>_args option attribute.

<P>
<dt><b><a id='l2h-2036'>kwargs</a></b></dt>
<dd>
is a dictionary of arbitrary keyword arguments supplied via
<var>callback_kwargs</var>.
</dl>

<P>
Since <var>args</var> and <var>kwargs</var> are optional (they are only passed
if you supply <var>callback_args</var> and/or <var>callback_kwargs</var> when
you define your callback option), the minimal callback function is:

<P>
<div class="verbatim"><pre>
def my_callback (option, opt, value, parser):
    pass
</pre></div>

<P>

<DIV CLASS="navigation">
<div class='online-navigation'><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="6.20.4.1 Defining a callback" 
  rel="prev" title="6.20.4.1 Defining a callback" 
  href="optparse-defining-callback-option.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.20.4 Callback Options" 
  rel="parent" title="6.20.4 Callback Options" 
  href="optparse-callback-options.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.20.4.3 Error handling" 
  rel="next" title="6.20.4.3 Error handling" 
  href="optparse-callback-error-handling.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" 
  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" 
  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-defining-callback-option.html">6.20.4.1 Defining a callback</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-callback-options.html">6.20.4 Callback Options</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-callback-error-handling.html">6.20.4.3 Error handling</A>
</div>
</div>
<hr />
<span class="release-info">Release 2.3.4, documentation updated on May 20, 2004.</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>