Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
24911340
Commit
24911340
authored
Aug 01, 2019
by
mental
Committed by
Guido van Rossum
Aug 01, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-37726: Prefer argparse over getopt in stdlib tutorial (#15052)
parent
0d30ae1a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
4 deletions
+17
-4
Doc/tutorial/stdlib.rst
Doc/tutorial/stdlib.rst
+15
-4
Misc/NEWS.d/next/Documentation/2019-07-31-11-40-06.bpo-37726.h-3o9a.rst
...xt/Documentation/2019-07-31-11-40-06.bpo-37726.h-3o9a.rst
+2
-0
No files found.
Doc/tutorial/stdlib.rst
View file @
24911340
...
...
@@ -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:
...
...
Misc/NEWS.d/next/Documentation/2019-07-31-11-40-06.bpo-37726.h-3o9a.rst
0 → 100644
View file @
24911340
Stop recommending getopt in the tutorial for command line argument parsing
and promote argparse.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment