summaryrefslogtreecommitdiffstats
path: root/Doc/library/optparse.rst
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2008-12-21 00:06:59 (GMT)
committerBenjamin Peterson <benjamin@python.org>2008-12-21 00:06:59 (GMT)
commit6ebe78f5ccf7a58e64b42e74f7977c49d1c8f08b (patch)
tree61827c58a7a8aa27b508a1bedcb67ec9d7271175 /Doc/library/optparse.rst
parentf767050cdc3b8ba09e42e93defd01a1659d33ed8 (diff)
downloadcpython-6ebe78f5ccf7a58e64b42e74f7977c49d1c8f08b.zip
cpython-6ebe78f5ccf7a58e64b42e74f7977c49d1c8f08b.tar.gz
cpython-6ebe78f5ccf7a58e64b42e74f7977c49d1c8f08b.tar.bz2
Merged revisions 67654,67676-67677,67681,67692,67725,67761,67784-67785,67787-67788,67802,67848-67850,67862-67864,67880,67882 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r67654 | georg.brandl | 2008-12-07 16:42:09 -0600 (Sun, 07 Dec 2008) | 2 lines #4457: rewrite __import__() documentation. ........ r67676 | benjamin.peterson | 2008-12-08 20:03:03 -0600 (Mon, 08 Dec 2008) | 1 line specify how things are copied ........ r67677 | benjamin.peterson | 2008-12-08 20:05:11 -0600 (Mon, 08 Dec 2008) | 1 line revert unrelated change to installer script ........ r67681 | jeremy.hylton | 2008-12-09 15:03:10 -0600 (Tue, 09 Dec 2008) | 2 lines Add simple unittests for Request ........ r67692 | amaury.forgeotdarc | 2008-12-10 18:03:42 -0600 (Wed, 10 Dec 2008) | 2 lines #1030250: correctly pass the dry_run option to the mkpath() function. ........ r67725 | benjamin.peterson | 2008-12-12 22:02:20 -0600 (Fri, 12 Dec 2008) | 1 line fix incorrect example ........ r67761 | benjamin.peterson | 2008-12-14 11:26:04 -0600 (Sun, 14 Dec 2008) | 1 line fix missing bracket ........ r67784 | georg.brandl | 2008-12-15 02:33:58 -0600 (Mon, 15 Dec 2008) | 2 lines #4446: document "platforms" argument for setup(). ........ r67785 | georg.brandl | 2008-12-15 02:36:11 -0600 (Mon, 15 Dec 2008) | 2 lines #4611: fix typo. ........ r67787 | georg.brandl | 2008-12-15 02:58:59 -0600 (Mon, 15 Dec 2008) | 2 lines #4578: fix has_key() usage in compiler package. ........ r67788 | georg.brandl | 2008-12-15 03:07:39 -0600 (Mon, 15 Dec 2008) | 2 lines #4568: remove limitation in varargs callback example. ........ r67802 | amaury.forgeotdarc | 2008-12-15 16:29:14 -0600 (Mon, 15 Dec 2008) | 4 lines #3632: the "pyo" macro from gdbinit can now run when the GIL is released. Patch by haypo. ........ r67848 | benjamin.peterson | 2008-12-18 20:28:56 -0600 (Thu, 18 Dec 2008) | 1 line fix typo ........ r67849 | benjamin.peterson | 2008-12-18 20:31:35 -0600 (Thu, 18 Dec 2008) | 1 line _call_method -> _callmethod and _get_value to _getvalue ........ r67850 | raymond.hettinger | 2008-12-19 03:06:07 -0600 (Fri, 19 Dec 2008) | 9 lines Fix-up and clean-up docs for int.bit_length(). * Replace dramatic footnote with in-line comment about possible round-off errors in logarithms of large numbers. * Add comments to the pure python code equivalent. * replace floor() with int() in the mathematical equivalent so the type is correct (should be an int, not a float). * add abs() to the mathematical equivalent so that it matches the previous line that it is supposed to be equivalent to. * make one combined example with a negative input. ........ r67862 | benjamin.peterson | 2008-12-19 20:48:02 -0600 (Fri, 19 Dec 2008) | 1 line copy sentence from docstring ........ r67863 | benjamin.peterson | 2008-12-19 20:51:26 -0600 (Fri, 19 Dec 2008) | 1 line add headings ........ r67864 | benjamin.peterson | 2008-12-19 20:57:19 -0600 (Fri, 19 Dec 2008) | 1 line beef up docstring ........ r67880 | benjamin.peterson | 2008-12-20 16:49:24 -0600 (Sat, 20 Dec 2008) | 1 line remove redundant sentence ........ r67882 | benjamin.peterson | 2008-12-20 16:59:49 -0600 (Sat, 20 Dec 2008) | 1 line add some recent releases to the list ........
Diffstat (limited to 'Doc/library/optparse.rst')
-rw-r--r--Doc/library/optparse.rst47
1 files changed, 22 insertions, 25 deletions
diff --git a/Doc/library/optparse.rst b/Doc/library/optparse.rst
index 3d8b43c..7989d33 100644
--- a/Doc/library/optparse.rst
+++ b/Doc/library/optparse.rst
@@ -1625,36 +1625,33 @@ directly).
Nevertheless, here's a stab at a callback for an option with variable
arguments::
- def vararg_callback(option, opt_str, value, parser):
- assert value is None
- done = 0
- value = []
- rargs = parser.rargs
- while rargs:
- arg = rargs[0]
-
- # Stop if we hit an arg like "--foo", "-a", "-fx", "--file=f",
- # etc. Note that this also stops on "-3" or "-3.0", so if
- # your option takes numeric values, you will need to handle
- # this.
- if ((arg[:2] == "--" and len(arg) > 2) or
- (arg[:1] == "-" and len(arg) > 1 and arg[1] != "-")):
- break
- else:
- value.append(arg)
- del rargs[0]
-
- setattr(parser.values, option.dest, value)
+ def vararg_callback(option, opt_str, value, parser):
+ assert value is None
+ value = []
+
+ def floatable(str):
+ try:
+ float(str)
+ return True
+ except ValueError:
+ return False
+
+ 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",
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: