Skip to content

CodeCommentRemover is a CLI tool that strips source-code comments & blocks from individual files or entire directory trees. It understands both line and block comment syntax, skips binary files automatically, and preserves sequences that appear inside strings or raw string literals. It also respects pre-existing file encodings/editor settings.

License

Notifications You must be signed in to change notification settings

Barrixar/CodeCommentRemover

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CodeCommentRemover

CodeCommentRemover is a CLI tool that strips source-code comments & blocks from individual files or entire directory trees. It understands both line and block comment syntax, skips binary files automatically, and preserves sequences that appear inside strings or raw string literals. It also respects pre-existing file encodings/editor settings.

Author: Barrixar

CodeCommentRemover is available at: https://github.com/Barrixar/CodeCommentRemover

Features

  • Concurrent processing: Parallel file processing using Go 1.25's sync.WaitGroup.Go() for optimal performance
  • Built-in language aliases: Predefined patterns for common languages (cpp, go, shell, doxygen, and more)
  • Quote-aware parsing: Preserves comment-like sequences inside strings, raw strings, and backticks
  • Binary detection: Automatically skips binary files to avoid corruption
  • Informative output: Reports which files were updated and provides summary statistics
  • Windows shell-friendly: Clear documentation for quoting patterns on Windows

Build

Requirements: Go 1.25 or later

Clone the repository and compile:

powershell go build -o CodeCommentRemover.exe CodeCommentRemover.go

This produces CodeCommentRemover.exe (or CodeCommentRemover on Unix-like systems).

Usage

CodeCommentRemover.exe [-workers=N] <pattern> <file-or-directory> CodeCommentRemover.exe [-workers=N] AliasMode <alias> [file-or-directory]

Arguments

  • - A comma-separated list of comment definitions:

    • Line prefixes: //, #, etc.
    • Block patterns: /|/, /**|*/ (use start|end format)
    • Windows users: Quote patterns containing special characters: "/|/"
  • - Target file or folder (processes recursively for directories)

  • AliasMode - Use a predefined language pattern (see Built-in Aliases below)

    • Optional path argument (defaults to current directory if omitted)

Flags

  • -workers=N - Max files to process concurrently (default: GOMAXPROCS)

Built-in Aliases

Aliases are case-insensitive and expand to the patterns shown:

Alias Pattern Description
standard // Single-line comments starting with //
�lock /* */
doxygen //,/** /,/
cpp //,/* */
c //,/* */
csharp //,/* */
go //,/* */
ypescript //,/* */
shell # Shell script comments
hash # Hash-style comments (same as shell)

Examples

Using Aliases (Recommended)

Remove C++ comments from a directory: powershell CodeCommentRemover.exe AliasMode cpp C:\path\to\project

Remove Doxygen comments from current directory: powershell CodeCommentRemover.exe AliasMode doxygen .

Using Direct Patterns

Remove C-style block comments (Windows - note the quotes): powershell CodeCommentRemover.exe "/*|*/" C:\path\to\file.c

Remove both // and # comments: powershell CodeCommentRemover.exe "//,#" C:\path\to\src

Remove line comments only: powershell CodeCommentRemover.exe "//" C:\path\to\project

Output Examples

When files are updated: Updated C:\path\to\file1.cpp Updated C:\path\to\file2.cpp Completed: 2 file(s) updated, 3 file(s) unchanged.

When no changes are needed: No changes needed. Checked 5 file(s), no comments matched the pattern.

Behavior Notes

File Processing

  • Binary files (containing null bytes) are automatically skipped
  • Only files with matching comments are modified
  • File encoding/your editor's settings are universally respected (Not modified). CodeCommentRemover won't mess with it.
  • Line endings (CRLF/LF) are preserved
  • File permissions aren't modified

Nothing can or will go wrong with your file or its properties, but as usual, make a backup just in case.

Comment Detection

  • Comments inside strings ("...", '...', ...) are preserved
  • Escape sequences are handled correctly
  • Shebang lines (#!/...) are always preserved, even when # is targeted

Performance

  • Files are processed concurrently for maximum speed
  • Default concurrency matches GOMAXPROCS (typically CPU count)
  • Use -workers flag to adjust parallelism for I/O-bound workloads

Pattern Behavior

  • Patterns are automatically deduplicated
  • Block patterns are processed before line patterns
  • Partial line comments (trailing //) are trimmed while preserving code

Supported Go Version

This tool requires Go 1.25 or later and uses modern features including:

  • sync.WaitGroup.Go() for cleaner goroutine management
  • strings.SplitSeq() for efficient string iteration
  • slices and maps standard library packages
  • cmp.Or() for value defaulting

Testing

The test suite is still under development and will be added later. The codebase was matured over a course of weeks, and saw a lot of personal use bebfore publishing it. I have accounted for all edge-cases and niche requirements that i could think of, but suggestions and PR's are very much welcome!

Don't be surprised to see low activity after initial publishing, because the codebase is already very polished and stable. I don't tend to skate one night's ice.

License

See LICENSE file for details.

About

CodeCommentRemover is a CLI tool that strips source-code comments & blocks from individual files or entire directory trees. It understands both line and block comment syntax, skips binary files automatically, and preserves sequences that appear inside strings or raw string literals. It also respects pre-existing file encodings/editor settings.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages