bpo-37726: Prefer argparse over getopt in stdlib tutorial (GH-15052) (#15069)

(cherry picked from commit 2491134029)

Co-authored-by: mental <m3nta1@yahoo.com>
This commit is contained in:
Miss Islington (bot) 2019-08-01 07:35:20 -07:00 committed by Guido van Rossum
parent 35d9c37e27
commit 8990ac0ab0
2 changed files with 16 additions and 3 deletions

View File

@ -72,10 +72,21 @@ three`` at the command line::
>>> print(sys.argv) >>> print(sys.argv)
['demo.py', 'one', 'two', 'three'] ['demo.py', 'one', 'two', 'three']
The :mod:`getopt` module processes *sys.argv* using the conventions of the Unix The :mod:`argparse` module provides a mechanism to process command line arguments.
:func:`getopt` function. More powerful and flexible command line processing is It should always be preferred over directly processing ``sys.argv`` manually.
provided by the :mod:`argparse` module.
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: .. _tut-stderr:

View File

@ -0,0 +1,2 @@
Stop recommending getopt in the tutorial for command line argument parsing
and promote argparse.