From e32dd0b388e1d8fe0ddb23ba2271c2a71fedc63f Mon Sep 17 00:00:00 2001 From: "steven.bethard" Date: Sat, 28 Mar 2009 14:56:14 +0000 Subject: No really, add documentation for FileType. --- doc/other-utilities.html | 130 +++++++++++++++++++++++++++++++++++++++++ doc/source/other-utilities.rst | 21 +++++++ 2 files changed, 151 insertions(+) create mode 100644 doc/other-utilities.html create mode 100644 doc/source/other-utilities.rst diff --git a/doc/other-utilities.html b/doc/other-utilities.html new file mode 100644 index 0000000..7a63463 --- /dev/null +++ b/doc/other-utilities.html @@ -0,0 +1,130 @@ + + + + + + + Other utilities — argparse v0.9.0 documentation + + + + + + + + + + + + +
+
+
+
+ +
+

Other utilities

+
+

FileType objects

+
+
+ +class FileType(mode='r', bufsize=None)
+

The FileType factory creates objects that can be passed to the type argument of add_argument(). Arguments that have FileType objects as their type will open command-line args as files with the requested modes and buffer sizes:

+
>>> parser = argparse.ArgumentParser()
+>>> parser.add_argument('--output', type=argparse.FileType('wb', 0))
+>>> parser.parse_args(['--output', 'out'])
+Namespace(output=<open file 'out', mode 'wb' at 0x013A2380>)
+
+
+

FileType objects understand the pseudo-argument '-' and automatically convert this into sys.stdin for readable FileType objects and sys.stdout for writable FileType objects:

+
>>> parser = argparse.ArgumentParser()
+>>> parser.add_argument('infile', type=argparse.FileType('r'))
+>>> parser.parse_args(['-'])
+Namespace(infile=<open file '<stdin>', mode 'r' at 0x00ADF020>)
+
+
+
+ +
+
+ + +
+
+
+
+
+

Table Of Contents

+ + +

Previous topic

+

Other methods

+

This Page

+ + + +
+
+
+
+ + + + \ No newline at end of file diff --git a/doc/source/other-utilities.rst b/doc/source/other-utilities.rst new file mode 100644 index 0000000..9fd9d61 --- /dev/null +++ b/doc/source/other-utilities.rst @@ -0,0 +1,21 @@ +Other utilities +=============== + +FileType objects +---------------- + +.. class:: FileType(mode='r', bufsize=None) + + The :class:`FileType` factory creates objects that can be passed to the type argument of :meth:`add_argument`. Arguments that have :class:`FileType` objects as their type will open command-line args as files with the requested modes and buffer sizes: + + >>> parser = argparse.ArgumentParser() + >>> parser.add_argument('--output', type=argparse.FileType('wb', 0)) + >>> parser.parse_args(['--output', 'out']) + Namespace(output=) + + FileType objects understand the pseudo-argument ``'-'`` and automatically convert this into ``sys.stdin`` for readable :class:`FileType` objects and ``sys.stdout`` for writable :class:`FileType` objects: + + >>> parser = argparse.ArgumentParser() + >>> parser.add_argument('infile', type=argparse.FileType('r')) + >>> parser.parse_args(['-']) + Namespace(infile=', mode 'r' at 0x00ADF020>) -- cgit v1.2.1