An extensible cyclomatic complexity analyzer for many programming languages including C/C++, Java, JavaScript, Python, Ruby, Swift, and more.
—
Primary analysis functions for processing source code and extracting complexity metrics from files and directories. These functions form the foundation of Lizard's code analysis capabilities.
Analyzes source files in directories or specific paths, providing comprehensive code complexity metrics.
def analyze(paths, exclude_pattern=None, threads=1, exts=None, lans=None):
"""
Main analysis function that processes source files and returns function statistics.
Args:
paths: List of file or directory paths to analyze
exclude_pattern: List of glob patterns to exclude from analysis (optional)
threads: Number of threads for parallel processing (default: 1)
exts: List of extension objects for additional analysis (optional)
lans: List of language names to analyze (optional)
Returns:
Iterator of FileInformation objects containing function statistics
Example:
results = analyze(['src/', 'lib/'], exclude_pattern=['*test*'], threads=4)
for file_info in results:
print(f"{file_info.filename}: {file_info.CCN} complexity")
"""Analyzes a specific list of files rather than searching directories.
def analyze_files(files, threads=1, exts=None):
"""
Analyzes specific files using FileAnalyzer with extension support.
Args:
files: List of file paths to analyze
threads: Number of threads for parallel processing (default: 1)
exts: List of extension objects for additional analysis (optional)
Returns:
Iterator of FileInformation objects
Example:
files = ['app.py', 'utils.py', 'config.py']
results = analyze_files(files, threads=2)
for file_info in results:
for func in file_info.function_list:
print(f"{func.name}: {func.cyclomatic_complexity}")
"""Programmatic access to Lizard's command-line interface with full argument support.
def main(argv=None):
"""
Command-line entry point for Lizard with optional arguments vector.
Args:
argv: Optional list of command-line arguments (default: sys.argv)
If None, uses sys.argv for arguments
Returns:
Exit code (0 for success, non-zero for errors)
Example:
# Analyze Python files with complexity threshold of 10
main(['-l', 'python', '-C', '10', 'src/'])
# Generate XML output
main(['--xml', 'src/'])
# Use extensions
main(['-Ewordcount', '-Eoutside', 'src/'])
"""Creates and configures the command-line argument parser for customization.
def arg_parser(prog=None):
"""
Creates and configures the command-line argument parser.
Args:
prog: Program name for help messages (optional)
Returns:
ArgumentParser object configured with all Lizard options
Example:
parser = arg_parser('my-analyzer')
parser.add_argument('--custom-option', help='Custom option')
args = parser.parse_args()
"""Parses and validates command-line arguments with field name validation.
def parse_args(argv):
"""
Parses command-line arguments and validates field names.
Args:
argv: List of command-line arguments
Returns:
Parsed options object with validated configuration
Raises:
SystemExit: If arguments are invalid or help is requested
Example:
options = parse_args(['-l', 'python', '-C', '15', 'src/'])
print(f"CCN threshold: {options.CCN}")
print(f"Languages: {options.languages}")
"""import lizard
# Analyze all supported files in a directory
results = lizard.analyze(['src/'])
for file_info in results:
print(f"File: {file_info.filename}")
print(f" Lines of code: {file_info.nloc}")
print(f" Cyclomatic complexity: {file_info.CCN}")
for func in file_info.function_list:
if func.cyclomatic_complexity > 10:
print(f" Complex function: {func.name} (CCN: {func.cyclomatic_complexity})")import lizard
# Analyze with multiple threads and exclusion patterns
results = lizard.analyze(
paths=['src/', 'lib/'],
exclude_pattern=['*test*', '*/migrations/*', '*.min.js'],
threads=4,
lans=['python', 'javascript']
)
total_functions = 0
complex_functions = 0
for file_info in results:
total_functions += len(file_info.function_list)
complex_functions += sum(1 for f in file_info.function_list if f.cyclomatic_complexity > 15)
print(f"Total functions: {total_functions}")
print(f"Complex functions (CCN > 15): {complex_functions}")import lizard
import sys
# Capture output and run analysis
original_stdout = sys.stdout
sys.stdout = open('analysis_output.txt', 'w')
try:
# Run with custom options
lizard.main([
'--languages', 'python,javascript',
'--CCN', '10',
'--length', '50',
'--arguments', '5',
'--xml',
'src/'
])
finally:
sys.stdout.close()
sys.stdout = original_stdout
print("Analysis complete. Results saved to analysis_output.txt")Install with Tessl CLI
npx tessl i tessl/pypi-lizard