mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
bpo-38049: Add command-line interface for the ast module. (GH-15724)
This commit is contained in:
parent
b9f65f01fd
commit
832e864008
3 changed files with 61 additions and 0 deletions
24
Lib/ast.py
24
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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue