Initial commit of OpenSPARC T2 design and verification files.
[OpenSPARC-T2-DV] / tools / src / nas,5.n2.os.2 / lib / python / html / python / lib / optparse-how-optparse-handles-errors.html
CommitLineData
86530b38
AT
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-putting-it-all-together.html" />
13<link rel="prev" href="optparse-printing-version-string.html" />
14<link rel="parent" href="optparse-tutorial.html" />
15<link rel="next" href="optparse-putting-it-all-together.html" />
16<meta name='aesop' content='information' />
17<title>6.21.2.8 How optparse handles errors</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.2.7 Printing a version"
25 href="optparse-printing-version-string.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.2 Tutorial"
28 href="optparse-tutorial.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.2.9 Putting it all"
31 href="optparse-putting-it-all-together.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-printing-version-string.html">6.21.2.7 Printing a version</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="optparse-tutorial.html">6.21.2 Tutorial</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="optparse-putting-it-all-together.html">6.21.2.9 Putting it all</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H3><A NAME="SECTION0082128000000000000000"></A><A NAME="optparse-how-optparse-handles-errors"></A>
56<BR>
576.21.2.8 How <tt class="module">optparse</tt> handles errors
58</H3>
59
60<P>
61There are two broad classes of errors that <tt class="module">optparse</tt> has to worry about:
62programmer errors and user errors. Programmer errors are usually
63erroneous calls to <code>parser.add_option()</code>, e.g. invalid option strings,
64unknown option attributes, missing option attributes, etc. These are
65dealt with in the usual way: raise an exception (either
66<code>optparse.OptionError</code> or <code>TypeError</code>) and let the program crash.
67
68<P>
69Handling user errors is much more important, since they are guaranteed
70to happen no matter how stable your code is. <tt class="module">optparse</tt> can automatically
71detect some user errors, such as bad option arguments (passing <code>"-n
724x"</code> where <b class="programopt">-n</b> takes an integer argument), missing arguments
73(<code>"-n"</code> at the end of the command line, where <b class="programopt">-n</b> takes an argument
74of any type). Also, you can call <code>parser.error()</code> to signal an
75application-defined error condition:
76<div class="verbatim"><pre>
77(options, args) = parser.parse_args()
78[...]
79if options.a and options.b:
80 parser.error("options -a and -b are mutually exclusive")
81</pre></div>
82
83<P>
84In either case, <tt class="module">optparse</tt> handles the error the same way: it prints the
85program's usage message and an error message to standard error and
86exits with error status 2.
87
88<P>
89Consider the first example above, where the user passes <code>"4x"</code> to an
90option that takes an integer:
91<div class="verbatim"><pre>
92$ /usr/bin/foo -n 4x
93usage: foo [options]
94
95foo: error: option -n: invalid integer value: '4x'
96</pre></div>
97
98<P>
99Or, where the user fails to pass a value at all:
100<div class="verbatim"><pre>
101$ /usr/bin/foo -n
102usage: foo [options]
103
104foo: error: -n option requires an argument
105</pre></div>
106
107<P>
108<tt class="module">optparse</tt>-generated error messages take care always to mention the option
109involved in the error; be sure to do the same when calling
110<code>parser.error()</code> from your application code.
111
112<P>
113If <tt class="module">optparse</tt>'s default error-handling behaviour does not suite your needs,
114you'll need to subclass OptionParser and override <code>exit()</code> and/or
115<tt class="method">error()</tt>.
116
117<P>
118
119<DIV CLASS="navigation">
120<div class='online-navigation'>
121<p></p><hr />
122<table align="center" width="100%" cellpadding="0" cellspacing="2">
123<tr>
124<td class='online-navigation'><a rel="prev" title="6.21.2.7 Printing a version"
125 href="optparse-printing-version-string.html"><img src='../icons/previous.png'
126 border='0' height='32' alt='Previous Page' width='32' /></A></td>
127<td class='online-navigation'><a rel="parent" title="6.21.2 Tutorial"
128 href="optparse-tutorial.html"><img src='../icons/up.png'
129 border='0' height='32' alt='Up One Level' width='32' /></A></td>
130<td class='online-navigation'><a rel="next" title="6.21.2.9 Putting it all"
131 href="optparse-putting-it-all-together.html"><img src='../icons/next.png'
132 border='0' height='32' alt='Next Page' width='32' /></A></td>
133<td align="center" width="100%">Python Library Reference</td>
134<td class='online-navigation'><a rel="contents" title="Table of Contents"
135 href="contents.html"><img src='../icons/contents.png'
136 border='0' height='32' alt='Contents' width='32' /></A></td>
137<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
138 border='0' height='32' alt='Module Index' width='32' /></a></td>
139<td class='online-navigation'><a rel="index" title="Index"
140 href="genindex.html"><img src='../icons/index.png'
141 border='0' height='32' alt='Index' width='32' /></A></td>
142</tr></table>
143<div class='online-navigation'>
144<b class="navlabel">Previous:</b>
145<a class="sectref" rel="prev" href="optparse-printing-version-string.html">6.21.2.7 Printing a version</A>
146<b class="navlabel">Up:</b>
147<a class="sectref" rel="parent" href="optparse-tutorial.html">6.21.2 Tutorial</A>
148<b class="navlabel">Next:</b>
149<a class="sectref" rel="next" href="optparse-putting-it-all-together.html">6.21.2.9 Putting it all</A>
150</div>
151</div>
152<hr />
153<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
154</DIV>
155<!--End of Navigation Panel-->
156<ADDRESS>
157See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
158</ADDRESS>
159</BODY>
160</HTML>