0
# Core Analysis Functions
1
2
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.
3
4
## Capabilities
5
6
### Main Analysis Function
7
8
Analyzes source files in directories or specific paths, providing comprehensive code complexity metrics.
9
10
```python { .api }
11
def analyze(paths, exclude_pattern=None, threads=1, exts=None, lans=None):
12
"""
13
Main analysis function that processes source files and returns function statistics.
14
15
Args:
16
paths: List of file or directory paths to analyze
17
exclude_pattern: List of glob patterns to exclude from analysis (optional)
18
threads: Number of threads for parallel processing (default: 1)
19
exts: List of extension objects for additional analysis (optional)
20
lans: List of language names to analyze (optional)
21
22
Returns:
23
Iterator of FileInformation objects containing function statistics
24
25
Example:
26
results = analyze(['src/', 'lib/'], exclude_pattern=['*test*'], threads=4)
27
for file_info in results:
28
print(f"{file_info.filename}: {file_info.CCN} complexity")
29
"""
30
```
31
32
### File-Specific Analysis
33
34
Analyzes a specific list of files rather than searching directories.
35
36
```python { .api }
37
def analyze_files(files, threads=1, exts=None):
38
"""
39
Analyzes specific files using FileAnalyzer with extension support.
40
41
Args:
42
files: List of file paths to analyze
43
threads: Number of threads for parallel processing (default: 1)
44
exts: List of extension objects for additional analysis (optional)
45
46
Returns:
47
Iterator of FileInformation objects
48
49
Example:
50
files = ['app.py', 'utils.py', 'config.py']
51
results = analyze_files(files, threads=2)
52
for file_info in results:
53
for func in file_info.function_list:
54
print(f"{func.name}: {func.cyclomatic_complexity}")
55
"""
56
```
57
58
### Command-Line Interface
59
60
Programmatic access to Lizard's command-line interface with full argument support.
61
62
```python { .api }
63
def main(argv=None):
64
"""
65
Command-line entry point for Lizard with optional arguments vector.
66
67
Args:
68
argv: Optional list of command-line arguments (default: sys.argv)
69
If None, uses sys.argv for arguments
70
71
Returns:
72
Exit code (0 for success, non-zero for errors)
73
74
Example:
75
# Analyze Python files with complexity threshold of 10
76
main(['-l', 'python', '-C', '10', 'src/'])
77
78
# Generate XML output
79
main(['--xml', 'src/'])
80
81
# Use extensions
82
main(['-Ewordcount', '-Eoutside', 'src/'])
83
"""
84
```
85
86
### Argument Parsing
87
88
Creates and configures the command-line argument parser for customization.
89
90
```python { .api }
91
def arg_parser(prog=None):
92
"""
93
Creates and configures the command-line argument parser.
94
95
Args:
96
prog: Program name for help messages (optional)
97
98
Returns:
99
ArgumentParser object configured with all Lizard options
100
101
Example:
102
parser = arg_parser('my-analyzer')
103
parser.add_argument('--custom-option', help='Custom option')
104
args = parser.parse_args()
105
"""
106
```
107
108
### Arguments Processing
109
110
Parses and validates command-line arguments with field name validation.
111
112
```python { .api }
113
def parse_args(argv):
114
"""
115
Parses command-line arguments and validates field names.
116
117
Args:
118
argv: List of command-line arguments
119
120
Returns:
121
Parsed options object with validated configuration
122
123
Raises:
124
SystemExit: If arguments are invalid or help is requested
125
126
Example:
127
options = parse_args(['-l', 'python', '-C', '15', 'src/'])
128
print(f"CCN threshold: {options.CCN}")
129
print(f"Languages: {options.languages}")
130
"""
131
```
132
133
## Usage Examples
134
135
### Basic Directory Analysis
136
137
```python
138
import lizard
139
140
# Analyze all supported files in a directory
141
results = lizard.analyze(['src/'])
142
for file_info in results:
143
print(f"File: {file_info.filename}")
144
print(f" Lines of code: {file_info.nloc}")
145
print(f" Cyclomatic complexity: {file_info.CCN}")
146
147
for func in file_info.function_list:
148
if func.cyclomatic_complexity > 10:
149
print(f" Complex function: {func.name} (CCN: {func.cyclomatic_complexity})")
150
```
151
152
### Multi-threaded Analysis with Filtering
153
154
```python
155
import lizard
156
157
# Analyze with multiple threads and exclusion patterns
158
results = lizard.analyze(
159
paths=['src/', 'lib/'],
160
exclude_pattern=['*test*', '*/migrations/*', '*.min.js'],
161
threads=4,
162
lans=['python', 'javascript']
163
)
164
165
total_functions = 0
166
complex_functions = 0
167
168
for file_info in results:
169
total_functions += len(file_info.function_list)
170
complex_functions += sum(1 for f in file_info.function_list if f.cyclomatic_complexity > 15)
171
172
print(f"Total functions: {total_functions}")
173
print(f"Complex functions (CCN > 15): {complex_functions}")
174
```
175
176
### Programmatic CLI Usage
177
178
```python
179
import lizard
180
import sys
181
182
# Capture output and run analysis
183
original_stdout = sys.stdout
184
sys.stdout = open('analysis_output.txt', 'w')
185
186
try:
187
# Run with custom options
188
lizard.main([
189
'--languages', 'python,javascript',
190
'--CCN', '10',
191
'--length', '50',
192
'--arguments', '5',
193
'--xml',
194
'src/'
195
])
196
finally:
197
sys.stdout.close()
198
sys.stdout = original_stdout
199
200
print("Analysis complete. Results saved to analysis_output.txt")
201
```