Optik: Python command-line parsing library (original) (raw)
| | Optik: Quick Links | | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | | | home SF project page bug tracker download mailing list docs (old) docs (current) | |
Optik (aka optparse
)
Introduction
Optik is a powerful, flexible, extensible, easy-to-use command-line parsing library for Python. Using Optik, you can add intelligent, sophisticated handling of command-line options to your scripts with very little overhead. (And, since Python 2.3, Optik is now part of the Python standard library, under the name optparse
.)
Here's an example of using Optik to add some command-line options to a simple script:
from optik import OptionParser [...] parser = OptionParser() parser.add_option("-f", "--file", action="store", type="string", dest="filename", help="write report to FILE", metavar="FILE") parser.add_option("-q", "--quiet", action="store_false", dest="verbose", default=1, help="don't print status messages to stdout")
(options, args) = parser.parse_args()
With these few lines of code, users of your script can now do the "usual thing" on the command-line:
yourscript -f outfile --quiet yourscript -qfoutfile yourscript --file=outfile -q yourscript --quiet --file outfile
(All of these result in
options.filename == "outfile" options.verbose == 0
...just as you might expect.)
Even niftier, users can run one of
yourscript -h yourscript --help
and Optik will print out a brief summary of your script's optons:
usage: yourscript [options]
options: -h, --help show this help message and exit -fFILE, --file=FILE write report to FILE -q, --quiet don't print status messages to stdout
That's just a taste of the flexibility Optik gives you in parsing your command-line. See the documentation included in the package for details.
Requirements
Optik 1.5.1 requires Python 2.0 or greater (although I only tested it with 2.1 through 2.5a1).
As of Python 2.3, Optik is part of the standard library -- although it's now called optparse
. For forwards compatibility with Python 2.3, the standalone Optik distribution also installs a module called optparse
(Optik 1.4.1 or later).
Installation
Installation is easy; just use the standard incantion for installing Python modules:
python setup.py install
If you're using Python 2.3 or later and don't need the new features in Optik 1.5, you don't need to install anything: just import fromoptparse
in the standard library.
Documentation
If you're using Optik 1.5 (or optparse from Python 2.4), see theOptik 1.5 documentation directory.
If you're still using Optik 1.4 (optparse from Python 2.3), theOptik 1.4 directory here is much improved from the docs originally distributed with Optik (or Python).
Distributing Optik-Based Applications
Since Optik is now included in the standard library, you have a couple of options for distributing scripts that require Optik:
- continue to import from
optik
, and require that users install a particular version of Optik to use your application - distribute your own version Optik as a sub-package of your library (e.g. SCons includes a package
SCons.Optik
in its own library) - change your code to import from
optparse
, and simply require either Python >= 2.3 or Optik >= 1.4.1
If you choose the latter option, be careful about precisely which Optik features you use; I recommend one of the following approaches:
- code to Optik 1.4.1 and document that your application requires_either_ Python >= 2.3 or Optik >= 1.4.1
- code to Optik 1.5 and document that your application requires_either_ Python >= 2.4 or Optik >= 1.5
- code to Optik 1.5.1 and document that your application requires_either_ Python >= 2.5 or Optik >= 1.5.1
(subject to change pending final release of Python 2.5)
Author, copyright, & license
Optik was written by Greg Ward (gward at python dot net).
Copyright (c) 2001-2006 Gregory P. Ward. All rights reserved.
Optik is licensed under the BSD license; see the README.txt in the distribution for exact terms.
Download
Please see the Optik download page.
Mailing list
The optik-users@lists.sourceforge.net list is for general discussion of Optik:
- join the list
- visit the list archive
- post to the list (subscribers only!)
Related work
- cfgparse, a config file parsing module that is modeled after and cooperates with Optik (Dan Gass ).
- optcomplete, a shell completion self-generator for Optik-based programs (Martin Blais ) (I should have a list of competing option parsing libraries here... please email me if you want to add one!)