optparser question

M

Michele Petrazzo

I'm trying optparse and I see a strange (for me) behavior:

def store_value(option, opt_str, value, parser):
setattr(parser.values, option.dest, value)

parser = optparse.OptionParser()
parser.add_option("-f", "--foo",
action="callback", callback=store_value,
type="int", dest="foo")

args = ["-f", "1"]
(options, args) = parser.parse_args(args)
print options, args

{'foo': 1} [] # with the type
{'foo': None} ['1'] #without it

If I not specify the type in add_options, the value aren't passed to the
store_value (into value variable), but it's understood as args!
If I specify it, it

Is this normal?

Thanks,
Michele
 
S

Steven Bethard

Michele said:
I'm trying optparse and I see a strange (for me) behavior:

def store_value(option, opt_str, value, parser):
setattr(parser.values, option.dest, value)

parser = optparse.OptionParser()
parser.add_option("-f", "--foo",
action="callback", callback=store_value,
type="int", dest="foo")

args = ["-f", "1"]
(options, args) = parser.parse_args(args)
print options, args

{'foo': 1} [] # with the type
{'foo': None} ['1'] #without it

If I not specify the type in add_options, the value aren't passed to the
store_value (into value variable), but it's understood as args!
If I specify it, it

Is this normal?

I believe so. The optparse module lists 'callback' as one of the
TYPED_ACTIONS but not one of the ALWAYS_TYPED_ACTIONS, so it should only
set nargs=1 if a type= argument was provided. That means that callbacks
will be assumed to take zero arguments until you pass an nargs= or a
type= argument.

You can try using argparse_, which doesn't make these weird inferences,
and generally assumes that your action will take a single argument
unless you specify otherwise::
... def __call__(self, parser, namespace, value, opt_str=None):
... setattr(namespace, self.dest, value)
... Namespace(foo='1')
Namespace(foo=1)

Not sure exactly what your callback was trying to do though -- it seems
like you're just duplicating the 'store' action (which is the default
anyway).

FWIW, if you want to write a custom action in argparse, you don't have
to worry about the weird TYPED_ACTIONS kinds of things in optparse.
Just specify in the action's constructor whatever defaults you need, e.g.::
... def __init__(self, nargs=2, type=int, **kwargs):
... superinit = super(MyStoreValue, self).__init__
... superinit(nargs=nargs, type=type, **kwargs)
... def __call__(self, parser, namespace, value, opt_str=None):
... setattr(namespace, self.dest, value)
... Namespace(foo=[1, 2])

Of course, you can always specify nargs= and type= in the
``add_argument()`` call too.

... _argparse: http://argparse.python-hosting.com/

STeVe
 
M

Michele Petrazzo

Steven said:
You can try using argparse_, which doesn't make these weird
inferences, and generally assumes that your action will take a single
argument unless you specify otherwise::

Not sure exactly what your callback was trying to do though -- it
seems like you're just duplicating the 'store' action (which is the
default anyway).

I made only a copy from the python docs for show the "problem".

Ok, I have not understand the "trickle" for transform the
action="callback" and provide a callback to a new action.

I believe, however, that the doc has to be more explicit about this
strange behavior, because a not so expert dev (like me :) ), can don't
understand at the first time, it.

Thanks,
Michele
 
S

Steven Bethard

Michele said:
Ok, I have not understand the "trickle" for transform the
action="callback" and provide a callback to a new action.

Yeah, you're not the only one. ;-)
I believe, however, that the doc has to be more explicit about this
strange behavior, because a not so expert dev (like me :) ), can don't
understand at the first time, it.

Yeah, it's pretty complicated. I didn't really understand it until I
wrote argparse_ to replace the optparse module. If you have a good idea
for how to improve the documentation, I'm sure the maintainers would be
grateful. You can post your suggestion here:

http://sourceforge.net/tracker/?group_id=5470&atid=105470

You don't need to know LaTeX or anything -- just a simple snippet of
text and where in the docs you think it should go is all they need.


... _argparse: http://argparse.python-hosting.com/

STeVe
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top