Skip to main content

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.

unregister_conflict(options)

like register_conflict but removes the combination of options from registered conflicts.

Example Usage

#/usr/bin/env python
# File: tryconflictsparse.py
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 tryconflictsparse.py -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 tryconflictsparse.py -v --no-output 42

Then the parser would automatically raise an error and exit.

Usage: python tryconflictsparse.py [OPTIONS] ARG

tryconflictsparse.py: error: --verbose, --quiet, --no-output are incompatible options.

Installation

ConflictsOptionParser is available from the Python Package Index at http://pypi.python.org/pypi/ConflictsOptionParser

You can install using pip.

pip install ConflictsOptionParser

Development

Source code is hosted on BitBucket at https://bitbucket.org/gotgenes/conflictsoptionparser/

Bug Reports

Please report any bugs on the BitBucket Issue Tracker at https://bitbucket.org/gotgenes/conflictsoptionparser/issues

Acknowledgements

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.

Source Distribution

ConflictsOptionParser-0.1.1.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file ConflictsOptionParser-0.1.1.tar.gz.

File metadata

File hashes

Hashes for ConflictsOptionParser-0.1.1.tar.gz
Algorithm Hash digest
SHA256 05c72d18a89ce3dba6f8517a7ea8222436c1b4d9290a9af35fff1d96e838d187
MD5 48bb59ed0d0bdfe4c095ee64b1a0f9ba
BLAKE2b-256 1f036eb1bbdcc979e59ecfdbad80d3c9c7438daf2c9e2edb23f7b87679776e89

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page