Skip to content

Commit d2faac6

Browse files
bpo-38678: Improve argparse example in tutorial (GH-17207) (GH-17213)
(cherry picked from commit 04c79d6) Co-authored-by: Raymond Hettinger <rhettinger@users.noreply.github.com>
1 parent 72321c7 commit d2faac6

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

Doc/library/argparse.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -778,10 +778,12 @@ how the command-line arguments should be handled. The supplied actions are:
778778
example, this is useful for increasing verbosity levels::
779779

780780
>>> parser = argparse.ArgumentParser()
781-
>>> parser.add_argument('--verbose', '-v', action='count')
781+
>>> parser.add_argument('--verbose', '-v', action='count', default=0)
782782
>>> parser.parse_args(['-vvv'])
783783
Namespace(verbose=3)
784784

785+
Note, the *default* will be ``None`` unless explicitly set to *0*.
786+
785787
* ``'help'`` - This prints a complete help message for all the options in the
786788
current parser and then exits. By default a help action is automatically
787789
added to the parser. See :class:`ArgumentParser` for details of how the

Doc/tutorial/stdlib.rst

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,23 @@ three`` at the command line::
7272
>>> print(sys.argv)
7373
['demo.py', 'one', 'two', 'three']
7474

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+
9092

9193
.. _tut-stderr:
9294

0 commit comments

Comments
 (0)