Skip to content

Commit ec737a9

Browse files
committed
port to argparse
1 parent 3ba07e5 commit ec737a9

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

py/dump.py

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,33 @@
11
#!/usr/bin/python3
22

3+
import sys
4+
35

46
def main():
57
from clang.cindex import Index
68
from pprint import pprint
79

8-
from optparse import OptionParser
9-
10-
global opts
11-
12-
parser = OptionParser("usage: %prog [options] {filename} [clang-args*]")
13-
parser.add_option("", "--show-ids", dest="showIDs",
14-
help="don't compute cursor IDs(very slow)",
15-
default=False)
16-
parser.add_option("", "--max-depth", dest="maxDepth",
17-
help="limit cursor expansion to depth N",
18-
metavar="N", type=int, default=None)
19-
parser.disable_interspersed_args()
20-
(opts, args) = parser.parse_args()
10+
from argparse import ArgumentParser
2111

22-
if len(args) == 0:
23-
parser.error('invalid number arguments')
12+
parser = ArgumentParser(
13+
description="dump TU info",
14+
usage="%(prog)s [options] {filename} [clang-args*]")
2415

25-
print("type(args):", type(args), " args: ", args)
16+
parser.add_argument("--show-ids", dest='showIDs',
17+
help="don't compute cursor IDs(very slow)",
18+
default=False)
19+
parser.add_argument("--max-depth", dest='maxDepth',
20+
help="limit cursor expansion to depth N",
21+
metavar="N", type=int, default=None)
22+
opts, unknown = parser.parse_known_args(sys.argv)
2623

2724
index = Index.create()
28-
tu = index.parse(None, args)
25+
tu = index.parse(None, args=sys.argv[1:])
2926
if not tu:
3027
parser.error("unable to load input")
3128

32-
# pprint(('diags', map(get_diag_info, tu.diagnostics)))
33-
# pprint(('nodes', get_info(tu.cursor)))
29+
pprint(('diags', list(map(get_diag_info, tu.diagnostics))))
30+
pprint(('nodes', get_info(opts, tu.cursor)))
3431

3532

3633
def get_diag_info(diag):
@@ -43,7 +40,7 @@ def get_diag_info(diag):
4340
}
4441

4542

46-
def get_cursor_id(cursor, cursor_list=[]):
43+
def get_cursor_id(opts, cursor, cursor_list=[]):
4744
if not opts.showIDs:
4845
return None
4946

@@ -57,21 +54,21 @@ def get_cursor_id(cursor, cursor_list=[]):
5754
return len(cursor_list) - 1
5855

5956

60-
def get_info(node, depth=0):
57+
def get_info(opts, node, depth=0):
6158
if opts.maxDepth is not None and depth >= opts.maxDepth:
6259
children = None
6360
else:
64-
children = [get_info(c, depth+1) for c in node.get_children()]
61+
children = [get_info(opts, c, depth + 1) for c in node.get_children()]
6562

6663
return {
67-
'id': get_cursor_id(node),
64+
'id': get_cursor_id(opts, node),
6865
'kind': node.kind,
6966
'usr': node.get_usr(),
7067
'spelling': node.spelling,
7168
'extent.start': node.extent.start,
7269
'extent.end': node.extent.end,
7370
'is_definition': node.is_definition(),
74-
'definition_id': get_cursor_id(node.get_definition()),
71+
'definition_id': get_cursor_id(opts, node.get_definition()),
7572
'children': children
7673
}
7774

0 commit comments

Comments
 (0)