Skip to content

Commit 814d687

Browse files
isidenticalvstinner
authored andcommitted
bpo-38348: Extend command line options of ast parsing tool (GH-16540)
Add -i and --indent (indentation level), and --no-type-comments (type comments) command line options to ast parsing tool.
1 parent a322f50 commit 814d687

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

Doc/library/ast.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,19 @@ The following options are accepted:
381381
Specify what kind of code must be compiled, like the *mode* argument
382382
in :func:`parse`.
383383

384+
.. cmdoption:: --no-type-comments
385+
386+
Don't parse type comments.
387+
384388
.. cmdoption:: -a, --include-attributes
385389

386390
Include attributes such as line numbers and column offsets.
387391

392+
.. cmdoption:: -i <indent>
393+
--indent <indent>
394+
395+
Indentation of nodes in AST (number of spaces).
396+
388397
If :file:`infile` is specified its contents are parsed to AST and dumped
389398
to stdout. Otherwise, the content is read from stdin.
390399

Lib/ast.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,15 +1258,19 @@ def main():
12581258
parser.add_argument('-m', '--mode', default='exec',
12591259
choices=('exec', 'single', 'eval', 'func_type'),
12601260
help='specify what kind of code must be parsed')
1261+
parser.add_argument('--no-type-comments', default=True, action='store_false',
1262+
help="don't add information about type comments")
12611263
parser.add_argument('-a', '--include-attributes', action='store_true',
12621264
help='include attributes such as line numbers and '
12631265
'column offsets')
1266+
parser.add_argument('-i', '--indent', type=int, default=3,
1267+
help='indentation of nodes (number of spaces)')
12641268
args = parser.parse_args()
12651269

12661270
with args.infile as infile:
12671271
source = infile.read()
1268-
tree = parse(source, args.infile.name, args.mode, type_comments=True)
1269-
print(dump(tree, include_attributes=args.include_attributes, indent=3))
1272+
tree = parse(source, args.infile.name, args.mode, type_comments=args.no_type_comments)
1273+
print(dump(tree, include_attributes=args.include_attributes, indent=args.indent))
12701274

12711275
if __name__ == '__main__':
12721276
main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add ``-i`` and ``--indent`` (indentation level), and ``--no-type-comments``
2+
(type comments) command line options to ast parsing tool.

0 commit comments

Comments
 (0)