Skip to content

bpo-21150: Add summary of argparse #1210

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
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
84 changes: 84 additions & 0 deletions Doc/library/argparse.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,61 @@ module also automatically generates help and usage messages and issues errors
when users give the program invalid arguments.


Summary
-------

Core function
Copy link
Member

Choose a reason for hiding this comment

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

s/function/functions/ ?

Also ArgumentParser is a class, not a function.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe «Core functionality» was meant.

^^^^^^^^^^^^^

.. csv-table::
:header: "Name", "Common Used Arguments", "Example"
Copy link
Member

Choose a reason for hiding this comment

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

Commonly*


":class:`argparse.ArgumentParser`", "`prog`_, `description`_, `formatter_class`_", "``parser = argparse.ArgumentParser(prog='PROG', description='DESC', formatter_class=argparse.RawDescriptionHelpFormatter)``"
":func:`ArgumentParser.add_argument`", "`name or flags`_, `action`_, `default`_, `type`_, `required`_, `help`_", "``parser.add_argument('-v', '--verbose', action='store_true', help='Show various debugging information')``"

Copy link
Member

Choose a reason for hiding this comment

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

Why not have two small code blocks rather than a table here?

Copy link
Member

Choose a reason for hiding this comment

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

With a code block it is not possible to link to the individual args, but I'm not sure that's really necessary since there are already links in one of the following tables.


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


**Name or Flags Type**

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


**Basic Arguemnts:**
Copy link
Member

Choose a reason for hiding this comment

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

s/Arguements/Arguments/


====================== =============================================== =========================================================================================================================
Name Description Keywords
====================== =============================================== =========================================================================================================================
`action`_ What to do on arguments ``'store'``, ``'store_const'``, ``'store_true'``, ``'append'``, ``append_const``, ``count``, ``help``, ``version``
Copy link
Member

Choose a reason for hiding this comment

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

Maybe s/on/with the/?

The last few values are missing the quotes.

`default`_ Default value of 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 would say "Default value used when the argument is not provided" to make it clearer.

`type`_ Treat arguments as type ``int``, ``float``, ``bool``, ``argparse.FileType('w')``, ``callable function``
Copy link
Member

Choose a reason for hiding this comment

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

"Automatically converts the argument to the given type"

`help`_ Help message of 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 would use singular "argument" throughout the table, since add_argument and its options only affect a single argument.

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



**Advance Arguments:**

====================== ================================================== =======================================================================================================================
Name Description Keywords
====================== ================================================== =======================================================================================================================
`nargs`_ Associates number of arguments ``N`` (an integer), ``'?'``, ``'*'``, ``'+'``, ``argparse.REMAINDER``
Copy link
Member

Choose a reason for hiding this comment

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

This description is not particularly clear.

You should also decide if you want to use 2nd or 3rd person and do it consistently (e.g. this is 3rd, but then you have "indicate" which is 2nd/imperative).

You could use :class:`int` instead of integer.

`const`_ The constant values of name or flags
Copy link
Member

Choose a reason for hiding this comment

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

This is also not particularly clear.

`choices`_ Arguments should selected from this set or values ``['foo', 'bar']``, ``range(1, 10)``, Any object that support ``in`` operator
Copy link
Member

Choose a reason for hiding this comment

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

"A container that lists the possible values"

(container could link to the glossary entry if there is one)

`required`_ Indicate a optional argument is required or not ``True``, ``False``
Copy link
Member

Choose a reason for hiding this comment

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

s/a /an /

`metavar`_ A alternative display name for argument
Copy link
Member

Choose a reason for hiding this comment

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

s/A/An/

`dest`_ The name used in ``parse_args()``
====================== ================================================== =======================================================================================================================



Example
-------

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


.. _prog:

prog
^^^^
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 the target markup is not necessary. The headings are automatically targets, but the markup to create the link is different (if I remember right):

`:ref:`prog`


Expand Down Expand Up @@ -282,6 +339,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 +421,8 @@ and one in the child) and raise an error.
not be reflected in the child.


.. _formatter_class:

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

Expand Down Expand Up @@ -681,6 +742,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 @@ -713,6 +776,8 @@ be positional::
PROG: error: too few arguments


.. _action:

action
^^^^^^

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

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


.. _nargs:

nargs
^^^^^

Expand Down Expand Up @@ -914,6 +982,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 @@ -937,6 +1007,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 @@ -987,6 +1059,8 @@ command-line argument was not present.::
Namespace(foo='1')


.. _type:

type
^^^^

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


.. _choices:

choices
^^^^^^^

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


.. _required:

required
^^^^^^^^

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


.. _help:

help
^^^^

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


.. _metavar:

metavar
^^^^^^^

Expand Down Expand Up @@ -1229,6 +1311,8 @@ arguments::
--foo bar baz


.. _dest:

dest
^^^^

Expand Down