@@ -741,6 +741,21 @@ how the command-line arguments should be handled. The supplied actions are:
741741 >>> parser.parse_args('--str --int'.split())
742742 Namespace(types=[<class 'str'>, <class 'int'>])
743743
744+ * ``'extend' `` - This stores a list and appends each item from the multi-value
745+ argument list to it.
746+ The ``'extend' `` action is typically used with the nargs _ keyword argument
747+ value ``'+' `` or ``'*' ``.
748+ Note that when nargs _ is ``None `` (the default) or ``'?' ``, each
749+ character of the argument string will be appended to the list.
750+ Example usage::
751+
752+ >>> parser = argparse.ArgumentParser()
753+ >>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
754+ >>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
755+ Namespace(foo=['f1', 'f2', 'f3', 'f4'])
756+
757+ .. versionadded :: 3.8
758+
744759* ``'count' `` - This counts the number of times a keyword argument occurs. For
745760 example, this is useful for increasing verbosity levels::
746761
@@ -766,17 +781,6 @@ how the command-line arguments should be handled. The supplied actions are:
766781 >>> parser.parse_args(['--version'])
767782 PROG 2.0
768783
769- * ``'extend' `` - This stores a list, and extends each argument value to the
770- list.
771- Example usage::
772-
773- >>> parser = argparse.ArgumentParser()
774- >>> parser.add_argument("--foo", action="extend", nargs="+", type=str)
775- >>> parser.parse_args(["--foo", "f1", "--foo", "f2", "f3", "f4"])
776- Namespace(foo=['f1', 'f2', 'f3', 'f4'])
777-
778- .. versionadded :: 3.8
779-
780784Only actions that consume command-line arguments (e.g. ``'store' ``,
781785``'append' `` or ``'extend' ``) can be used with positional arguments.
782786
@@ -865,16 +869,14 @@ See also :ref:`specifying-ambiguous-arguments`. The supported values are:
865869 output files::
866870
867871 >>> parser = argparse.ArgumentParser()
868- >>> parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
869- ... default=sys.stdin)
870- >>> parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
871- ... default=sys.stdout)
872+ >>> parser.add_argument('infile', nargs='?')
873+ >>> parser.add_argument('outfile', nargs='?')
872874 >>> parser.parse_args(['input.txt', 'output.txt'])
873- Namespace(infile=<_io.TextIOWrapper name='input.txt' encoding='UTF-8'>,
874- outfile=<_io.TextIOWrapper name='output.txt' encoding='UTF-8'>)
875+ Namespace(infile='input.txt', outfile='output.txt')
876+ >>> parser.parse_args(['input.txt'])
877+ Namespace(infile='input.txt', outfile=None)
875878 >>> parser.parse_args([])
876- Namespace(infile=<_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>,
877- outfile=<_io.TextIOWrapper name='<stdout>' encoding='UTF-8'>)
879+ Namespace(infile=None, outfile=None)
878880
879881.. index :: single: * (asterisk); in argparse module
880882
@@ -1033,7 +1035,6 @@ Common built-in types and functions can be used as type converters:
10331035 parser.add_argument('distance', type=float)
10341036 parser.add_argument('street', type=ascii)
10351037 parser.add_argument('code_point', type=ord)
1036- parser.add_argument('dest_file', type=argparse.FileType('w', encoding='latin-1'))
10371038 parser.add_argument('datapath', type=pathlib.Path)
10381039
10391040User defined functions can be used as well:
@@ -1827,9 +1828,19 @@ FileType objects
18271828 >>> parser.parse_args(['-'])
18281829 Namespace(infile=<_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>)
18291830
1831+ .. note ::
1832+
1833+ If one argument uses *FileType * and then a subsequent argument fails,
1834+ an error is reported but the file is not automatically closed.
1835+ This can also clobber the output files.
1836+ In this case, it would be better to wait until after the parser has
1837+ run and then use the :keyword: `with `-statement to manage the files.
1838+
18301839 .. versionchanged :: 3.4
18311840 Added the *encodings * and *errors * parameters.
18321841
1842+ .. deprecated :: 3.14
1843+
18331844
18341845Argument groups
18351846^^^^^^^^^^^^^^^
0 commit comments