summaryrefslogtreecommitdiffstats
path: root/Doc/library/argparse.rst
diff options
context:
space:
mode:
Diffstat (limited to 'Doc/library/argparse.rst')
-rw-r--r--Doc/library/argparse.rst25
1 files changed, 16 insertions, 9 deletions
diff --git a/Doc/library/argparse.rst b/Doc/library/argparse.rst
index ef0db3e..65663d4 100644
--- a/Doc/library/argparse.rst
+++ b/Doc/library/argparse.rst
@@ -865,16 +865,14 @@ See also :ref:`specifying-ambiguous-arguments`. The supported values are:
output files::
>>> parser = argparse.ArgumentParser()
- >>> parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
- ... default=sys.stdin)
- >>> parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'),
- ... default=sys.stdout)
+ >>> parser.add_argument('infile', nargs='?')
+ >>> parser.add_argument('outfile', nargs='?')
>>> parser.parse_args(['input.txt', 'output.txt'])
- Namespace(infile=<_io.TextIOWrapper name='input.txt' encoding='UTF-8'>,
- outfile=<_io.TextIOWrapper name='output.txt' encoding='UTF-8'>)
+ Namespace(infile='input.txt', outfile='output.txt')
+ >>> parser.parse_args(['input.txt'])
+ Namespace(infile='input.txt', outfile=None)
>>> parser.parse_args([])
- Namespace(infile=<_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>,
- outfile=<_io.TextIOWrapper name='<stdout>' encoding='UTF-8'>)
+ Namespace(infile=None, outfile=None)
.. index:: single: * (asterisk); in argparse module
@@ -1033,7 +1031,6 @@ Common built-in types and functions can be used as type converters:
parser.add_argument('distance', type=float)
parser.add_argument('street', type=ascii)
parser.add_argument('code_point', type=ord)
- parser.add_argument('dest_file', type=argparse.FileType('w', encoding='latin-1'))
parser.add_argument('datapath', type=pathlib.Path)
User defined functions can be used as well:
@@ -1827,9 +1824,19 @@ FileType objects
>>> parser.parse_args(['-'])
Namespace(infile=<_io.TextIOWrapper name='<stdin>' encoding='UTF-8'>)
+ .. note::
+
+ If one argument uses *FileType* and then a subsequent argument fails,
+ an error is reported but the file is not automatically closed.
+ This can also clobber the output files.
+ In this case, it would be better to wait until after the parser has
+ run and then use the :keyword:`with`-statement to manage the files.
+
.. versionchanged:: 3.4
Added the *encodings* and *errors* parameters.
+ .. deprecated:: 3.14
+
Argument groups
^^^^^^^^^^^^^^^