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.
This commit is contained in:
Batuhan Taşkaya 2019-12-16 21:23:27 +03:00 committed by Victor Stinner
parent a322f50c36
commit 814d687c7d
3 changed files with 17 additions and 2 deletions

View File

@ -381,10 +381,19 @@ The following options are accepted:
Specify what kind of code must be compiled, like the *mode* argument Specify what kind of code must be compiled, like the *mode* argument
in :func:`parse`. in :func:`parse`.
.. cmdoption:: --no-type-comments
Don't parse type comments.
.. cmdoption:: -a, --include-attributes .. cmdoption:: -a, --include-attributes
Include attributes such as line numbers and column offsets. Include attributes such as line numbers and column offsets.
.. cmdoption:: -i <indent>
--indent <indent>
Indentation of nodes in AST (number of spaces).
If :file:`infile` is specified its contents are parsed to AST and dumped If :file:`infile` is specified its contents are parsed to AST and dumped
to stdout. Otherwise, the content is read from stdin. to stdout. Otherwise, the content is read from stdin.

View File

@ -1258,15 +1258,19 @@ def main():
parser.add_argument('-m', '--mode', default='exec', parser.add_argument('-m', '--mode', default='exec',
choices=('exec', 'single', 'eval', 'func_type'), choices=('exec', 'single', 'eval', 'func_type'),
help='specify what kind of code must be parsed') help='specify what kind of code must be parsed')
parser.add_argument('--no-type-comments', default=True, action='store_false',
help="don't add information about type comments")
parser.add_argument('-a', '--include-attributes', action='store_true', parser.add_argument('-a', '--include-attributes', action='store_true',
help='include attributes such as line numbers and ' help='include attributes such as line numbers and '
'column offsets') 'column offsets')
parser.add_argument('-i', '--indent', type=int, default=3,
help='indentation of nodes (number of spaces)')
args = parser.parse_args() args = parser.parse_args()
with args.infile as infile: with args.infile as infile:
source = infile.read() source = infile.read()
tree = parse(source, args.infile.name, args.mode, type_comments=True) tree = parse(source, args.infile.name, args.mode, type_comments=args.no_type_comments)
print(dump(tree, include_attributes=args.include_attributes, indent=3)) print(dump(tree, include_attributes=args.include_attributes, indent=args.indent))
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View File

@ -0,0 +1,2 @@
Add ``-i`` and ``--indent`` (indentation level), and ``--no-type-comments``
(type comments) command line options to ast parsing tool.