CLI tool to remove comments from source files.
Project description
Sanitize Comments CLI
A fast command-line tool to recursively remove comments from source code files within a directory. It uses the Pygments library for accurate language detection and comment tokenization, making it significantly more robust than simple regex solutions.
The goal is to help clean up codebases by removing potentially distracting or outdated comments, especially before distribution or analysis, while minimizing the risk of corrupting the actual code.
⚠️ WARNING: USE WITH EXTREME CAUTION! ⚠️
This tool modifies your source files directly!
- Potential for Errors: While
Pygmentsis robust, extremely complex or non-standard code syntax might confuse the tokenizer in rare edge cases, potentially leading to incorrect modifications.- Version Control Essential: ALWAYS use this tool on projects managed with version control (like Git). Commit or stash your changes before running
sanitize.- Review Changes: Carefully review the changes made by the tool using
git diffor similar before committing the results.- Use Backups: The
--backupflag is highly recommended. It creates backups of modified files in a.sanitize_backupsdirectory.- Dry Run First: Always perform a
--dry-runfirst to see which files would be modified without actually changing them.- Skipped Files: Files for which
Pygmentsdoes not have a suitable lexer (tokenizer) will be skipped automatically to avoid errors.
Features
- Removes single-line (
#,//, etc.) and multi-line (/* ... */, etc.) comments. - Uses
Pygmentsfor language detection and accurate tokenization. - Handles comment markers safely, even when they appear inside strings or other code constructs.
- Preserves shebang lines (
#!/usr/bin/env python3). - Recursively processes files in a directory.
- Supports a wide range of languages (any language known to Pygments).
- Configurable file extensions and exclusions.
- Optional backups of original files in a dedicated
.sanitize_backupsdirectory. - Dry run mode to preview changes.
Installation
Requires Python 3.8+ and pipx.
pipx install sanitize-comments-cli
# Show help message and all options
sanitize --help
# 1. Recommended: Dry run to see potential changes (verbose output)
sanitize --dry-run --verbose .
# 2. Run with backups (creates backups in ./.sanitize_backups/)
sanitize --backup .
# 3. Review changes with Git BEFORE committing
git status
git diff path/to/changed/file.js # Review individual files
# --- Common Options ---
# Specify a different target directory
sanitize --backup ./src
# Process only specific file types (e.g., Python and JavaScript)
sanitize -e .py .js --backup .
# Exclude specific directories (e.g., build artifacts, vendor libs)
# Note: Common ones like .git, node_modules, venv are excluded by default
sanitize --exclude-dirs build dist generated --backup .
# Exclude specific files
sanitize --exclude-files config.js legacy_code.py --backup .
# Run without backups (Use ONLY if you have reliable version control!)
# sanitize .
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file sanitize_comments_cli-0.1.0.tar.gz.
File metadata
- Download URL: sanitize_comments_cli-0.1.0.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ef349f602a1c5af160336ae3633ad30989fc11288bebbb285ab66e9d7da7852
|
|
| MD5 |
17511250ae4dc51e2e0b1d1b6afdeb3b
|
|
| BLAKE2b-256 |
628608aac102939f1cff14e64263a2f93c723361ae88aa4e2036934f63e9877d
|
File details
Details for the file sanitize_comments_cli-0.1.0-py3-none-any.whl.
File metadata
- Download URL: sanitize_comments_cli-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdd877d2e82d28afe23e6e6557b62eadb0c627f714ad47df3adb9a7d8fb5f1bc
|
|
| MD5 |
010613f897209e884c1926f99ff758ce
|
|
| BLAKE2b-256 |
84756117d5b28669b661fcb64fcf3aaef4413d0e98d64814f95c45b3f92779e8
|