From 7e6213d98384471ac7cd89af4e043a0edd37e791 Mon Sep 17 00:00:00 2001 From: zzyyyl Date: Thu, 22 Sep 2022 15:35:34 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=20clang-formatter.p?= =?UTF-8?q?y?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 允许 格式化单个文件 2. 允许 自定义 style 3. 允许 指定后缀名 --- tools/ClangFormatter/clang-formatter.py | 44 ++++++++++++++++--------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/tools/ClangFormatter/clang-formatter.py b/tools/ClangFormatter/clang-formatter.py index b1bec8d847..d14e9ab66a 100644 --- a/tools/ClangFormatter/clang-formatter.py +++ b/tools/ClangFormatter/clang-formatter.py @@ -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