Problem with optparser or In

F

Florian Lindner

Hi,
I'm not sure where the problems lies...
I'm using optparse and I've a option defined:

parser.add_option("-t", "--type", action="callback",
callback=option_callback, type="string")

def option_callback(option, opt, value, parser, *args, **kwargs):
if (str(option) == "-t/--type") and (opt in ("mailbox", "ftp")):
print "Error!"
else:
parser.values.type = opt

This should print error if the commandline is "--type=ftp" or
"--type=mailbox" (same for the short args).
But it never gets till Error... Although opt contains "mailbox" or "ftp".
What's wrong there?


Thanks,
Florian
 
P

Peter Otten

Florian said:
Hi,
I'm not sure where the problems lies...
I'm using optparse and I've a option defined:

parser.add_option("-t", "--type", action="callback",
callback=option_callback, type="string")

def option_callback(option, opt, value, parser, *args, **kwargs):

Change to
if (str(option) == "-t/--type") and (value in ("mailbox", "ftp")):
if (str(option) == "-t/--type") and (opt in ("mailbox", "ftp")):
print "Error!"
else:
parser.values.type = opt

This should print error if the commandline is "--type=ftp" or
"--type=mailbox" (same for the short args).
But it never gets till Error... Although opt contains "mailbox" or "ftp".
What's wrong there?

In such cases the poor man's debugger aka print statement is your friend.

print opt

would reveal that opt contains the actual option and value - surprise,
surprise -the value.


Peter
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top