summaryrefslogtreecommitdiffstats
path: root/Doc/tutorial/stdlib.rst
diff options
context:
space:
mode:
authormental <m3nta1@yahoo.com>2019-08-01 14:17:30 (GMT)
committerGuido van Rossum <gvanrossum@gmail.com>2019-08-01 14:17:30 (GMT)
commit2491134029b195d3159a489e1803ee22a7839b41 (patch)
tree732bea59c1473a1a918899be2b25de1e8dfea17a /Doc/tutorial/stdlib.rst
parent0d30ae1a03102de07758650af9243fd31211325a (diff)
downloadcpython-2491134029b195d3159a489e1803ee22a7839b41.zip
cpython-2491134029b195d3159a489e1803ee22a7839b41.tar.gz
cpython-2491134029b195d3159a489e1803ee22a7839b41.tar.bz2
bpo-37726: Prefer argparse over getopt in stdlib tutorial (#15052)
Diffstat (limited to 'Doc/tutorial/stdlib.rst')
-rw-r--r--Doc/tutorial/stdlib.rst19
1 files changed, 15 insertions, 4 deletions
diff --git a/Doc/tutorial/stdlib.rst b/Doc/tutorial/stdlib.rst
index e030f8f..f32063e 100644
--- a/Doc/tutorial/stdlib.rst
+++ b/Doc/tutorial/stdlib.rst
@@ -72,10 +72,21 @@ three`` at the command line::
>>> print(sys.argv)
['demo.py', 'one', 'two', 'three']
-The :mod:`getopt` module processes *sys.argv* using the conventions of the Unix
-:func:`getopt` function. More powerful and flexible command line processing is
-provided by the :mod:`argparse` module.
-
+The :mod:`argparse` module provides a mechanism to process command line arguments.
+It should always be preferred over directly processing ``sys.argv`` manually.
+
+Take, for example, the below snippet of code::
+
+ >>> import argparse
+ >>> from getpass import getuser
+ >>> parser = argparse.ArgumentParser(description='An argparse example.')
+ >>> parser.add_argument('name', nargs='?', default=getuser(), help='The name of someone to greet.')
+ >>> parser.add_argument('--verbose', '-v', action='count')
+ >>> args = parser.parse_args()
+ >>> greeting = ["Hi", "Hello", "Greetings! its very nice to meet you"][args.verbose % 3]
+ >>> print(f'{greeting}, {args.name}')
+ >>> if not args.verbose:
+ >>> print('Try running this again with multiple "-v" flags!')
.. _tut-stderr: