1 unstable release
| 0.1.3 | Nov 6, 2025 |
|---|
#692 in Web programming
39KB
914 lines
Code Comment Cleaning Tool (Comments Cleaner)
A powerful command-line tool for automatically removing comments from code in multiple programming languages.
Features
- 🚀 Multi-language support: C/C++, Java, JavaScript, TypeScript, Python, HTML, CSS, PHP, Rust, Basic
- 🔍 Auto-detection: Automatically recognizes programming language based on file extension
- 📁 Batch processing: Supports single file and recursive directory processing
- 💾 Flexible output: Supports in-place modification, specified output path, automatic backup
- 🎯 Smart parsing: Correctly handles comment symbols in strings, won't mistakenly delete
- ✨ Whitespace optimization: Automatically merges excessive consecutive empty lines to keep code clean
- 🎨 Friendly interface: Colored output with detailed processing information
Supported Programming Languages
| Language | File Extensions | Comment Types |
|---|---|---|
| C | .c, .h |
// and /* */ |
| C++ | .cpp, .cc, .cxx, .hpp, .hxx |
// and /* */ |
| Java | .java |
// and /* */ |
| JavaScript | .js, .jsx |
// and /* */ |
| TypeScript | .ts, .tsx |
// and /* */ |
| Python | .py, .pyw |
# and """...""" / '''...''' |
| HTML | .html, .htm |
<!-- --> |
| CSS | .css |
/* */ |
| PHP | .php |
//, # and /* */ |
| Rust | .rs |
// and /* */ |
| Basic | .vb, .bas, .vba, .vbs |
' and REM |
Installation
Download pre-built binaries
You can download the latest pre-built executable from the Releases page.
- Go to the Releases page
- Download the appropriate binary for your operating system
- Extract the executable and add it to your system PATH (optional)
Build from source
git clone https://github.com/cyber-tao/comments-cleaner.git
cd comments-cleaner
cargo build --release
The compiled executable will be located at target/release/cclean.exe (Windows) or target/release/cclean (Linux/macOS)
Install to system
cargo install --path .
Usage
Basic usage
cclean <file or directory path> [options]
Common examples
1. Process single file (generate new file)
cclean src/main.cpp
Output: src/main_cleaned.cpp
2. Modify file in-place
cclean src/main.cpp -i
3. Specify output file
cclean src/main.cpp -o output/main.cpp
4. Create backup and modify in-place
cclean src/main.cpp -i -b
Output: Modify original file, create src/main.cpp.bak
5. Recursive directory processing
cclean src/ -r -i
6. Process directory and output to specified directory
cclean src/ -r -o cleaned/
7. Process only specific extension files
cclean src/ -r -e "cpp,h,hpp"
8. Manually specify programming language
cclean script.txt -l cpp -o script_cleaned.txt
9. Dry run (no actual modification)
cclean src/ -r --dry-run
Command-line Options
| Option | Short | Description |
|---|---|---|
--output <PATH> |
-o |
Specify output file or directory path |
--recursive |
-r |
Recursively process all files in directory |
--in-place |
-i |
Modify original file directly |
--backup |
-b |
Create backup file (.bak) |
--lang <LANGUAGE> |
-l |
Manually specify programming language |
--dry-run |
Dry run, do not actually modify files | |
--extensions <EXT> |
-e |
Specify file extensions to process (comma-separated) |
--help |
-h |
Show help information |
--version |
-V |
Show version information |
Supported Language Identifiers
When using -l or --lang option, you can use the following language identifiers:
c: C languagecpporc++: C++java: Javajsorjavascript: JavaScripttsortypescript: TypeScriptpythonorpy: Pythonhtml: HTMLcss: CSSphp: PHPrustorrs: Rustbasic,vb,vba, orvbs: Basic
Important Notes
- String safety: The tool can correctly identify comment symbols in strings and won't mistakenly delete them
- Backup recommendation: When processing important files, it's recommended to use the
-boption to create backups - Testing recommendation: For first-time use, it's recommended to run with
--dry-runfirst - Encoding support: Currently supports UTF-8 encoded files
Example Scenarios
Scenario 1: Clean comments for entire project
cclean ./project -r -o ./project_cleaned
Scenario 2: Clean code before release
cclean ./src -r -i -b
This will:
- Recursively process src directory
- Modify all files in-place
- Create .bak backup for each file
- Show detailed processing information
Scenario 3: Clean only JavaScript files
cclean ./src -r -e "js,jsx" -o ./dist
Technical Details
Comment Processing Rules
- C/C++/Java/JS/TS/Rust: Remove
//single-line comments and/* */multi-line comments - Python: Remove
#comments and triple-quoted strings"""..."""/'''...'''(including docstrings, replaced with empty string"") - HTML: Remove
<!-- -->comments, also process comments in embedded<script>and<style>tags - CSS: Remove
/* */comments - PHP: Remove
//single-line comments,#comments, and/* */multi-line comments - Basic: Remove
'single-line comments andREMkeyword comments
Whitespace Handling
After removing comments, the tool will automatically:
- Merge multiple consecutive empty lines into a single empty line
- Keep code structure clear
- Reduce file size
Smart Parsing
The tool uses state machine parsing to correctly handle:
- Comment symbols in strings (won't be deleted)
- Escape characters
- Multi-line strings
- Nested structures
Development
Run tests
cargo test
Development mode run
cargo run -- <arguments>
Example:
cargo run -- test.cpp
License
MIT License
Contributing
Issues and Pull Requests are welcome!
Dependencies
~3–7MB
~122K SLoC