Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions Doc/tutorial/stdlib.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,35 @@ 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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think sentence should end with .

It should always be preferred over directly processing ``sys.argv`` manually.

Take, for example, the below snippet of code::

>>> import argparse
>>> parser = argparse.ArgumentParser(description='An argparse example.')
>>> parser.add_argument('name', help='The name of someone to greet.')
>>> args = parser.parse_args()
>>> print(f'Hello, {args.name}!')

The three lines of code needed for creating the command line argument formula
are simple to read and understand and are easy to extend in terms of your
applications functionality.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should just let the example stand, instead of asserting that it's simple to read and extend.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was attempting to make obvious my point, but if the examples can stand on their own and have the same effect then I'll gladly omit the statement 👍


What if you wanted to provide a default value?

::

>>> from getpass import getuser
>>> parser.add_argument('name', nargs='?', default=getuser(), help='The name of someone to greet.')

How about if you wanted to control how verbose you are with your output?

::

>>> parser.add_argument('--verbose', '-v', action='count')
>>> greeting = ["Hi", "Hello", "Greetings! its very nice to meet you"][args.verbose % 3]
>>> print(f'{greeting}, {args.name}')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should just include all of these examples in with the first example, and only have a single example, without any additional text. That seems to be how it's done in the rest of the document.


.. _tut-stderr:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Stop recommending getopt in the tutorial for command line argument parsing
and promote argparse.