A lightweight Python tool to calculate the Cyclomatic Complexity of functions in your Python source code. It uses the ast module to parse code and identify decision points, helping you identify complex functions that may need refactoring.
- AST-based Analysis: Doesn't run your code; safely parses the Abstract Syntax Tree.
- Function-level Metrics: Provides complexity scores for every function and method.
- Configurable Thresholds: Flag functions that exceed a specific complexity limit.
- CI Friendly: Simple CLI interface and exit-code behavior (can be extended).
No dependencies outside the Python standard library are required.
git clone https://github.com/gbvk312/complexity-analyzer-cli.git
cd complexity-analyzer-cliAnalyze a single file:
python complexity_analyzer.py your_file.pyAnalyze multiple files with a custom threshold:
python complexity_analyzer.py file1.py file2.py --threshold 15Analyzing: example.py
Function Name Line Complexity
--------------------------------------------------
get_user_data 10 3
process_payments 25 12 [!] High Complexity
Cyclomatic complexity is a quantitative measure of the number of linearly independent paths through a program's source code.
- 1-10: Simple, easy to test.
- 11-20: More complex, moderate risk.
- 21-50: Complex, high risk.
- >50: Untestable, very high risk.
- Support for directory-wide recursive analysis.
- Export results to JSON/CSV.
- Integration with pre-commit hooks.
- Support for JavaScript/TypeScript (requires additional parser).
This project is licensed under the MIT License - see the LICENSE file for details.