Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / optparse-conflicts-between-options.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-other-methods.html" />
<link rel="prev" href="optparse-querying-manipulating-option-parser.html" />
<link rel="parent" href="optparse-reference-guide.html" />
<link rel="next" href="optparse-other-methods.html" />
<meta name='aesop' content='information' />
<title>6.21.3.9 Conflicts between options</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.3.8 Querying and manipulating"
href="optparse-querying-manipulating-option-parser.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.3 Reference Guide"
href="optparse-reference-guide.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.3.10 Other methods"
href="optparse-other-methods.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-querying-manipulating-option-parser.html">6.21.3.8 Querying and manipulating</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-reference-guide.html">6.21.3 Reference Guide</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-other-methods.html">6.21.3.10 Other methods</A>
</div>
<hr /></div>
</DIV>
<!--End of Navigation Panel-->
<H3><A NAME="SECTION0082139000000000000000"></A><A NAME="optparse-conflicts-between-options"></A>
<BR>
6.21.3.9 Conflicts between options
</H3>
<P>
If you're not careful, it's easy to define options with conflicting
option strings:
<div class="verbatim"><pre>
parser.add_option("-n", "--dry-run", ...)
[...]
parser.add_option("-n", "--noisy", ...)
</pre></div>
<P>
(This is particularly true if you've defined your own OptionParser
subclass with some standard options.)
<P>
Every time you add an option, <tt class="module">optparse</tt> checks for conflicts with existing
options. If it finds any, it invokes the current conflict-handling
mechanism. You can set the conflict-handling mechanism either in the
constructor:
<div class="verbatim"><pre>
parser = OptionParser(..., conflict_handler=handler)
</pre></div>
<P>
or with a separate call:
<div class="verbatim"><pre>
parser.set_conflict_handler(handler)
</pre></div>
<P>
The available conflict handlers are:
<BLOCKQUOTE>
</BLOCKQUOTE><DL>
<DT><STRONG><code>error</code> (default)</STRONG></DT>
<DD>assume option conflicts are a programming error and raise
OptionConflictError
</DD>
<DT><STRONG><code>resolve</code></STRONG></DT>
<DD>resolve option conflicts intelligently (see below)
</DD>
</DL><BLOCKQUOTE>
</BLOCKQUOTE>
<P>
As an example, let's define an OptionParser that resolves conflicts
intelligently and add conflicting options to it:
<div class="verbatim"><pre>
parser = OptionParser(conflict_handler="resolve")
parser.add_option("-n", "--dry-run", ..., help="do no harm")
parser.add_option("-n", "--noisy", ..., help="be noisy")
</pre></div>
<P>
At this point, <tt class="module">optparse</tt> detects that a previously-added option is already
using the <code>"-n"</code> option string. Since <code>conflict_handler</code> is
<code>"resolve"</code>, it resolves the situation by removing <code>"-n"</code> from the
earlier option's list of option strings. Now <code>"-dry-run"</code> is the
only way for the user to activate that option. If the user asks for
help, the help message will reflect that:
<div class="verbatim"><pre>
options:
--dry-run do no harm
[...]
-n, --noisy be noisy
</pre></div>
<P>
It's possible to whittle away the option strings for a previously-added
option until there are none left, and the user has no way of invoking
that option from the command-line. In that case, <tt class="module">optparse</tt> removes that
option completely, so it doesn't show up in help text or anywhere else.
Carrying on with our existing OptionParser:
<div class="verbatim"><pre>
parser.add_option("--dry-run", ..., help="new dry-run option")
</pre></div>
<P>
At this point, the original <b class="programopt">-n/-dry-run</b> option is no longer
accessible, so <tt class="module">optparse</tt> removes it, leaving this help text:
<div class="verbatim"><pre>
options:
[...]
-n, --noisy be noisy
--dry-run new dry-run option
</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.3.8 Querying and manipulating"
href="optparse-querying-manipulating-option-parser.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.3 Reference Guide"
href="optparse-reference-guide.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.3.10 Other methods"
href="optparse-other-methods.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-querying-manipulating-option-parser.html">6.21.3.8 Querying and manipulating</A>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="optparse-reference-guide.html">6.21.3 Reference Guide</A>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="optparse-other-methods.html">6.21.3.10 Other methods</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>