<!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-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>
<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.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>
<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>
<!--End of Navigation Panel-->
<H3><A NAME=
"SECTION0082139000000000000000"></A><A NAME=
"optparse-conflicts-between-options"></A>
6.21.3.9 Conflicts between options
If you're not careful, it's easy to define options with conflicting
<div class=
"verbatim"><pre>
parser.add_option(
"-n",
"--dry-run", ...)
parser.add_option(
"-n",
"--noisy", ...)
(This is particularly true if you've defined your own OptionParser
subclass with some standard options.)
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
<div class=
"verbatim"><pre>
parser = OptionParser(..., conflict_handler=handler)
<div class=
"verbatim"><pre>
parser.set_conflict_handler(handler)
The available conflict handlers are:
<DT><STRONG><code>error
</code> (default)
</STRONG></DT>
<DD>assume option conflicts are a programming error and raise
<DT><STRONG><code>resolve
</code></STRONG></DT>
<DD>resolve option conflicts intelligently (see below)
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")
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>
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")
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>
--dry-run new dry-run option
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<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>
<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>
<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.