Help with optionparse recipe

S

Stephen Boulet

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

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278844

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:
optionparse.exit()
if opt.file:
print opt.file
if opt.subject:
print opt.subject
if opt.address:
print opt.address

Result:

$ python opttest.py -f "myfile.txt" -s "Here's the file" \
-a (e-mail address removed)
True
True
True

What am I doing wrong here?

--

Stephen

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"
 
M

Michele Simionato

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

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278844

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:
optionparse.exit()
if opt.file:
print opt.file
if opt.subject:
print opt.subject
if opt.address:
print opt.address

Result:

$ python opttest.py -f "myfile.txt" -s "Here's the file" \
-a (e-mail address removed)
True
True
True

What am I doing wrong here?

It is the docstring. Try with this:

"""An example script invoking optionparse, my wrapper around optparse.

usage: %prog [options] args
-f, --file=FILE: file to attach
-s, --subject=SUBJECT: email subject
-a, --address=ADDRESS: email address
"""

Technically "optionparse" works by parsing the docstring and it
distinguishes option arguments from flags by looking at the "=" sign.

Michele Simionato
 
S

Stephen Boulet

Michele said:
It is the docstring. Try with this:

"""An example script invoking optionparse, my wrapper around optparse.

usage: %prog [options] args
-f, --file=FILE: file to attach
-s, --subject=SUBJECT: email subject
-a, --address=ADDRESS: email address
"""

Thanks!

--

Stephen

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"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top