Metadata-Version: 2.4
Name: cppgolf
Version: 0.1.9
Summary: C++ multi-file merge & code golf / minifier tool
License: MIT
Project-URL: Homepage, https://github.com/KnCRJVirX/cppgolf
Project-URL: Issues, https://github.com/KnCRJVirX/cppgolf/issues
Keywords: cpp,c++,golf,minify,code-golf,competitive-programming
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Code Generators
Classifier: Topic :: Text Processing :: Filters
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Requires-Dist: libclang
Provides-Extra: dev
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Requires-Dist: pytest; extra == "dev"

# CPPGolf

`cppgolf` is a C/C++ multi-file merge and code-golf/minifier tool.

## Install

```bash
pip install cppgolf
```

`libclang`-backed features such as symbol renaming, type renaming, and static deduplication are optional at runtime. The base CLI and text transforms work without `clang.cindex`.

## CLI

```bash
cppgolf solution.cpp
cppgolf solution.cpp -o golf.cpp
cppgolf solution.cpp --merge-only -I include/
cppgolf solution.cpp -DDEBUG=1 --inject-define --merge-only
cppgolf solution.cpp -I include/ --rename-functions --stats
cppgolf solution.cpp -DBOTZONE_PIKAFISH_STANDALONE=1 -DZSTD_DISABLE_ASM
cppgolf solution.cpp --no-rename
```

### Common options

| Option | Meaning |
| --- | --- |
| `-o FILE` | Write output to a file instead of stdout. |
| `-I DIR` | Add an include search directory. Can be repeated. |
| `-D MACRO[=VALUE]` | Pass a macro definition to libclang-backed optional passes. Can be repeated. |
| `--inject-define` | Write `-D` macro definitions into the generated source code. |
| `--merge-only` | Only merge files. Skip all later transforms and compression passes. |
| `--no-merge` | Skip inlining `#include "..."`. |
| `--no-strip-comments` | Keep comments. |
| `--no-compress-ws` | Keep whitespace formatting. |
| `--no-std-ns` | Do not add `using namespace std;`. |
| `--no-typedefs` | Do not add shortcuts such as `ll` / `ld`. |
| `--no-rename` | Disable symbol renaming. |
| `--no-win-lean` | Do not inject Windows header conflict guards. |
| `--keep-main-return` | Keep the trailing `return 0;` in `main`. |
| `--keep-endl` | Keep `endl`. |
| `--keep-inline` | Keep `inline`. |
| `--dedup-statics` | Deduplicate merged `static` definitions with libclang. |
| `--aggressive` | Remove braces from single-statement `if` / `for` / `while`. |
| `--shortcuts` | Insert `#define` shortcuts for frequent `cout` / `cin`. |
| `--rename-functions` | Also rename user-defined functions and methods. |
| `--rename-type` | Add typedef aliases for long user-defined type names. |
| `--stats` | Print size reduction statistics to stderr. |

## Python API

```python
from pathlib import Path

from cppgolf import process

result, merged_size = process(
    Path("solution.cpp"),
    include_dirs=[],
    defines=["BOTZONE_PIKAFISH_STANDALONE=1"],
    inject_defines=False,
    merge_only=False,
    rename_symbols=False,
)

print(merged_size)
print(result)
```

Individual passes can also be used directly:

```python
from cppgolf import compress_whitespace, strip_comments
from cppgolf.transforms import golf_typedefs

code = Path("a.cpp").read_text()
code = strip_comments(code)
code = golf_typedefs(code)
code = compress_whitespace(code)
```

## Features

- Merge local `#include "..."` files recursively and deduplicate top-level system headers.
- Strip comments while preserving strings, character literals, and raw strings.
- Apply safe default text transforms such as `std::` removal, typedef shortcuts, `endl` replacement, `inline` removal, and trailing `return 0;` removal in `main`.
- Compress whitespace at token granularity while preserving preprocessor line structure.
- Optionally rename symbols and user-defined types with libclang.
