Code Line Counter
Code Line Counting Tool
Repo
Code Line Counting Tool: README
This tool counts the number of code lines in a repository, with options to ignore specific folders and filter by file extensions. It logs detailed results to both the console and a log file.
Installation
- Clone the repository:
1
2git clone https://github.com/xiyuanyang-code/Repo-Code-Line-Counting.git
cd Repo-Code-Line-Counting - Install the package (preferably in a virtual environment):For development, you can install in editable mode:
1
pip install .
1
pip install -e .
Usage
Command Line Interface
After installation, you can use the count-code
command directly in your terminal.
1 |
|
Options:
--path PATH
: The path to the repository to be counted. Defaults to the current working directory (.
).--file_type FILE_TYPE [FILE_TYPE ...]
: List of file extensions to include (e.g.,.py .cpp .h
). If not provided, all file types are counted.--ignored_dir IGNORED_DIR [IGNORED_DIR ...]
: List of subdirectory names to ignore. Defaults to common ignore directories (.git
,.history
,.vscode
,build
,__pycache__
,log
).--log_level {DEBUG,INFO,WARNING,ERROR,CRITICAL,NOTICE}
: Set the logging level for console output. Defaults toNOTICE
.
Examples:
Count lines in the current directory (using default ignored directories and counting all file types):
1 |
|
Count only .cpp
and .hpp
files in a specific repository:
1 |
|
Count lines ignoring the docs
and tests
directories:
1 |
|
Run with DEBUG logging level:
1 |
|
As a Python Module
You can also import and use the count_code_lines
function directly in your Python code.
1 |
|
Log Output
Detailed logs, including file-by-file line counts and any errors encountered, are saved to a file in the log/
directory (e.g., log/code-counting-YYYYMMDD_HH-MM-SS.log
). Summary results are also printed to the console based on the specified log level.
Todo
-
Add support for file matching using regular expressions.
-
Add some test.
Code
cli.py
: usingargparse
to accept user’s input. In simplest cases, user will only inputcount-code
and this program will count all lines of code in current directory!
1 |
|
counter.py
: the main component of this section.
1 |
|