Skip to main content
Join the official 2020 Python Developers SurveyStart the survey!

A command line interface that recognizes conflicting options given as arguments.

Project description

ConflictsOptionParser provides a command line interface, based off the Python Standard Library’s own optparse.OptionParser. (Users of this library should have familiarity with the optparse library.) conflictsparse.ConflictsOptionParser acts as a drop-in replacement for optparse.OptionParser that has a few additional methods for specifying options which conflict with each other, and has additional behavior for automatically raising an error when detecting conflicting options parsed from the command line arguments.

These two additional methods in conflictsparse.ConflictsOptionParser are

register_conflict(options, message=None)
where options is an iterable of options, either optparse.Option instances, or their corresponding option strings; marks any combination of these options as conflicting, and will cause the parser to report the conflicting options through an error, using either the specified message or constructing one as needed, and to exit immediately.
like register_conflict but removes the combination of options from registered conflicts.

Example Usage

#/usr/bin/env python
# File:
import conflictsparse
parser = conflictsparse.ConflictsOptionParser("python %prog [OPTIONS] ARG")
# You can retain the Option instances for flexibility, in case you change
# option strings later
verbose_opt = parser.add_option('-v', '--verbose', action='store_true')
quiet_opt = parser.add_option('-q', '--quiet', action='store_true')
# Alternatively, you don't need to keep references to the instances;
# we can re-use the option strings later
parser.add_option('--no-output', action='store_true')
# Register the conflict. Specifying an error message is optional; the
# generic one that is generated will usually do.
parser.register_conflict((verbose_opt, quiet_opt, '--no-output'))
# Now we parse the arguments as we would with
# optparse.OptionParser.parse_args()
opts, args = parser.parse_args()
print "Opts are", opts
print "Args are", args

Then if we ran

python -v 42

we would get

Opts are {'verbose': True, 'no_output': None, 'quiet': None}
Args are ['42']

But suppose we give conflicting options on the command line.

python -v --no-output 42

Then the parser would automatically raise an error and exit.

Usage: python [OPTIONS] ARG error: --verbose, --quiet, --no-output are incompatible options.


ConflictsOptionParser is available from the Python Package Index at

You can install using pip.

pip install ConflictsOptionParser


Source code is hosted on BitBucket at

Bug Reports

Please report any bugs on the BitBucket Issue Tracker at


This work was originally inspired by a solution on Stack Overflow by Támas.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ConflictsOptionParser, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size ConflictsOptionParser-0.1.1.tar.gz (5.8 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page