Skip to content
21 changes: 17 additions & 4 deletions Doc/library/ast.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ compiled into a Python code object using the built-in :func:`compile` function.

.. _abstract-grammar:

Abstract Grammar
Abstract grammar
----------------

The abstract grammar is currently defined as follows:
Expand Down Expand Up @@ -2159,7 +2159,7 @@ Async and await
occurrences of the same value (e.g. :class:`ast.Add`).


:mod:`ast` Helpers
:mod:`ast` helpers
------------------

Apart from the node classes, the :mod:`ast` module defines these utility functions
Expand Down Expand Up @@ -2484,7 +2484,7 @@ and classes for traversing abstract syntax trees:

.. _ast-compiler-flags:

Compiler Flags
Compiler flags
--------------

The following flags may be passed to :func:`compile` in order to change
Expand Down Expand Up @@ -2533,7 +2533,7 @@ effects on the compilation of a program:

.. _ast-cli:

Command-Line Usage
Command-line usage
------------------

.. versionadded:: 3.9
Expand Down Expand Up @@ -2572,6 +2572,19 @@ The following options are accepted:

Indentation of nodes in AST (number of spaces).

.. option:: --feature-version <version>

Minor version (int) or 3.x tuple (e.g., 3.10).

.. option:: -o <level>
--optimize <level>

Optimization level for parser.

.. option:: --show-empty

Show empty lists and fields in dump output.

If :file:`infile` is specified its contents are parsed to AST and dumped
to stdout. Otherwise, the content is read from stdin.

Expand Down
4 changes: 4 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,10 @@ ast
that the root node type is appropriate.
(Contributed by Irit Katriel in :gh:`130139`.)

* Add new ``--feature-version``, ``--optimize``, ``--show-empty`` options to
command-line interface.
(Contributed by Semyon Moroz in :gh:`133367`.)

bdb
---

Expand Down
26 changes: 24 additions & 2 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,14 @@ def main():
'column offsets')
parser.add_argument('-i', '--indent', type=int, default=3,
help='indentation of nodes (number of spaces)')
parser.add_argument('--feature-version',
type=str, default=None, metavar='VERSION',
help='minor version (int) or 3.x tuple (e.g., 3.10)')
parser.add_argument('-o', '--optimize',
type=int, default=-1, metavar='LEVEL',
help='optimization level for parser (default -1)')
parser.add_argument('--show-empty', default=False, action='store_true',
help='show empty lists and fields in dump output')
args = parser.parse_args()

if args.infile == '-':
Expand All @@ -652,8 +660,22 @@ def main():
name = args.infile
with open(args.infile, 'rb') as infile:
source = infile.read()
tree = parse(source, name, args.mode, type_comments=args.no_type_comments)
print(dump(tree, include_attributes=args.include_attributes, indent=args.indent))

# Process feature_version
feature_version = None
if args.feature_version:
if '.' in args.feature_version:
major_minor = tuple(map(int, args.feature_version.split('.', 1)))
if len(major_minor) != 2 or major_minor[0] != 3:
parser.error("--feature-version must be 3.x tuple (e.g., 3.10)")
feature_version = major_minor
else:
feature_version = int(args.feature_version)

tree = parse(source, name, args.mode, type_comments=args.no_type_comments,
feature_version=feature_version, optimize=args.optimize)
print(dump(tree, include_attributes=args.include_attributes,
indent=args.indent, show_empty=args.show_empty))

if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add ``--feature-version``, ``--optimize``, ``--show-empty`` options for
:mod:`ast` command-line interface. Patch by Semyon Moroz.
Loading