Add doctest for example in the library reference.

This commit is contained in:
Raymond Hettinger 2003-04-29 19:58:04 +00:00
parent 70d566be10
commit 1a4a9d0b0f
1 changed files with 40 additions and 0 deletions

View File

@ -125,6 +125,46 @@ opts, args = getopt.gnu_getopt(cmdline, 'ab:', ['alpha', 'beta='])
verify(opts == [('-a', '')])
verify(args == ['arg1', '-b', '1', '--alpha', '--beta=2'])
#------------------------------------------------------------------------------
libreftest = """
Examples from the Library Reference: Doc/lib/libgetopt.tex
An example using only Unix style options:
>>> import getopt
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
>>> args
['-a', '-b', '-cfoo', '-d', 'bar', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'abc:d:')
>>> optlist
[('-a', ''), ('-b', ''), ('-c', 'foo'), ('-d', 'bar')]
>>> args
['a1', 'a2']
Using long option names is equally easy:
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
>>> args = s.split()
>>> args
['--condition=foo', '--testing', '--output-file', 'abc.def', '-x', 'a1', 'a2']
>>> optlist, args = getopt.getopt(args, 'x', [
... 'condition=', 'output-file=', 'testing'])
>>> optlist
[('--condition', 'foo'), ('--testing', ''), ('--output-file', 'abc.def'), ('-x', '')]
>>> args
['a1', 'a2']
"""
__test__ = {'libreftest' : libreftest}
import doctest, sys
doctest.testmod(sys.modules[__name__])
#------------------------------------------------------------------------------
if verbose:
print "Module getopt: tests completed successfully."