Re: Difficulties in using optparse for the very first time.
David Goodger <[email protected]> Sun, 04 Jul 2004 18:07:28 -0400
| Newsgroups | gmane.comp.python.optik.user |
|---|---|
| Message-ID | <[email protected]> |
Hi Laura,
> What am I doing wrong? There has _got_ to be an easier way to get
> what I want than this.
I think the root of the problem may be this:
> "usage: %prog [-s [filename ...] | [-i|-c filename ...]]
and:
> Skipping the philosophy section,
The command-line usage you desire is not supported by optparse.
Specifically, optparse doesn't support "zero or more option arguments"
as implied by the usage string.
Some options never take an argument. Some options always take an
argument. Lots of people want an ``optional option arguments''
feature, meaning that some options will take an argument if they
see it, and won't if they don't. This is somewhat controversial,
because it makes parsing ambiguous: if -a and -b are both options,
and -a takes an optional argument, how do we interpret -ab?
optparse does not support optional option arguments.
Another "feature" not supported is a "required option", which is an
oxymoron. It can be done by client code, but optparse doesn't try to
make it easy. And optparse has no concept of mutually exclusive
options (the example is of inverse switches). Mutual exclusivity
logic has to be implement by client code.
I recommend reading the philosophy section. It's not very long, and
it helps to establish a common vocabulary and clear up some common
misconceptions. At the least, after finishing it you may have been
able to decide if optparse is or is not the right tool for you.
The usage line doesn't make clear what "filename" is. Is it an input
filename, or output filename? I suspect it's the input filename, and
that the program will support multiple input files (zero implying
stdin for input). I suspect that you really want this usage:
usage: %prog [-s | -i | -c] [input_filename ...]
You should also pass a description to OptionParser (although I don't
see that parameter in the docs).
I would re-implement it like this:
from optparse import OptionParser
usage = "usage: %prog [-s | -i | -c] [input_filename ...]"
description = """Reads in one or more files (default: stdin), \
parses & processes them ..., and writes them out. The output may \
be sent to stdout (the default, set explicitly with -s); the files \
may be rewritten in-place (with -i); or new files may be written \
(-c, an extension ".new" is added).""" # or whatever
optparser = OptionParser(usage, description=description)
optparser.add_option("-s", "--stdout",
help="send your output to stdout")
optparser.add_option("-i", "--InPlace",
help="overwrite files in place")
optparser.add_option("-c", "--MakeCopy",
help="copy files ... fn.py --> fn.new.py")
options, args = optparser.parse_args()
output_opt_total = (hasattr(options, 'stdout')
+ hasattr(options, 'InPlace')
+ hasattr(options, 'MakeCopy'))
if output_opt_total > 1:
optparser.error(
'Cannot combine -s -i and -c options. Use one only.')
elif output_opt_total == 0:
options.stdout = 1
if not args and (hasattr(options, 'InPlace')
or hasattr(options, 'MakeCopy')):
optparser.error(
'-i and -c options require at least one filename')
print 'options are: ', options
print 'args are: ', args
I'd suggest lower-case long option names instead of CamelCase, like
"--in-place" & "--make-copy".
I agree that the docs need some work. The docs are a tutorial,
requiring too much drilling-down to get at the API details. There
should be a real reference section for quick & complete access to the
API details.
--
David Goodger <http://python.net/~goodger>
-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 -
digital self defense, top technical experts, no vendor pitches,
unmatched networking opportunities. Visit www.blackhat.com