#4568: remove limitation in varargs callback example.
This commit is contained in:
parent
2d2fe572a4
commit
60b2e38b68
|
@ -1630,36 +1630,33 @@ directly).
|
||||||
Nevertheless, here's a stab at a callback for an option with variable
|
Nevertheless, here's a stab at a callback for an option with variable
|
||||||
arguments::
|
arguments::
|
||||||
|
|
||||||
def vararg_callback(option, opt_str, value, parser):
|
def vararg_callback(option, opt_str, value, parser):
|
||||||
assert value is None
|
assert value is None
|
||||||
done = 0
|
value = []
|
||||||
value = []
|
|
||||||
rargs = parser.rargs
|
|
||||||
while rargs:
|
|
||||||
arg = rargs[0]
|
|
||||||
|
|
||||||
# Stop if we hit an arg like "--foo", "-a", "-fx", "--file=f",
|
def floatable(str):
|
||||||
# etc. Note that this also stops on "-3" or "-3.0", so if
|
try:
|
||||||
# your option takes numeric values, you will need to handle
|
float(str)
|
||||||
# this.
|
return True
|
||||||
if ((arg[:2] == "--" and len(arg) > 2) or
|
except ValueError:
|
||||||
(arg[:1] == "-" and len(arg) > 1 and arg[1] != "-")):
|
return False
|
||||||
break
|
|
||||||
else:
|
|
||||||
value.append(arg)
|
|
||||||
del rargs[0]
|
|
||||||
|
|
||||||
setattr(parser.values, option.dest, value)
|
for arg in parser.rargs:
|
||||||
|
# stop on --foo like options
|
||||||
|
if arg[:2] == "--" and len(arg) > 2:
|
||||||
|
break
|
||||||
|
# stop on -a, but not on -3 or -3.0
|
||||||
|
if arg[:1] == "-" and len(arg) > 1 and not floatable(arg):
|
||||||
|
break
|
||||||
|
value.append(arg)
|
||||||
|
|
||||||
|
del parser.rargs[:len(value)]
|
||||||
|
setattr(parser.values, option.dest, value))
|
||||||
|
|
||||||
[...]
|
[...]
|
||||||
parser.add_option("-c", "--callback", dest="vararg_attr",
|
parser.add_option("-c", "--callback", dest="vararg_attr",
|
||||||
action="callback", callback=vararg_callback)
|
action="callback", callback=vararg_callback)
|
||||||
|
|
||||||
The main weakness with this particular implementation is that negative numbers
|
|
||||||
in the arguments following ``"-c"`` will be interpreted as further options
|
|
||||||
(probably causing an error), rather than as arguments to ``"-c"``. Fixing this
|
|
||||||
is left as an exercise for the reader.
|
|
||||||
|
|
||||||
|
|
||||||
.. _optparse-extending-optparse:
|
.. _optparse-extending-optparse:
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue