@@ -72,21 +72,23 @@ three`` at the command line::
72
72
>>> print (sys.argv)
73
73
['demo.py', 'one', 'two', 'three']
74
74
75
- The :mod: `argparse ` module provides a mechanism to process command line arguments.
76
- It should always be preferred over directly processing ``sys.argv `` manually.
77
-
78
- Take, for example, the below snippet of code::
79
-
80
- >>> import argparse
81
- >>> from getpass import getuser
82
- >>> parser = argparse.ArgumentParser(description='An argparse example.')
83
- >>> parser.add_argument('name', nargs='?', default=getuser(), help='The name of someone to greet.')
84
- >>> parser.add_argument('--verbose', '-v', action='count')
85
- >>> args = parser.parse_args()
86
- >>> greeting = ["Hi", "Hello", "Greetings! its very nice to meet you"][args.verbose % 3]
87
- >>> print(f'{greeting}, {args.name}')
88
- >>> if not args.verbose:
89
- >>> print('Try running this again with multiple "-v" flags!')
75
+ The :mod: `argparse ` module provides a more sophisticated mechanism to process
76
+ command line arguments. The following script extracts one or more filenames
77
+ and an optional number of lines to be displayed::
78
+
79
+ import argparse
80
+
81
+ parser = argparse.ArgumentParser(prog = 'top',
82
+ description = 'Show top lines from each file')
83
+ parser.add_argument('filenames', nargs='+')
84
+ parser.add_argument('-l', '--lines', type=int, default=10)
85
+ args = parser.parse_args()
86
+ print(args)
87
+
88
+ When run at the command line with ``python top.py --lines=5 alpha.txt
89
+ beta.txt ``, the script sets ``args.lines `` to ``5 `` and ``args.filenames ``
90
+ to ``['alpha.txt', 'beta.txt'] ``.
91
+
90
92
91
93
.. _tut-stderr :
92
94
0 commit comments