diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst --- a/Doc/library/argparse.rst +++ b/Doc/library/argparse.rst @@ -607,8 +607,8 @@ ------------------------- .. method:: ArgumentParser.add_argument(name or flags..., [action], [nargs], \ - [const], [default], [type], [choices], [required], \ - [help], [metavar], [dest]) + const=None, default=None, [type], [choices], \ + [required], [help], [metavar], [dest]) Define how a single command-line argument should be parsed. Each parameter has its own more detailed description below, but in short they are: @@ -909,21 +909,25 @@ default ^^^^^^^ -All optional arguments and some positional arguments may be omitted at the -command line. The ``default`` keyword argument of -:meth:`~ArgumentParser.add_argument`, whose value defaults to ``None``, -specifies what value should be used if the command-line argument is not present. -For optional arguments, the ``default`` value is used when the option string -was not present at the command line:: +The *default* keyword argument of :meth:`~ArgumentParser.add_argument` +(default: ``None``) specifies what value to use if a non-required argument +is omitted from the command line. For optional arguments, this occurs when +the option string is not present (but not when the option string is present +with no arguments --- see const_ for that case):: >>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', default=42) - >>> parser.parse_args('--foo 2'.split()) + >>> parser.parse_args(['--foo', '2']) Namespace(foo='2') - >>> parser.parse_args(''.split()) + >>> parser.parse_args([]) Namespace(foo=42) -If the ``default`` value is a string, the parser parses the value as if it +For positional arguments, an argument can be omitted when nargs_ equals +``'?'`` or ``'*'``. If *nargs* equals ``'*'`` and *default* is ``None`` for +a positional argument, an empty list ``[]`` is used as the default value +instead of ``None``. + +If *default* is a string, the parser parses the value as if it were a command-line argument. In particular, the parser applies any type_ conversion argument, if provided, before setting the attribute on the :class:`Namespace` return value. Otherwise, the parser uses the value as is:: @@ -931,22 +935,11 @@ >>> parser = argparse.ArgumentParser() >>> parser.add_argument('--length', default='10', type=int) >>> parser.add_argument('--width', default=10.5, type=int) - >>> parser.parse_args() + >>> parser.parse_args([]) Namespace(length=10, width=10.5) -For positional arguments with nargs_ equal to ``?`` or ``*``, the ``default`` value -is used when no command-line argument was present:: - - >>> parser = argparse.ArgumentParser() - >>> parser.add_argument('foo', nargs='?', default=42) - >>> parser.parse_args('a'.split()) - Namespace(foo='a') - >>> parser.parse_args(''.split()) - Namespace(foo=42) - - Providing ``default=argparse.SUPPRESS`` causes no attribute to be added if the -command-line argument was not present.:: +command-line argument was not present:: >>> parser = argparse.ArgumentParser() >>> parser.add_argument('--foo', default=argparse.SUPPRESS)