mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Docs: turn getopt examples into doctests (#126377)
This commit is contained in:
parent
081706f873
commit
0d80777981
1 changed files with 10 additions and 2 deletions
|
@ -97,6 +97,8 @@ exception:
|
||||||
|
|
||||||
An example using only Unix style options:
|
An example using only Unix style options:
|
||||||
|
|
||||||
|
.. doctest::
|
||||||
|
|
||||||
>>> import getopt
|
>>> import getopt
|
||||||
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
|
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
|
||||||
>>> args
|
>>> args
|
||||||
|
@ -109,6 +111,8 @@ An example using only Unix style options:
|
||||||
|
|
||||||
Using long option names is equally easy:
|
Using long option names is equally easy:
|
||||||
|
|
||||||
|
.. doctest::
|
||||||
|
|
||||||
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
|
>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
|
||||||
>>> args = s.split()
|
>>> args = s.split()
|
||||||
>>> args
|
>>> args
|
||||||
|
@ -120,7 +124,9 @@ Using long option names is equally easy:
|
||||||
>>> args
|
>>> args
|
||||||
['a1', 'a2']
|
['a1', 'a2']
|
||||||
|
|
||||||
In a script, typical usage is something like this::
|
In a script, typical usage is something like this:
|
||||||
|
|
||||||
|
.. testcode::
|
||||||
|
|
||||||
import getopt, sys
|
import getopt, sys
|
||||||
|
|
||||||
|
@ -150,7 +156,9 @@ In a script, typical usage is something like this::
|
||||||
main()
|
main()
|
||||||
|
|
||||||
Note that an equivalent command line interface could be produced with less code
|
Note that an equivalent command line interface could be produced with less code
|
||||||
and more informative help and error messages by using the :mod:`argparse` module::
|
and more informative help and error messages by using the :mod:`argparse` module:
|
||||||
|
|
||||||
|
.. testcode::
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue