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

Sam Smith nikita_raja at yahoo.com
Fri Mar 12 13:47:27 EST 2004


>> What is wrong with default values?

Per-se there is nothing wrong with default values.  But I think that
there should be some way for detecting if the user has specified a
specific option or option-arg on the commandline or not.  Your
solution works in the sense that we are able to detect whether an
option or option-arg is specified or not, but now default value
assignment will not work.

What I really want is to specify an option having a default value
which could be specified by the user too.  The user may decide to
specify this option on the commandline, and if not the option should
take the default value.  But I should be able to query to find if this
setting of the value has been done due to optparse filling in default
value or it has been done by the user.

Usage Scenario:

I am implementing a multi-level commandline program.  On windows
platform similar to "net /?" command which in turn has "net use /?",
"net computer /?" etc commandlines.

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.

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.

Some suggestions:

* Maybe a method can be added to the option class, isSpecified() 
  which will return True if the option is found on commandline 
  and optparse has read it or False if the option is not found 
  on the commandline.

* Add ability to query the Values instance which is returned by
  parse_args

Thanks



More information about the Python-list mailing list