summaryrefslogtreecommitdiffstats
path: root/Doc/tutorial/stdlib.rst
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2019-11-18 06:06:19 (GMT)
committerGitHub <noreply@github.com>2019-11-18 06:06:19 (GMT)
commit04c79d6088a22d467f04dbe438050c26de22fa85 (patch)
tree4066a5d93a0569adb371e229724f59be78470ffe /Doc/tutorial/stdlib.rst
parent4544e78ec4558b75bf95e5b7dfc1b5bbb07ae5f0 (diff)
downloadcpython-04c79d6088a22d467f04dbe438050c26de22fa85.zip
cpython-04c79d6088a22d467f04dbe438050c26de22fa85.tar.gz
cpython-04c79d6088a22d467f04dbe438050c26de22fa85.tar.bz2
bpo-38678: Improve argparse example in tutorial (GH-17207)
Diffstat (limited to 'Doc/tutorial/stdlib.rst')
-rw-r--r--Doc/tutorial/stdlib.rst32
1 files changed, 17 insertions, 15 deletions
diff --git a/Doc/tutorial/stdlib.rst b/Doc/tutorial/stdlib.rst
index f32063e..a52653b 100644
--- a/Doc/tutorial/stdlib.rst
+++ b/Doc/tutorial/stdlib.rst
@@ -72,21 +72,23 @@ three`` at the command line::
>>> print(sys.argv)
['demo.py', 'one', 'two', 'three']
-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!')
+The :mod:`argparse` module provides a more sophisticated mechanism to process
+command line arguments. The following script extracts one or more filenames
+and an optional number of lines to be displayed::
+
+ import argparse
+
+ parser = argparse.ArgumentParser(prog = 'top',
+ description = 'Show top lines from each file')
+ parser.add_argument('filenames', nargs='+')
+ parser.add_argument('-l', '--lines', type=int, default=10)
+ args = parser.parse_args()
+ print(args)
+
+When run at the command line with ``python top.py --lines=5 alpha.txt
+beta.txt``, the script sets ``args.lines`` to ``5`` and ``args.filenames``
+to ``['alpha.txt', 'beta.txt']``.
+
.. _tut-stderr: