[Python-checkins] bpo-38049: Add command-line interface for the ast module. (GH-15724)

Serhiy Storchaka webhook-mailer at python.org
Mon Sep 9 16:36:17 EDT 2019


https://github.com/python/cpython/commit/832e8640086ac4fa547c055a72929879cc5a963a
commit: 832e8640086ac4fa547c055a72929879cc5a963a
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019-09-09T23:36:13+03:00
summary:

bpo-38049: Add command-line interface for the ast module. (GH-15724)

files:
A Misc/NEWS.d/next/Library/2019-09-07-12-32-50.bpo-38049.xKP4tf.rst
M Doc/library/ast.rst
M Lib/ast.py

diff --git a/Doc/library/ast.rst b/Doc/library/ast.rst
index cb8e7ec829be..b468f4235df3 100644
--- a/Doc/library/ast.rst
+++ b/Doc/library/ast.rst
@@ -340,6 +340,42 @@ and classes for traversing abstract syntax trees:
       Added the *indent* option.
 
 
+.. _ast-cli:
+
+Command-Line Usage
+------------------
+
+.. versionadded:: 3.9
+
+The :mod:`ast` module can be executed as a script from the command line.
+It is as simple as:
+
+.. code-block:: sh
+
+   python -m ast [-m <mode>] [-a] [infile]
+
+The following options are accepted:
+
+.. program:: ast
+
+.. cmdoption:: -h, --help
+
+   Show the help message and exit.
+
+.. cmdoption:: -m <mode>
+               --mode <mode>
+
+   Specify what kind of code must be compiled, like the *mode* argument
+   in :func:`parse`.
+
+.. cmdoption:: -a, --include-attributes
+
+   Include attributes such as line numbers and column offsets.
+
+If :file:`infile` is specified its contents are parsed to AST and dumped
+to stdout.  Otherwise, the content is read from stdin.
+
+
 .. seealso::
 
     `Green Tree Snakes <https://greentreesnakes.readthedocs.io/>`_, an external documentation resource, has good
diff --git a/Lib/ast.py b/Lib/ast.py
index 498484f19855..720dd48a761b 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -550,3 +550,27 @@ def __new__(cls, *args, **kwargs):
     bytes: 'Bytes',
     type(...): 'Ellipsis',
 }
+
+
+def main():
+    import argparse
+
+    parser = argparse.ArgumentParser(prog='python -m ast')
+    parser.add_argument('infile', type=argparse.FileType(mode='rb'), nargs='?',
+                        default='-',
+                        help='the file to parse; defaults to stdin')
+    parser.add_argument('-m', '--mode', default='exec',
+                        choices=('exec', 'single', 'eval', 'func_type'),
+                        help='specify what kind of code must be parsed')
+    parser.add_argument('-a', '--include-attributes', action='store_true',
+                        help='include attributes such as line numbers and '
+                             'column offsets')
+    args = parser.parse_args()
+
+    with args.infile as infile:
+        source = infile.read()
+    tree = parse(source, args.infile.name, args.mode, type_comments=True)
+    print(dump(tree, include_attributes=args.include_attributes, indent=3))
+
+if __name__ == '__main__':
+    main()
diff --git a/Misc/NEWS.d/next/Library/2019-09-07-12-32-50.bpo-38049.xKP4tf.rst b/Misc/NEWS.d/next/Library/2019-09-07-12-32-50.bpo-38049.xKP4tf.rst
new file mode 100644
index 000000000000..9f17683032f1
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-09-07-12-32-50.bpo-38049.xKP4tf.rst
@@ -0,0 +1 @@
+Added command-line interface for the :mod:`ast` module.



More information about the Python-checkins mailing list