Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / optparse-adding-new-types.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-adding-new-actions.html" />
<link rel="prev" href="optparse-extending-optparse.html" />
<link rel="parent" href="optparse-extending-optparse.html" />
<link rel="next" href="optparse-adding-new-actions.html" />
<meta name='aesop' content='information' />
<title>6.21.5.1 Adding new types</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.5 Extending optparse"
href="optparse-extending-optparse.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.5 Extending optparse"
href="optparse-extending-optparse.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.5.2 Adding new actions"
href="optparse-adding-new-actions.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-extending-optparse.html">6.21.5 Extending optparse</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-extending-optparse.html">6.21.5 Extending optparse</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-adding-new-actions.html">6.21.5.2 Adding new actions</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H3><A NAME="SECTION0082151000000000000000"></A><A NAME="optparse-adding-new-types"></A>
<BR>
6.21.5.1 Adding new types
</H3>
<P>
To add new types, you need to define your own subclass of <tt class="module">optparse</tt>'s Option
class. This class has a couple of attributes that define <tt class="module">optparse</tt>'s types:
<tt class="member">TYPES</tt> and <tt class="member">TYPE_CHECKER</tt>.
<P>
<tt class="member">TYPES</tt> is a tuple of type names; in your subclass, simply define a new
tuple <tt class="member">TYPES</tt> that builds on the standard one.
<P>
<tt class="member">TYPE_CHECKER</tt> is a dictionary mapping type names to type-checking
functions. A type-checking function has the following signature:
<div class="verbatim"><pre>
def check_mytype(option, opt, value)
</pre></div>
<P>
where <code>option</code> is an <tt class="class">Option</tt> instance, <code>opt</code> is an option string
(e.g., <code>"-f"</code>), and <code>value</code> is the string from the command line that
must be checked and converted to your desired type. <code>check_mytype()</code>
should return an object of the hypothetical type <code>mytype</code>. The value
returned by a type-checking function will wind up in the OptionValues
instance returned by <tt class="method">OptionParser.parse_args()</tt>, or be passed to a
callback as the <code>value</code> parameter.
<P>
Your type-checking function should raise OptionValueError if it
encounters any problems. OptionValueError takes a single string
argument, which is passed as-is to OptionParser's <tt class="method">error()</tt> method,
which in turn prepends the program name and the string <code>"error:"</code> and
prints everything to stderr before terminating the process.
<P>
Here's a silly example that demonstrates adding a <code>complex</code> option
type to parse Python-style complex numbers on the command line. (This
is even sillier than it used to be, because <tt class="module">optparse</tt> 1.3 added built-in
support for complex numbers, but never mind.)
<P>
First, the necessary imports:
<div class="verbatim"><pre>
from copy import copy
from optparse import Option, OptionValueError
</pre></div>
<P>
You need to define your type-checker first, since it's referred to later
(in the <tt class="member">TYPE_CHECKER</tt> class attribute of your Option subclass):
<div class="verbatim"><pre>
def check_complex(option, opt, value):
try:
return complex(value)
except ValueError:
raise OptionValueError(
"option %s: invalid complex value: %r" % (opt, value))
</pre></div>
<P>
Finally, the Option subclass:
<div class="verbatim"><pre>
class MyOption (Option):
TYPES = Option.TYPES + ("complex",)
TYPE_CHECKER = copy(Option.TYPE_CHECKER)
TYPE_CHECKER["complex"] = check_complex
</pre></div>
<P>
(If we didn't make a <tt class="function">copy()</tt> of <tt class="member">Option.TYPE_CHECKER</tt>, we would end
up modifying the <tt class="member">TYPE_CHECKER</tt> attribute of <tt class="module">optparse</tt>'s Option class.
This being Python, nothing stops you from doing that except good manners
and common sense.)
<P>
That's it! Now you can write a script that uses the new option type
just like any other <tt class="module">optparse</tt>-based script, except you have to instruct your
OptionParser to use MyOption instead of Option:
<div class="verbatim"><pre>
parser = OptionParser(option_class=MyOption)
parser.add_option("-c", type="complex")
</pre></div>
<P>
Alternately, you can build your own option list and pass it to
OptionParser; if you don't use <tt class="method">add_option()</tt> in the above way, you
don't need to tell OptionParser which option class to use:
<div class="verbatim"><pre>
option_list = [MyOption("-c", action="store", type="complex", dest="c")]
parser = OptionParser(option_list=option_list)
</pre></div>
<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.5 Extending optparse"
href="optparse-extending-optparse.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.5 Extending optparse"
href="optparse-extending-optparse.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.5.2 Adding new actions"
href="optparse-adding-new-actions.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-extending-optparse.html">6.21.5 Extending optparse</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-extending-optparse.html">6.21.5 Extending optparse</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-adding-new-actions.html">6.21.5.2 Adding new actions</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>