Help with optionparse recipe

Stephen Boulet at
Tue Jul 27 07:40:46 CEST 2004

I'm having trouble with the "optionparse" recipe in the cookbook:

This does not give me the results I expected (namely the arguments on the
command line for each flag):

#!/usr/bin/env python
"""An example script invoking optionparse, my wrapper around optparse.

  usage: %prog [options] args
  -f, --file: file to attach
  -s, --subject: email subject
  -a, --address: email address

import optionparse
opt, args = optionparse.parse(__doc__)
if not opt and not args:
if opt.file:
    print opt.file
if opt.subject:
    print opt.subject
if opt.address:
    print opt.address


$ python -f "myfile.txt" -s "Here's the file" \
 -a someone at

What am I doing wrong here?



If your desktop gets out of control easily,
you probably have too much stuff on it that
doesn't need to be there.
         Donna Smallin, "Unclutter Your Home"

More information about the Python-list mailing list