Calculate the cyclomatic complexity of the source code.
Current supported languages:
The tool is not published to Pypi, you can install it directly from source code.
pip install git+https://github.com/frite/mccabe.gitYou can use mccabe both as a CLI program and as a module in your program.
> mcc --help
Usage: mcc [OPTIONS]
Calculate the cyclomatic complexity of the source code
Options:
-V, --version Show the version and exit.
-d, --directory TEXT The source code directory
-f, --file TEXT The source code file
-m, --min INTEGER The min threshold value
-l, --language [py|go|js] The source code language [required]
--help Show this message and exit.You can calculate the cyclomatic complexity for a single source file.
> mcc -l py -f tests/test_languages.py
{
"tests/test_languages.py": 9
}You can also calculate the cyclomatic complexity for all files inside a directory, which leverages parallel computing.
> mcc -l py -d tests
{
"tests/test_languages.py": 9,
"tests/__init__.py": 0,
"tests/test_provider.py": 4,
"tests/test_cyclomaticc_complexity.py": 8,
"tests/sources/py.py": 24
}You can set a minimun threshold value by using the -m flag.
> mcc -l py -d tests -m 5
{
"tests/test_languages.py": 9,
"tests/test_cyclomaticc_complexity.py": 8,
"tests/sources/py.py": 24
}Also, you can load it as a module in your programs.
frommccimportget_provider_classfrommcc.languagesimportLangcls=get_provider_class(Lang.py)
ret=cls(file="test.py").run()
print(ret)First, you need to clone the repo.
git clone https://github.com/long2ice/mccabe.gitThen install the requirements.
make depsBefore commiting your code, you should check the code style and syntax.
make style ciIf you want to add other language support by tree-sitter like c++, just follow the steps:
Run
git submodule add https://github.com/tree-sitter/tree-sitter-cpp.git vendor/tree-sitter-cppUpdate
build.pyto addcppsupport.Language.build_library( 'languages.so', [ 'vendor/tree-sitter-go', 'vendor/tree-sitter-javascript', 'vendor/tree-sitter-python', 'vendor/tree-sitter-cpp' ] )
Update
languages.pyaddcppenum.classLang(str, Enum): py="py"go="go"js="js"cpp="cpp"
Create new file
cpp.pyand write a class which inheritMccabe.frommcc.providersimportMccabefrommcc.languagesimportLangclassMccabeCpp(Mccabe): suffix=".cpp"language=Lang.cppjudge_nodes= [...]
That's all! You can now calculate the cyclomatic complexity of cpp source files!
This project is licensed under the Apache-2.0 License.