| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
| 2 | <html> |
| 3 | <head> |
| 4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> |
| 5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> |
| 6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> |
| 7 | <link rel="first" href="lib.html" title='Python Library Reference' /> |
| 8 | <link rel='contents' href='contents.html' title="Contents" /> |
| 9 | <link rel='index' href='genindex.html' title='Index' /> |
| 10 | <link rel='last' href='about.html' title='About this document...' /> |
| 11 | <link rel='help' href='about.html' title='About this document...' /> |
| 12 | <link rel="next" href="optparse-adding-new-actions.html" /> |
| 13 | <link rel="prev" href="optparse-extending-optparse.html" /> |
| 14 | <link rel="parent" href="optparse-extending-optparse.html" /> |
| 15 | <link rel="next" href="optparse-adding-new-actions.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>6.21.5.1 Adding new types</title> |
| 18 | </head> |
| 19 | <body> |
| 20 | <DIV CLASS="navigation"> |
| 21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 23 | <tr> |
| 24 | <td class='online-navigation'><a rel="prev" title="6.21.5 Extending optparse" |
| 25 | href="optparse-extending-optparse.html"><img src='../icons/previous.png' |
| 26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 27 | <td class='online-navigation'><a rel="parent" title="6.21.5 Extending optparse" |
| 28 | href="optparse-extending-optparse.html"><img src='../icons/up.png' |
| 29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 30 | <td class='online-navigation'><a rel="next" title="6.21.5.2 Adding new actions" |
| 31 | href="optparse-adding-new-actions.html"><img src='../icons/next.png' |
| 32 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 33 | <td align="center" width="100%">Python Library Reference</td> |
| 34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 35 | href="contents.html"><img src='../icons/contents.png' |
| 36 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 38 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 39 | <td class='online-navigation'><a rel="index" title="Index" |
| 40 | href="genindex.html"><img src='../icons/index.png' |
| 41 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 42 | </tr></table> |
| 43 | <div class='online-navigation'> |
| 44 | <b class="navlabel">Previous:</b> |
| 45 | <a class="sectref" rel="prev" href="optparse-extending-optparse.html">6.21.5 Extending optparse</A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="optparse-extending-optparse.html">6.21.5 Extending optparse</A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="optparse-adding-new-actions.html">6.21.5.2 Adding new actions</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H3><A NAME="SECTION0082151000000000000000"></A><A NAME="optparse-adding-new-types"></A> |
| 56 | <BR> |
| 57 | 6.21.5.1 Adding new types |
| 58 | </H3> |
| 59 | |
| 60 | <P> |
| 61 | To add new types, you need to define your own subclass of <tt class="module">optparse</tt>'s Option |
| 62 | class. This class has a couple of attributes that define <tt class="module">optparse</tt>'s types: |
| 63 | <tt class="member">TYPES</tt> and <tt class="member">TYPE_CHECKER</tt>. |
| 64 | |
| 65 | <P> |
| 66 | <tt class="member">TYPES</tt> is a tuple of type names; in your subclass, simply define a new |
| 67 | tuple <tt class="member">TYPES</tt> that builds on the standard one. |
| 68 | |
| 69 | <P> |
| 70 | <tt class="member">TYPE_CHECKER</tt> is a dictionary mapping type names to type-checking |
| 71 | functions. A type-checking function has the following signature: |
| 72 | <div class="verbatim"><pre> |
| 73 | def check_mytype(option, opt, value) |
| 74 | </pre></div> |
| 75 | |
| 76 | <P> |
| 77 | where <code>option</code> is an <tt class="class">Option</tt> instance, <code>opt</code> is an option string |
| 78 | (e.g., <code>"-f"</code>), and <code>value</code> is the string from the command line that |
| 79 | must be checked and converted to your desired type. <code>check_mytype()</code> |
| 80 | should return an object of the hypothetical type <code>mytype</code>. The value |
| 81 | returned by a type-checking function will wind up in the OptionValues |
| 82 | instance returned by <tt class="method">OptionParser.parse_args()</tt>, or be passed to a |
| 83 | callback as the <code>value</code> parameter. |
| 84 | |
| 85 | <P> |
| 86 | Your type-checking function should raise OptionValueError if it |
| 87 | encounters any problems. OptionValueError takes a single string |
| 88 | argument, which is passed as-is to OptionParser's <tt class="method">error()</tt> method, |
| 89 | which in turn prepends the program name and the string <code>"error:"</code> and |
| 90 | prints everything to stderr before terminating the process. |
| 91 | |
| 92 | <P> |
| 93 | Here's a silly example that demonstrates adding a <code>complex</code> option |
| 94 | type to parse Python-style complex numbers on the command line. (This |
| 95 | is even sillier than it used to be, because <tt class="module">optparse</tt> 1.3 added built-in |
| 96 | support for complex numbers, but never mind.) |
| 97 | |
| 98 | <P> |
| 99 | First, the necessary imports: |
| 100 | <div class="verbatim"><pre> |
| 101 | from copy import copy |
| 102 | from optparse import Option, OptionValueError |
| 103 | </pre></div> |
| 104 | |
| 105 | <P> |
| 106 | You need to define your type-checker first, since it's referred to later |
| 107 | (in the <tt class="member">TYPE_CHECKER</tt> class attribute of your Option subclass): |
| 108 | <div class="verbatim"><pre> |
| 109 | def check_complex(option, opt, value): |
| 110 | try: |
| 111 | return complex(value) |
| 112 | except ValueError: |
| 113 | raise OptionValueError( |
| 114 | "option %s: invalid complex value: %r" % (opt, value)) |
| 115 | </pre></div> |
| 116 | |
| 117 | <P> |
| 118 | Finally, the Option subclass: |
| 119 | <div class="verbatim"><pre> |
| 120 | class MyOption (Option): |
| 121 | TYPES = Option.TYPES + ("complex",) |
| 122 | TYPE_CHECKER = copy(Option.TYPE_CHECKER) |
| 123 | TYPE_CHECKER["complex"] = check_complex |
| 124 | </pre></div> |
| 125 | |
| 126 | <P> |
| 127 | (If we didn't make a <tt class="function">copy()</tt> of <tt class="member">Option.TYPE_CHECKER</tt>, we would end |
| 128 | up modifying the <tt class="member">TYPE_CHECKER</tt> attribute of <tt class="module">optparse</tt>'s Option class. |
| 129 | This being Python, nothing stops you from doing that except good manners |
| 130 | and common sense.) |
| 131 | |
| 132 | <P> |
| 133 | That's it! Now you can write a script that uses the new option type |
| 134 | just like any other <tt class="module">optparse</tt>-based script, except you have to instruct your |
| 135 | OptionParser to use MyOption instead of Option: |
| 136 | <div class="verbatim"><pre> |
| 137 | parser = OptionParser(option_class=MyOption) |
| 138 | parser.add_option("-c", type="complex") |
| 139 | </pre></div> |
| 140 | |
| 141 | <P> |
| 142 | Alternately, you can build your own option list and pass it to |
| 143 | OptionParser; if you don't use <tt class="method">add_option()</tt> in the above way, you |
| 144 | don't need to tell OptionParser which option class to use: |
| 145 | <div class="verbatim"><pre> |
| 146 | option_list = [MyOption("-c", action="store", type="complex", dest="c")] |
| 147 | parser = OptionParser(option_list=option_list) |
| 148 | </pre></div> |
| 149 | |
| 150 | <P> |
| 151 | |
| 152 | <DIV CLASS="navigation"> |
| 153 | <div class='online-navigation'> |
| 154 | <p></p><hr /> |
| 155 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 156 | <tr> |
| 157 | <td class='online-navigation'><a rel="prev" title="6.21.5 Extending optparse" |
| 158 | href="optparse-extending-optparse.html"><img src='../icons/previous.png' |
| 159 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 160 | <td class='online-navigation'><a rel="parent" title="6.21.5 Extending optparse" |
| 161 | href="optparse-extending-optparse.html"><img src='../icons/up.png' |
| 162 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 163 | <td class='online-navigation'><a rel="next" title="6.21.5.2 Adding new actions" |
| 164 | href="optparse-adding-new-actions.html"><img src='../icons/next.png' |
| 165 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 166 | <td align="center" width="100%">Python Library Reference</td> |
| 167 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 168 | href="contents.html"><img src='../icons/contents.png' |
| 169 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 170 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 171 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 172 | <td class='online-navigation'><a rel="index" title="Index" |
| 173 | href="genindex.html"><img src='../icons/index.png' |
| 174 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 175 | </tr></table> |
| 176 | <div class='online-navigation'> |
| 177 | <b class="navlabel">Previous:</b> |
| 178 | <a class="sectref" rel="prev" href="optparse-extending-optparse.html">6.21.5 Extending optparse</A> |
| 179 | <b class="navlabel">Up:</b> |
| 180 | <a class="sectref" rel="parent" href="optparse-extending-optparse.html">6.21.5 Extending optparse</A> |
| 181 | <b class="navlabel">Next:</b> |
| 182 | <a class="sectref" rel="next" href="optparse-adding-new-actions.html">6.21.5.2 Adding new actions</A> |
| 183 | </div> |
| 184 | </div> |
| 185 | <hr /> |
| 186 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 187 | </DIV> |
| 188 | <!--End of Navigation Panel--> |
| 189 | <ADDRESS> |
| 190 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 191 | </ADDRESS> |
| 192 | </BODY> |
| 193 | </HTML> |