summaryrefslogtreecommitdiff
path: root/Lib/ast.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2019-09-09 23:36:13 +0300
committerGitHub <noreply@github.com>2019-09-09 23:36:13 +0300
commit832e8640086ac4fa547c055a72929879cc5a963a (patch)
treea53bf8e8b0b8982eb1c1fbe6b858a426f3284c24 /Lib/ast.py
parentb9f65f01fd761da7799f36d29b54518399d3458e (diff)
downloadcpython-git-832e8640086ac4fa547c055a72929879cc5a963a.tar.gz
bpo-38049: Add command-line interface for the ast module. (GH-15724)
Diffstat (limited to 'Lib/ast.py')
-rw-r--r--Lib/ast.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/ast.py b/Lib/ast.py
index 498484f198..720dd48a76 100644
--- a/Lib/ast.py
+++ b/Lib/ast.py
@@ -550,3 +550,27 @@ _const_node_type_names = {
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()