Skip to content

Commit 783fb15

Browse files
committed
gh-133367: Add missing options to ast CLI
1 parent ac7d5ba commit 783fb15

File tree

4 files changed

+47
-6
lines changed

4 files changed

+47
-6
lines changed

Doc/library/ast.rst

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ compiled into a Python code object using the built-in :func:`compile` function.
2929

3030
.. _abstract-grammar:
3131

32-
Abstract Grammar
32+
Abstract grammar
3333
----------------
3434

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

21612161

2162-
:mod:`ast` Helpers
2162+
:mod:`ast` helpers
21632163
------------------
21642164

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

24852485
.. _ast-compiler-flags:
24862486

2487-
Compiler Flags
2487+
Compiler flags
24882488
--------------
24892489

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

25342534
.. _ast-cli:
25352535

2536-
Command-Line Usage
2536+
Command-line usage
25372537
------------------
25382538

25392539
.. versionadded:: 3.9
@@ -2572,6 +2572,19 @@ The following options are accepted:
25722572

25732573
Indentation of nodes in AST (number of spaces).
25742574

2575+
.. option:: --feature-version <version>
2576+
2577+
Minor version (int) or 3.x tuple (e.g., 3.10).
2578+
2579+
.. option:: -o <level>
2580+
--optimize <level>
2581+
2582+
Optimization level for parser.
2583+
2584+
.. option:: --show-empty
2585+
2586+
Show empty lists and fields in dump output.
2587+
25752588
If :file:`infile` is specified its contents are parsed to AST and dumped
25762589
to stdout. Otherwise, the content is read from stdin.
25772590

Doc/whatsnew/3.14.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,6 +1991,10 @@ ast
19911991
Use :attr:`!ast.Constant.value` instead.
19921992
(Contributed by Alex Waygood in :gh:`119562`.)
19931993

1994+
* Added new ``--feature-version``, ``--optimize``, ``--show-empty`` options to
1995+
command-line interface.
1996+
(Contributed by Semyon Moroz in :gh:`133367`.)
1997+
19941998
asyncio
19951999
-------
19962000

Lib/ast.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,14 @@ def main():
643643
'column offsets')
644644
parser.add_argument('-i', '--indent', type=int, default=3,
645645
help='indentation of nodes (number of spaces)')
646+
parser.add_argument('--feature-version',
647+
type=str, default=None, metavar='VERSION',
648+
help='minor version (int) or 3.x tuple (e.g., 3.10)')
649+
parser.add_argument('-o', '--optimize',
650+
type=int, default=-1, metavar='LEVEL',
651+
help='optimization level for parser (default -1)')
652+
parser.add_argument('--show-empty', default=False, action='store_true',
653+
help='show empty lists and fields in dump output')
646654
args = parser.parse_args()
647655

648656
if args.infile == '-':
@@ -652,8 +660,22 @@ def main():
652660
name = args.infile
653661
with open(args.infile, 'rb') as infile:
654662
source = infile.read()
655-
tree = parse(source, name, args.mode, type_comments=args.no_type_comments)
656-
print(dump(tree, include_attributes=args.include_attributes, indent=args.indent))
663+
664+
# Process feature_version
665+
feature_version = None
666+
if args.feature_version:
667+
if '.' in args.feature_version:
668+
major_minor = tuple(map(int, args.feature_version.split('.', 1)))
669+
if len(major_minor) != 2 or major_minor[0] != 3:
670+
parser.error("--feature-version must be 3.x tuple (e.g., 3.10)")
671+
feature_version = major_minor
672+
else:
673+
feature_version = int(args.feature_version)
674+
675+
tree = parse(source, name, args.mode, type_comments=args.no_type_comments,
676+
feature_version=feature_version, optimize=args.optimize)
677+
print(dump(tree, include_attributes=args.include_attributes,
678+
indent=args.indent, show_empty=args.show_empty))
657679

658680
if __name__ == '__main__':
659681
main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add ``--feature-version``, ``--optimize``, ``--show-empty`` flags for
2+
:mod:`ast` command-line interface. Patch by Semyon Moroz.

0 commit comments

Comments
 (0)