diff options
author | Raymond Hettinger <python@rcn.com> | 2003-04-29 19:58:04 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2003-04-29 19:58:04 (GMT) |
commit | 1a4a9d0b0f0bd457ae311c5e1925d6e56e4838cb (patch) | |
tree | e748f0bcdfc290e462c6fafce78e3095a0364457 /Lib/test/test_getopt.py | |
parent | 70d566be10581db010233f08798a637a8201996a (diff) | |
download | cpython-1a4a9d0b0f0bd457ae311c5e1925d6e56e4838cb.zip cpython-1a4a9d0b0f0bd457ae311c5e1925d6e56e4838cb.tar.gz cpython-1a4a9d0b0f0bd457ae311c5e1925d6e56e4838cb.tar.bz2 |
Add doctest for example in the library reference.
Diffstat (limited to 'Lib/test/test_getopt.py')
-rw-r--r-- | Lib/test/test_getopt.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/Lib/test/test_getopt.py b/Lib/test/test_getopt.py index 70ea65b..13834b8 100644 --- a/Lib/test/test_getopt.py +++ b/Lib/test/test_getopt.py @@ -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." |