comments-cleaner

Code Comment Cleaning Tool (Comments Cleaner)

GitHub stars GitHub forks License: MIT Crates.io Crates.io downloads CI Rust

A powerful command-line tool for automatically removing comments from code in multiple programming languages.

Features

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

The easiest way to install comments-cleaner is using Cargo:

cargo install comments-cleaner

This will download, compile, and install the latest version from crates.io. Make sure you have Rust installed on your system.

Download pre-built binaries

You can download the latest pre-built executable from the Releases page.

  1. Go to the Releases page
  2. Download the appropriate binary for your operating system
  3. 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:

Important Notes

  1. String safety: The tool can correctly identify comment symbols in strings and won’t mistakenly delete them
  2. Backup recommendation: When processing important files, it’s recommended to use the -b option to create backups
  3. Testing recommendation: For first-time use, it’s recommended to run with --dry-run first
  4. 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:

Scenario 3: Clean only JavaScript files

cclean ./src -r -e "js,jsx" -o ./dist

Technical Details

Comment Processing Rules

Whitespace Handling

After removing comments, the tool will automatically:

Smart Parsing

The tool uses state machine parsing to correctly handle:

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!