chore: 更新 clang-formatter.py

1. 允许 格式化单个文件
2. 允许 自定义 style
3. 允许 指定后缀名
This commit is contained in:
zzyyyl
2022-09-22 15:35:34 +08:00
parent faffb391f5
commit 7e6213d983

View File

@@ -1,28 +1,40 @@
from argparse import ArgumentParser
import json
import os
def ArgParser():
parser = ArgumentParser()
parser.add_argument("--input", help="input dir", metavar="I", dest="src")
parser.add_argument("--clang-format", help="the path of clang-format.exe", metavar="PATH", dest="exe", default="clang-format")
parser.add_argument("--style", help="clang-format style", dest="style", default="file")
parser.add_argument("--rule", help="json-style '.xxx' array", dest="rule", default="[\".c\", \".h\", \".cpp\", \".hpp\", \".json\"]")
return parser
args = ArgParser().parse_args()
if __name__ == "__main__":
args = ArgParser().parse_args()
clang_format_exe = args.exe
input_path = args.src
rule_array = json.loads(args.rule)
clang_format_exe = args.exe
input_dir = args.src
os.system(f"{clang_format_exe} --version");
os.system(f"{clang_format_exe} --version");
if not input_path:
print("No input dir.")
elif os.path.isdir(input_path):
if type(rule_array) != list:
print("Invalid rule!")
else:
for root, dirs, files in os.walk(input_path):
for f in files:
file = os.path.join(root, f)
if os.path.splitext(file)[-1] in rule_array:
print(file)
os.system(f"{clang_format_exe} -i -style={args.style} \"{file}\"")
elif os.path.isfile(input_path):
file = input_path
print(file)
os.system(f"{clang_format_exe} -i -style={args.style} \"{file}\"")
else:
print("Invalid input_path!")
if not input_dir:
print("No input dir, exit.")
exit()
for root, dirs, files in os.walk(input_dir):
for f in files:
file = os.path.join(root, f)
if os.path.splitext(file)[-1] in [".c", ".h", ".cpp", ".hpp", ".json"]:
print(file)
os.system(f"{clang_format_exe} -i -style=file \"{file}\"");
# example: python tools\ClangFormater\clang-formater.py --input=src\MeoAssistant
# example: python tools\ClangFormatter\clang-formatter.py --input=src\MeoAssistant