467,920 Members | 1,257 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,920 developers. It's quick & easy.

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

I am using optparse for the commandline parsing for my programs. I
was wondering if it is possible to detect if an option or option-arg
has been specified on the commandline by the user or not. Please do
not suggest default value solutions.

Thanks.
Jul 18 '05 #1
  • viewed: 3661
Share:
4 Replies
Sam Smith wrote:
I am using optparse for the commandline parsing for my programs. I
was wondering if it is possible to detect if an option or option-arg
has been specified on the commandline by the user or not. Please do
not suggest default value solutions.


What's wrong with default values?
Anyway, here's the hasattr() approach:
p = optparse.OptionParser()
p.add_option("-x", action="store_true") <optparse.Option instance at 0x403a1fcc> class NS: pass .... values = NS()
p.parse_args([], values) (<__main__.NS instance at 0x402c3f6c>, []) hasattr(values, "x") False p.parse_args(["-x"], values) (<__main__.NS instance at 0x402c3f6c>, []) hasattr(values, "x") True


Peter
Jul 18 '05 #2
>> 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
Jul 18 '05 #3
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/f...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
Jul 18 '05 #4
Sam Smith wrote:
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.
I normally don't advocate code duplication, but in this case I'd either
specialcase

if specialcommandline:
parser.add_option(special option)
else:
parser.add_option(normal option)

or completely separate the parsers for the two differing scenarios.
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.
Irrespective of the usage scenario - you can't be serious here.
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


My doubts not withstanding, here's how to achieve the desired (I think)
behaviour in a few lines - not tested beyond what you see as I will hardly
ever use it myself:

import sets
import optparse as op

class Values(op.Values):
def __init__(self, defaults):
op.Values.__init__(self, defaults)
self.specified = sets.Set([])

def __setattr__(self, name, value):
self.__dict__[name] = value
if hasattr(self, "specified"):
self.specified.add(name)
def test(args):
p = op.OptionParser()
p.add_option("-f", dest="filename", default="tmp.txt")
v = Values(p.defaults)
p.parse_args(args, v)
if "filename" in v.specified:
info = "specified:"
else:
info = "using default:"
print info, v.filename

test(["-f", "other.txt"])
test(["-f", "tmp.txt"])
test([])

Peter

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Hans-Joachim Widmaier | last post: by
5 posts views Thread by GMTaglia | last post: by
7 posts views Thread by Henry Ludemann | last post: by
1 post views Thread by sector119 | last post: by
7 posts views Thread by R. Bernstein | last post: by
3 posts views Thread by Bob | last post: by
4 posts views Thread by Shatadal | last post: by
3 posts views Thread by Dan | last post: by
reply views Thread by Robert Kern | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.