| 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="prev" href="optparse-adding-new-actions.html" /> |
| 13 | <link rel="parent" href="optparse-extending-optparse.html" /> |
| 14 | <link rel="next" href="module-tempfile.html" /> |
| 15 | <meta name='aesop' content='information' /> |
| 16 | <title>6.21.5.3 Other reasons to extend optparse</title> |
| 17 | </head> |
| 18 | <body> |
| 19 | <DIV CLASS="navigation"> |
| 20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> |
| 21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 22 | <tr> |
| 23 | <td class='online-navigation'><a rel="prev" title="6.21.5.2 Adding new actions" |
| 24 | href="optparse-adding-new-actions.html"><img src='../icons/previous.png' |
| 25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 26 | <td class='online-navigation'><a rel="parent" title="6.21.5 Extending optparse" |
| 27 | href="optparse-extending-optparse.html"><img src='../icons/up.png' |
| 28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 29 | <td class='online-navigation'><a rel="next" title="6.22 tempfile " |
| 30 | href="module-tempfile.html"><img src='../icons/next.png' |
| 31 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 32 | <td align="center" width="100%">Python Library Reference</td> |
| 33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 34 | href="contents.html"><img src='../icons/contents.png' |
| 35 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 37 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 38 | <td class='online-navigation'><a rel="index" title="Index" |
| 39 | href="genindex.html"><img src='../icons/index.png' |
| 40 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 41 | </tr></table> |
| 42 | <div class='online-navigation'> |
| 43 | <b class="navlabel">Previous:</b> |
| 44 | <a class="sectref" rel="prev" href="optparse-adding-new-actions.html">6.21.5.2 Adding new actions</A> |
| 45 | <b class="navlabel">Up:</b> |
| 46 | <a class="sectref" rel="parent" href="optparse-extending-optparse.html">6.21.5 Extending optparse</A> |
| 47 | <b class="navlabel">Next:</b> |
| 48 | <a class="sectref" rel="next" href="module-tempfile.html">6.22 tempfile </A> |
| 49 | </div> |
| 50 | <hr /></div> |
| 51 | </DIV> |
| 52 | <!--End of Navigation Panel--> |
| 53 | |
| 54 | <H3><A NAME="SECTION0082153000000000000000"></A><A NAME="optparse-other-reasons-to-extend-optparse"></A> |
| 55 | <BR> |
| 56 | 6.21.5.3 Other reasons to extend <tt class="module">optparse</tt> |
| 57 | </H3> |
| 58 | |
| 59 | <P> |
| 60 | Adding new types and new actions are the big, obvious reasons why you |
| 61 | might want to extend <tt class="module">optparse</tt>. I can think of at least two other areas to |
| 62 | play with. |
| 63 | |
| 64 | <P> |
| 65 | First, the simple one: OptionParser tries to be helpful by calling |
| 66 | <tt class="function">sys.exit()</tt> when appropriate, i.e. when there's an error on the |
| 67 | command line or when the user requests help. In the former case, the |
| 68 | traditional course of letting the script crash with a traceback is |
| 69 | unacceptable; it will make users think there's a bug in your script when |
| 70 | they make a command-line error. In the latter case, there's generally |
| 71 | not much point in carrying on after printing a help message. |
| 72 | |
| 73 | <P> |
| 74 | If this behaviour bothers you, it shouldn't be too hard to ``fix'' it. |
| 75 | You'll have to |
| 76 | |
| 77 | <DL COMPACT> |
| 78 | <DT> |
| 79 | |
| 80 | 1) |
| 81 | <DD> |
| 82 | subclass OptionParser and override <tt class="method">error()</tt> |
| 83 | |
| 84 | <P> |
| 85 | <DT> |
| 86 | |
| 87 | 2) |
| 88 | <DD> |
| 89 | subclass Option and override <tt class="method">take_action()</tt>--you'll |
| 90 | need to provide your own handling of the <tt class="member">help</tt> action that |
| 91 | doesn't call <tt class="function">sys.exit()</tt> |
| 92 | |
| 93 | <P> |
| 94 | </DD> |
| 95 | </DL> |
| 96 | |
| 97 | <P> |
| 98 | The second, much more complex, possibility is to override the |
| 99 | command-line syntax implemented by <tt class="module">optparse</tt>. In this case, you'd leave the |
| 100 | whole machinery of option actions and types alone, but rewrite the code |
| 101 | that processes <var>sys.argv</var>. You'll need to subclass OptionParser in any |
| 102 | case; depending on how radical a rewrite you want, you'll probably need |
| 103 | to override one or all of <tt class="method">parse_args()</tt>, <tt class="method">_process_long_opt()</tt>, and |
| 104 | <tt class="method">_process_short_opts()</tt>. |
| 105 | |
| 106 | <P> |
| 107 | Both of these are left as an exercise for the reader. I have not tried |
| 108 | to implement either myself, since I'm quite happy with <tt class="module">optparse</tt>'s default |
| 109 | behaviour (naturally). |
| 110 | |
| 111 | <P> |
| 112 | Happy hacking, and don't forget: Use the Source, Luke. |
| 113 | |
| 114 | <DIV CLASS="navigation"> |
| 115 | <div class='online-navigation'> |
| 116 | <p></p><hr /> |
| 117 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 118 | <tr> |
| 119 | <td class='online-navigation'><a rel="prev" title="6.21.5.2 Adding new actions" |
| 120 | href="optparse-adding-new-actions.html"><img src='../icons/previous.png' |
| 121 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 122 | <td class='online-navigation'><a rel="parent" title="6.21.5 Extending optparse" |
| 123 | href="optparse-extending-optparse.html"><img src='../icons/up.png' |
| 124 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 125 | <td class='online-navigation'><a rel="next" title="6.22 tempfile " |
| 126 | href="module-tempfile.html"><img src='../icons/next.png' |
| 127 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 128 | <td align="center" width="100%">Python Library Reference</td> |
| 129 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 130 | href="contents.html"><img src='../icons/contents.png' |
| 131 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 132 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 133 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 134 | <td class='online-navigation'><a rel="index" title="Index" |
| 135 | href="genindex.html"><img src='../icons/index.png' |
| 136 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 137 | </tr></table> |
| 138 | <div class='online-navigation'> |
| 139 | <b class="navlabel">Previous:</b> |
| 140 | <a class="sectref" rel="prev" href="optparse-adding-new-actions.html">6.21.5.2 Adding new actions</A> |
| 141 | <b class="navlabel">Up:</b> |
| 142 | <a class="sectref" rel="parent" href="optparse-extending-optparse.html">6.21.5 Extending optparse</A> |
| 143 | <b class="navlabel">Next:</b> |
| 144 | <a class="sectref" rel="next" href="module-tempfile.html">6.22 tempfile </A> |
| 145 | </div> |
| 146 | </div> |
| 147 | <hr /> |
| 148 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 149 | </DIV> |
| 150 | <!--End of Navigation Panel--> |
| 151 | <ADDRESS> |
| 152 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 153 | </ADDRESS> |
| 154 | </BODY> |
| 155 | </HTML> |