-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtplmake.py
More file actions
executable file
·57 lines (43 loc) · 1.48 KB
/
tplmake.py
File metadata and controls
executable file
·57 lines (43 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python
#coding: utf-8
""" 模板生成工具
模板工具存在template包中,以{tpl_name}_tpl.py命名
{tpl_name}_tpl.py 必须实现如下三个函数
tpls() 返回模板内的模板字符串列表
outs() 返回模板输出的文件名列表
params(options) 接受命令行参数,返回模板字符串所需的参数字典
Author: hatlonely(hatlonely@gmail.com)
Date: 2014-10-15
"""
import sys
import argparse
command_example = """
command example:
tplmake.py -t cmakelists -h
tplmake.py -t cppclass -h
tplmake.py -t leetcode -h
tplmake.py -t cmakelists -t exe -p PROJECT -e executable
tplmake.py -t cppclass -c AB -n aa.bb
tplmake.py -t leetcode -q "Two Sum"
"""
def parser():
parser = argparse.ArgumentParser(description='tplmake.py 1.1.0', usage=command_example)
parser.add_argument('-t', '--type', required=True,
help='option value is one of (cmakelists, cppclass)');
return parser
def main():
args = parser().parse_args(sys.argv[1:3])
arg_type = args.type.lower() + "_tpl"
tpl = __import__("template." + arg_type, fromlist=[arg_type])
params = tpl.params(sys.argv[3:])
tpls = tpl.tpls()
outs = tpl.outs()
for i in range(0, len(tpls)):
if outs[i] == 'stdout':
print(tpls[i].format(**params))
else:
out = open(outs[i], 'w')
out.write(tpls[i].format(**params))
out.close()
if __name__ == '__main__':
main()