mirror of
https://github.com/python/cpython.git
synced 2025-07-24 03:35:53 +00:00
Clean-up the argparse docs quick links table (GH-91726)
This commit is contained in:
parent
4d2403fd50
commit
26f2e688b8
1 changed files with 34 additions and 56 deletions
|
@ -26,73 +26,51 @@ module also automatically generates help and usage messages and issues errors
|
||||||
when users give the program invalid arguments.
|
when users give the program invalid arguments.
|
||||||
|
|
||||||
|
|
||||||
Summary
|
|
||||||
-------
|
|
||||||
|
|
||||||
Core Functionality
|
Core Functionality
|
||||||
^^^^^^^^^^^^^^^^^^
|
------------------
|
||||||
|
|
||||||
The :mod:`argparse` module's support for command-line interfaces is built
|
The :mod:`argparse` module's support for command-line interfaces is built
|
||||||
from the following:
|
around an instance of :class:`argparse.ArgumentParser`. It is a container for
|
||||||
|
argument specifications and has options that apply the parser as whole::
|
||||||
|
|
||||||
The :class:`argparse.ArgumentParser` creates a new :class:`ArgumentParser`
|
parser = argparse.ArgumentParser(
|
||||||
object. Commonly used arguments include prog_, description_, and
|
prog = 'ProgramName',
|
||||||
formatter_class_. For example, the user can create an instance of
|
description = 'What the program does',
|
||||||
:class:`ArgumentParser` through the following::
|
epilog = 'Text at the bottom of help')
|
||||||
|
|
||||||
>>> parser = argparse.ArgumentParser(prog='PROG', description='DESC',
|
The :func:`ArgumentParser.add_argument` function attaches individual argument
|
||||||
... formatter_class=argparse.RawDescriptionHelpFormatter)
|
specifications to the parser. It supports positional arguments, options that
|
||||||
|
accept values, and on/off flags::
|
||||||
|
|
||||||
The :func:`ArgumentParser.add_argument` is a function that is used
|
parser.add_argument('filename') # positional argument
|
||||||
to define how a single command-line argument should be parsed. Commonly used
|
parser.add_argument('-c', '--count') # option that takes value
|
||||||
arguments include `name or flags`_, action_, default_, type_, required_,
|
parser.add_argument('-v', '--verbose',
|
||||||
and help_. An example of the function :func:`ArgumentParser.add_argument`
|
action='store_true') # on/off flag
|
||||||
is as follows::
|
|
||||||
|
|
||||||
>>> parser.add_argument('-v', '--verbose', action='store_true',
|
The :func:`ArgumentParser.parse_args` function runs the parser and puts
|
||||||
... help='Show various debugging information')
|
the extracted data in a :class:`argparse.Namespace` object::
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
print(args.filename, args.count, args.verbose)
|
||||||
|
|
||||||
|
|
||||||
Basic Usage of :func:`add_argument`
|
Quick Links for add_argument()
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
------------------------------
|
||||||
|
|
||||||
|
====================== =========================================================== ==========================================================================================================================
|
||||||
**Name or Flags Type**
|
Name Description Values
|
||||||
|
====================== =========================================================== ==========================================================================================================================
|
||||||
====================== ===========================
|
action_ Specify how an argument should be handled ``'store'``, ``'store_const'``, ``'store_true'``, ``'append'``, ``'append_const'``, ``'count'``, ``'help'``, ``'version'``
|
||||||
Type Example
|
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
|
||||||
Positional ``'foo'``
|
|
||||||
Optional ``'-v'``, ``'--verbose'``
|
|
||||||
====================== ===========================
|
|
||||||
|
|
||||||
|
|
||||||
**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
|
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``
|
dest_ Specify the attribute name in result namespace
|
||||||
help_ Help message of an argument
|
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
|
||||||
**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
|
Example
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue