Optparse: Detecting if option or option-arg is specified or not

David Goodger goodger at python.org
Fri Mar 12 20:52:29 CET 2004

Sam Smith wrote:
 > Some of my commandlines share the same option-arg, but the default
 > value to be associated with each of them is different.  In one of
 > the commandlines I was trying to detect if the user has specified
 > the option or not and if not assign a different value to the option.

Perhaps you should wait to specify your options (i.e., populate your
Parser object) until you know which command you're dealing with.

 > Irrespective of the usage scenario, I believe that it is important
 > to give the module user the ability to find out whether an option
 > has been specified or not by the user.

I don't.  I have yet to see a compelling example where such
introspection is really necessary.

But if you really do, then put it in post-processing.  Set the default
value to something that cannot be specified on the command line, test
for that value, and proceed accordingly.

I'm pretty sure this topic has been hashed out thoroughly on the
optik-users mailing list
(http://sourceforge.net/mailarchive/forum.php?forum_id=6064).  I
suggest some research there and if you're still convinced, post there.
I doubt if Greg Ward (the author of Optik/optparse) will see your
posts here.

-- David Goodger

More information about the Python-list mailing list