Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add script that uses the clang-format executable to format all project files #817

Closed
wants to merge 6 commits into from
59 changes: 59 additions & 0 deletions format_project.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import subprocess
import argparse


def format_files(directory, extensions, clang_format_path, config_path):
formatted_files = 0
for root, _, files in os.walk(directory):
for file in files:
if any(file.endswith(ext) for ext in extensions):
file_path = os.path.join(root, file)
try:
format(clang_format_path, config_path, file_path)
formatted_files += 1
except Exception as e:
print(
f"Unexpected error occurred while formatting {file_path}. Reason: {e}. Skipping..."
)

print(f"\nTotal files formatted: {formatted_files}")


def format(clang_format_path, config_path, file_path):
subprocess.run(
[
clang_format_path,
"-i",
"-style=file",
f"-assume-filename={config_path}",
file_path,
],
check=True,
)
print(f"Formatted: {file_path}")


def main():
parser = argparse.ArgumentParser(description="Format C++ files using clang-format")
parser.add_argument("directory", help="Directory to search for files")
parser.add_argument(
"--clang-format", default="clang-format", help="Path to clang-format executable"
)
parser.add_argument(
"--config", default=".clang-format", help="Path to .clang-format config file"
)
parser.add_argument(
"--extensions",
nargs="+",
default=[".cpp", ".hpp", ".h", ".c"],
help="File extensions to format",
)

args = parser.parse_args()

format_files(args.directory, args.extensions, args.clang_format, args.config)


if __name__ == "__main__":
main()
Loading