(argv: list[str])
| 216 | |
| 217 | |
| 218 | def main(argv: list[str]) -> int: |
| 219 | for stream in (sys.stdout, sys.stderr): |
| 220 | reconfigure = getattr(stream, "reconfigure", None) |
| 221 | if reconfigure is not None: |
| 222 | reconfigure(encoding="utf-8", errors="replace") |
| 223 | |
| 224 | parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) |
| 225 | parser.add_argument( |
| 226 | "--check", action="store_true", help="report counts only, write nothing" |
| 227 | ) |
| 228 | parser.add_argument( |
| 229 | "--diff", action="store_true", help="print a unified diff of each change" |
| 230 | ) |
| 231 | parser.add_argument("paths", nargs="+", type=Path, help="files or directories") |
| 232 | args = parser.parse_args(argv) |
| 233 | |
| 234 | files = sorted(set(_iter_files(args.paths))) |
| 235 | if not files: |
| 236 | print("no C++ source files found", file=sys.stderr) |
| 237 | return 2 |
| 238 | |
| 239 | total_spans = 0 |
| 240 | changed = 0 |
| 241 | for path in files: |
| 242 | raw = path.read_bytes().decode("utf-8", errors="replace") |
| 243 | spans = _inbody_comment_spans(raw.encode("utf-8")) |
| 244 | if not spans: |
| 245 | continue |
| 246 | total_spans += len(spans) |
| 247 | new_text = _strip_text(raw) |
| 248 | if new_text == raw: |
| 249 | continue |
| 250 | changed += 1 |
| 251 | if args.diff: |
| 252 | sys.stdout.writelines( |
| 253 | difflib.unified_diff( |
| 254 | raw.splitlines(keepends=True), |
| 255 | new_text.splitlines(keepends=True), |
| 256 | str(path), |
| 257 | str(path), |
| 258 | ) |
| 259 | ) |
| 260 | if args.check: |
| 261 | print(f"{path}: {len(spans)} in-body comment(s)") |
| 262 | else: |
| 263 | _write_lf(path, new_text) |
| 264 | print(f"{path}: stripped {len(spans)} in-body comment(s)", file=sys.stderr) |
| 265 | |
| 266 | mode = "would strip" if args.check else "stripped" |
| 267 | print( |
| 268 | f"\n{len(files)} files scanned, {mode} {total_spans} comment(s) " |
| 269 | f"across {changed} file(s)", |
| 270 | file=sys.stderr, |
| 271 | ) |
| 272 | return 1 if (args.check and total_spans) else 0 |
| 273 | |
| 274 | |
| 275 | if __name__ == "__main__": |
no test coverage detected