Optik: Quick Links | ||
---|---|---|
home SF project page bug tracker download mailing list docs (old) docs (current) |
optparse
)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 --helpand 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.
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 is easy; just use the standard incantion for installing Python modules:
python setup.py installIf 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 from
optparse
in the standard library.
If you're using Optik 1.5 (or optparse from Python 2.4), see the Optik 1.5 documentation directory.
If you're still using Optik 1.4 (optparse from Python 2.3), the Optik 1.4 directory here is much improved from the docs originally distributed with Optik (or Python).
Since Optik is now included in the standard library, you have a couple of options for distributing scripts that require Optik:
optik
, and require that users
install a particular version of Optik to use your application
SCons.Optik
in its own library)
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:
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.
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