473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help with optionparse recipe

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

http://aspn.activestate.com/ASPN/Coo.../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 so*****@there.net
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"
Jul 18 '05 #1
2 1320
Stephen Boulet <st********@spam.theboulets.net.please> wrote in message news:<J8********************@speakeasy.net>...
I'm having trouble with the "optionparse" recipe in the cookbook:

http://aspn.activestate.com/ASPN/Coo.../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 so*****@there.net
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
Jul 18 '05 #2
Michele Simionato wrote:
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"
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Mars | last post by:
I have looked long and hard at Mr. Martelli's Borg recipe: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 It is a very useful substitute for a Singleton, but I can't figure out...
10
by: Jacek Generowicz | last post by:
Where can I find concise, clear documentation describing what one has to do in order to enable Python's internal help to be able to provide descriptions of Python keywords ? I am in a situation...
4
by: Zeljko Vrba | last post by:
1. Is there any HOW-TO documentation about DB API except PEP 249? 2. How are you supposed to write DB-driver independent code when each driver has its own, possibly different from any other,...
8
by: Michele Simionato | last post by:
I was playing with string.Template in Python 2.4 and I came out with the following recipe: import sys from string import Template def merge(*dictionaries): """Merge from right (i.e. the...
6
by: Steven D'Aprano | last post by:
I've been working with the Borg design pattern from here: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66531 and I'm having problems subclassing it. I'm a newbie, so I've probably...
14
by: jojoba | last post by:
Hi, I hope this post is ok for this group. Here's my deal: I have two computers on my LAN at home. One desktop. One laptop. Both computers are wireless enabled (and wired enabled too). I...
8
by: Richard Hollenbeck | last post by:
I have a recipe database that I've been building but I haven't yet put any of the ingredients in because of a little problem of normalization. If I build a table of ingredients, all the recipes...
1
by: Thomas | last post by:
Hi list! I'm struggling with a recipe from the Python Cookbook (2nd ed.) concerning XML-RPC. It's recipe 15.2 "Serving XML-RPC Requests", and since I thought it's a popular book some other folks...
4
verbatim
by: verbatim | last post by:
i have 4 tables in a db, to allow users rate certain recipes. they might not rate all recipes at one time. they might even go back and rate a recipe more than one time. there will also be at least 1,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.