<!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-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>
<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.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>
<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>
<!--End of Navigation Panel-->
<H3><A NAME=
"SECTION0082151000000000000000"></A><A NAME=
"optparse-adding-new-types"></A>
6.21.5.1 Adding new types
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>.
<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.
<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)
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.
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.
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.)
First, the necessary imports:
<div class=
"verbatim"><pre>
from optparse import Option, OptionValueError
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):
"option %s: invalid complex value: %r" % (opt, value))
Finally, the Option subclass:
<div class=
"verbatim"><pre>
TYPES = Option.TYPES + (
"complex",)
TYPE_CHECKER = copy(Option.TYPE_CHECKER)
TYPE_CHECKER[
"complex"] = check_complex
(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
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")
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)
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<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>
<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>
<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.