I am looking for a command-line tool that removes all comments from an input file and returns the stripped output. It'd be nice it supports popular programming languages like c, c++, python, php, javascript, html, css, etc. It has to be syntax-aware as opposed to regexp-based, since the latter will catch the pattern in source code strings as well. Is there any such tool?
I am fully aware that comments are useful information and often leaving them as they are is a good idea. It's just that my focus is on different use cases.
We will parse the string line by line as an ideal compiler does. We will ignore all the characters between and after these block quotes when we encounter// or '/* /*. ' A function removeString(vector<string>&source) takes a source code as an input and returns the code after removing its comments.
cloc
, a free Perl script, can do this.
Remove Comments from Source Code
How can you tell if cloc correctly identifies comments? One way to convince yourself cloc is doing the right thing is to use its
--strip-comments
option to remove comments and blank lines from files, then compare the stripped-down files to originals.
It supports a lot of languages.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With