主函数
()
| 108 | |
| 109 | |
| 110 | def main(): |
| 111 | """主函数""" |
| 112 | import argparse |
| 113 | |
| 114 | parser = argparse.ArgumentParser( |
| 115 | description="格式化项目中的 C++ 文件(排除 3rdparty 目录)", |
| 116 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 117 | epilog=""" |
| 118 | 示例: |
| 119 | %(prog)s # 格式化所有文件 |
| 120 | %(prog)s --dry-run # 只检查格式,不修改文件 |
| 121 | %(prog)s --clang-format clang-format-17 # 使用特定版本的 clang-format |
| 122 | """, |
| 123 | ) |
| 124 | parser.add_argument("--dry-run", action="store_true", help="只检查格式,不修改文件") |
| 125 | parser.add_argument( |
| 126 | "--clang-format", |
| 127 | default="clang-format", |
| 128 | help="clang-format 命令(默认: clang-format)", |
| 129 | ) |
| 130 | parser.add_argument( |
| 131 | "--exclude", |
| 132 | nargs="+", |
| 133 | default=["3rdparty"], |
| 134 | help="要排除的目录(默认: 3rdparty)", |
| 135 | ) |
| 136 | parser.add_argument( |
| 137 | "--root", |
| 138 | type=str, |
| 139 | default=None, |
| 140 | help="项目根目录(默认: 脚本所在目录的父目录)", |
| 141 | ) |
| 142 | parser.add_argument("--verbose", action="store_true", help="显示详细信息") |
| 143 | |
| 144 | args = parser.parse_args() |
| 145 | |
| 146 | # 确定根目录 |
| 147 | if args.root: |
| 148 | root_dir = Path(args.root).resolve() |
| 149 | else: |
| 150 | # 默认使用脚本所在目录的父目录(项目根目录) |
| 151 | root_dir = Path(__file__).parent.parent.parent.resolve() |
| 152 | |
| 153 | # 检查 .clang-format 文件是否存在 |
| 154 | clang_format_config = root_dir / ".clang-format" |
| 155 | if not clang_format_config.exists(): |
| 156 | print( |
| 157 | f"错误: 未找到 .clang-format 文件: {clang_format_config}", file=sys.stderr |
| 158 | ) |
| 159 | sys.exit(1) |
| 160 | |
| 161 | if args.verbose: |
| 162 | print(f"项目根目录: {root_dir}") |
| 163 | print(f"配置文件: {clang_format_config}") |
| 164 | print(f"排除目录: {args.exclude}") |
| 165 | print(f"clang-format 命令: {args.clang_format}") |
| 166 | print() |
| 167 |
no test coverage detected