Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean-up the argparse docs quick links table #91726

Merged
merged 41 commits into from
Apr 20, 2022
Merged
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
bbd2da9
Merge pull request #1 from python/master
rhettinger Mar 16, 2021
74bdf1b
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
6c53f1a
Merge branch 'master' of github.com:python/cpython
rhettinger Mar 22, 2021
a487c4f
.
rhettinger Mar 24, 2021
eb56423
.
rhettinger Mar 25, 2021
cc7ba06
.
rhettinger Mar 26, 2021
d024dd0
.
rhettinger Apr 22, 2021
b10f912
merge
rhettinger May 5, 2021
fb6744d
merge
rhettinger May 6, 2021
7f21a1c
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 15, 2021
7da42d4
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Aug 25, 2021
e31757b
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 31, 2021
f058a6f
Merge branch 'main' of github.com:python/cpython
rhettinger Aug 31, 2021
1fc29bd
Merge branch 'main' of github.com:python/cpython
rhettinger Sep 4, 2021
e5c0184
Merge branch 'main' of github.com:python/cpython
rhettinger Oct 30, 2021
3c86ec1
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 9, 2021
96675e4
Merge branch 'main' of github.com:rhettinger/cpython
rhettinger Nov 9, 2021
de558c6
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 9, 2021
418a07f
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 14, 2021
ea23a8b
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 21, 2021
ba248b7
Merge branch 'main' of github.com:python/cpython
rhettinger Nov 27, 2021
9bc1df1
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 1, 2021
d4466ba
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 1, 2021
a89f02e
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 8, 2021
aae9a5f
Merge branch 'main' of github.com:python/cpython
rhettinger Dec 10, 2021
7ba634b
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 1, 2022
4910ba3
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 5, 2022
0e8d64a
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 9, 2022
7e49f3e
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 10, 2022
6257706
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 18, 2022
2fb7e2c
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 23, 2022
b345021
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 23, 2022
cbb9ace
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 23, 2022
7642c27
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 23, 2022
2320c28
Merge branch 'main' of github.com:python/cpython
rhettinger Jan 30, 2022
9dbc96c
Merge branch 'main' of github.com:python/cpython
rhettinger Feb 3, 2022
c7c9c0f
Merge branch 'main' of github.com:python/cpython
rhettinger Apr 7, 2022
23ed5e3
Merge branch 'main' of github.com:python/cpython
rhettinger Apr 18, 2022
c4f5cd1
Merge branch 'main' of github.com:python/cpython
rhettinger Apr 20, 2022
0499cc5
Clean-up the argparse docs quick links table
rhettinger Apr 20, 2022
6418ad4
Fix table and make the core functionality summary more focused
rhettinger Apr 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 33 additions & 55 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,73 +26,51 @@ module also automatically generates help and usage messages and issues errors
when users give the program invalid arguments.


Summary
-------

Core Functionality
^^^^^^^^^^^^^^^^^^
------------------

The :mod:`argparse` module's support for command-line interfaces is built
from the following:

The :class:`argparse.ArgumentParser` creates a new :class:`ArgumentParser`
object. Commonly used arguments include prog_, description_, and
formatter_class_. For example, the user can create an instance of
:class:`ArgumentParser` through the following::

>>> parser = argparse.ArgumentParser(prog='PROG', description='DESC',
... formatter_class=argparse.RawDescriptionHelpFormatter)

The :func:`ArgumentParser.add_argument` is a function that is used
to define how a single command-line argument should be parsed. Commonly used
arguments include `name or flags`_, action_, default_, type_, required_,
and help_. An example of the function :func:`ArgumentParser.add_argument`
is as follows::

>>> parser.add_argument('-v', '--verbose', action='store_true',
... help='Show various debugging information')
around an instance of :class:`argparse.ArgumentParser`. It is a container for
argument specifications and has options that apply the parser as whole::

parser = argparse.ArgumentParser(
prog = 'ProgramName',
description = 'What the program does',
epilog = 'Text at the bottom of help')

Basic Usage of :func:`add_argument`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The :func:`ArgumentParser.add_argument` function attaches individual argument
specifications to the parser. It supports positional arguments, options that
accept values, and on/off flags::

parser.add_argument('filename') # positional argument
parser.add_argument('-c', '--count') # option that takes value
parser.add_argument('-v', '--verbose',
action='store_true') # on/off flag

**Name or Flags Type**
The :func:`ArgumentParser.parse_args` function runs the parser and puts
the extracted data in a :class:`argparse.Namespace` object::

====================== ===========================
Type Example
====================== ===========================
Positional ``'foo'``
Optional ``'-v'``, ``'--verbose'``
====================== ===========================
args = parser.parse_args()
print(args.filename, args.count, args.verbose)


**Basic Arguments:**
Quick Links for add_argument()
------------------------------

====================== =========================================================== =========================================================================================================================
Name Description Keywords
====================== =========================================================== =========================================================================================================================
action_ Specifies how an argument should be handled ``'store'``, ``'store_const'``, ``'store_true'``, ``'append'``, ``'append_const'``, ``'count'``, ``'help'``, ``'version'``
====================== =========================================================== ==========================================================================================================================
Name Description Values
====================== =========================================================== ==========================================================================================================================
action_ Specify how an argument should be handled ``'store'``, ``'store_const'``, ``'store_true'``, ``'append'``, ``'append_const'``, ``'count'``, ``'help'``, ``'version'``
choices_ Limit values to specific set of choices ``['foo', 'bar']``, ``range(1, 10)``, or an object that supports ``in`` operator
const_ Store a constant value
default_ Default value used when an argument is not provided
type_ Automatically converts an argument to the given type :class:`int`, :class:`float`, :class:`bool`, ``argparse.FileType('w')``, ``callable function``
help_ Help message of an argument
====================== =========================================================== =========================================================================================================================



**Advanced Arguments:**

====================== =========================================================== =======================================================================================================================
Name Description Keywords
====================== =========================================================== =======================================================================================================================
nargs_ Associates a single action with the number of arguments ``N`` (:class:`int`), ``'?'``, ``'*'``, ``'+'``, ``argparse.REMAINDER``
const_ Stores constant values of names or flags
choices_ A container that lists the possible values ``['foo', 'bar']``, ``range(1, 10)``, Any object that supports ``in`` operator
required_ Indicates if an optional argument is required or not ``True``, ``False``
metavar_ An alternative display name for the argument
dest_ Specifies name of attribute to be used in ``parse_args()``
====================== =========================================================== =======================================================================================================================

dest_ Specify the attribute name in result namespace
help_ Help message for an argument
metavar_ Alternate display name for the argument as shown in help
nargs_ Number of times the argument can be used ``N`` (:class:`int`), ``'?'``, ``'*'``, ``'+'``, ``argparse.REMAINDER``
required_ Indicate whether an optional argument is required or not ``True``, ``False``
type_ Automatically convert an argument to the given type :class:`int`, :class:`float`, ``argparse.FileType('w')``, or any callable function
====================== =========================================================== ==========================================================================================================================


Example
Expand Down