Skip to content

Commit

Permalink
[3.12] Docs: turn getopt examples into doctests (GH-126377) (#126386)
Browse files Browse the repository at this point in the history
(cherry picked from commit 0d80777)

Co-authored-by: Erlend E. Aasland <[email protected]>
  • Loading branch information
miss-islington and erlend-aasland authored Nov 4, 2024
1 parent d1b8718 commit 70f777d
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions Doc/library/getopt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ exception:

An example using only Unix style options:

.. doctest::

>>> import getopt
>>> args = '-a -b -cfoo -d bar a1 a2'.split()
>>> args
Expand All @@ -104,6 +106,8 @@ An example using only Unix style options:

Using long option names is equally easy:

.. doctest::

>>> s = '--condition=foo --testing --output-file abc.def -x a1 a2'
>>> args = s.split()
>>> args
Expand All @@ -115,7 +119,9 @@ Using long option names is equally easy:
>>> args
['a1', 'a2']

In a script, typical usage is something like this::
In a script, typical usage is something like this:

.. testcode::

import getopt, sys

Expand Down Expand Up @@ -145,7 +151,9 @@ In a script, typical usage is something like this::
main()

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

Expand Down

0 comments on commit 70f777d

Please sign in to comment.