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

bpo-21150: add quick link/summary table to the top of argparse documentation #12005

Merged
merged 8 commits into from
Apr 18, 2022
98 changes: 98 additions & 0 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,75 @@ 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`
Copy link
Member

Choose a reason for hiding this comment

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

Just a suggestion, as they points to the exact same place, it may be lighter by removing this 2nd link. Also it's obvious a class creates an instance of itself.

Suggested change
The :class:`argparse.ArgumentParser` creates a new :class:`ArgumentParser`
The :class:`argparse.ArgumentParser` creates a new parser

object. Commonly used arguments include prog_, description_, and
formatter_class_. For example, the user can create an instance of
Copy link
Member

@JulienPalard JulienPalard Sep 11, 2019

Choose a reason for hiding this comment

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

the user often mention the person using the Python program, not the person writing it. Maybe just remove this sentence and remplace by a simple "Example::" ?

Yes I also try to shorten your paragraph a bit: it's just an introduction, the real thing is documented further down, so if you want for it to be read you have to keep it short, people tend to just skip the introductions.

:class:`ArgumentParser` through the following::

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

Choose a reason for hiding this comment

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

This line is too long (spawns an horizontal scrollbar in the example box, in the rendered HTML), can you please use hanging indentation, like:

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`
Copy link
Member

Choose a reason for hiding this comment

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

Same, you could drop the last sentence, its single link is redundent, make the paragraph a bit long, and add no real information, "Example::" is enough.

is as follows::

>>> parser.add_argument('-v', '--verbose', action='store_true',
Copy link
Member

Choose a reason for hiding this comment

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

For consistency please also use hanging indentation here, with a single argument per line.

... help='Show various debugging information')


Basic Usage of :func:`add_argument`
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


**Name or Flags Type**
Copy link
Member

Choose a reason for hiding this comment

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

This table links to nothing, which were the initial idea of this PR (adding table with links), so I bet it would read better as a sentence like The first parameter of add_argument tells if it's we're describing a positional argument (without dashes, like ``"foo"``) or an optional one (prefixed by dashes, likes `"--verbose"`). (I'm not native english, please reword as appropriate).


====================== ===========================
Type Example
====================== ===========================
Positional ``'foo'``
Optional ``'-v'``, ``'--verbose'``
Copy link
Member

@merwok merwok Feb 25, 2019

Choose a reason for hiding this comment

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

Is there an argparse term to name this kind of parameter?

Required vs. optional is orthogonal to arg vs. --param arg.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at the argparse documentation, it seems like they use positional and optional for these parameters. Is there something that I can do to make this section more clear?

Copy link
Member

Choose a reason for hiding this comment

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

Ah if that’s the terminology then keep it.

====================== ===========================


**Basic Arguments:**

====================== =========================================================== =========================================================================================================================
Name Description Keywords
====================== =========================================================== =========================================================================================================================
action_ Specifies how an argument should be handled ``'store'``, ``'store_const'``, ``'store_true'``, ``'append'``, ``'append_const'``, ``'count'``, ``'help'``, ``'version'``
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()``
====================== =========================================================== =======================================================================================================================



Example
-------

Expand Down Expand Up @@ -185,6 +254,8 @@ ArgumentParser objects
The following sections describe how each of these are used.


.. _prog:

prog
^^^^

Expand Down Expand Up @@ -282,6 +353,8 @@ The ``%(prog)s`` format specifier is available to fill in the program name in
your usage messages.


.. _description:

description
^^^^^^^^^^^

Expand Down Expand Up @@ -362,6 +435,8 @@ and one in the child) and raise an error.
not be reflected in the child.


.. _formatter_class:

formatter_class
^^^^^^^^^^^^^^^

Expand Down Expand Up @@ -683,6 +758,8 @@ The add_argument() method
The following sections describe how each of these are used.


.. _name_or_flags:

name or flags
^^^^^^^^^^^^^

Expand Down Expand Up @@ -715,6 +792,8 @@ be positional::
PROG: error: the following arguments are required: bar


.. _action:

action
^^^^^^

Expand Down Expand Up @@ -824,6 +903,9 @@ An example of a custom action::

For more details, see :class:`Action`.


.. _nargs:

nargs
^^^^^

Expand Down Expand Up @@ -924,6 +1006,8 @@ is determined by the action_. Generally this means a single command-line argume
will be consumed and a single item (not a list) will be produced.


.. _const:

const
^^^^^

Expand All @@ -947,6 +1031,8 @@ With the ``'store_const'`` and ``'append_const'`` actions, the ``const``
keyword argument must be given. For other actions, it defaults to ``None``.


.. _default:

default
^^^^^^^

Expand Down Expand Up @@ -997,6 +1083,8 @@ command-line argument was not present::
Namespace(foo='1')


.. _type:

type
^^^^

Expand Down Expand Up @@ -1059,6 +1147,8 @@ simply check against a range of values::
See the choices_ section for more details.


.. _choices:

choices
^^^^^^^

Expand Down Expand Up @@ -1094,6 +1184,8 @@ value, so :class:`dict` objects, :class:`set` objects, custom containers,
etc. are all supported.


.. _required:

required
^^^^^^^^

Expand All @@ -1120,6 +1212,8 @@ present at the command line.
*options* to be *optional*, and thus they should be avoided when possible.


.. _help:

help
^^^^

Expand Down Expand Up @@ -1175,6 +1269,8 @@ setting the ``help`` value to ``argparse.SUPPRESS``::
-h, --help show this help message and exit


.. _metavar:

metavar
^^^^^^^

Expand Down Expand Up @@ -1239,6 +1335,8 @@ arguments::
--foo bar baz


.. _dest:

dest
^^^^

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Place summary/quick links table at the top of the documentation for the argparse module to benefit ease of use.