1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/// CLI argument definitions for the `km` command.
///
/// Defines all subcommands and their arguments using the `clap` derive macros.
/// Long help text is stored in `cli_help.rs` to keep this file focused on structure.
use std::path::PathBuf;
use clap::{Args, Parser, Subcommand};
use crate::cli_help;
/// Top-level CLI parser with a single subcommand selector.
#[derive(Parser)]
#[command(name = "km", version, about = "Kimün — code metrics tools")]
pub struct Cli {
#[command(subcommand)]
pub command: Commands,
}
/// Common arguments shared by most analysis commands.
#[derive(Args)]
pub struct CommonArgs {
/// Directory to analyze (default: current directory)
pub path: Option<PathBuf>,
/// Output as JSON
#[arg(long)]
pub json: bool,
/// Include test files and directories in analysis (excluded by default)
#[arg(long)]
pub include_tests: bool,
}
/// All available analysis subcommands.
#[derive(Subcommand)]
pub enum Commands {
/// Count lines of code (blank, comment, code) by language
Loc {
#[command(flatten)]
common: CommonArgs,
/// Show summary stats (files read, unique, ignored, elapsed time)
#[arg(short, long)]
verbose: bool,
},
/// Detect duplicate code across files
Dups {
#[command(flatten)]
common: CommonArgs,
/// Show detailed report with duplicate locations
#[arg(short, long)]
report: bool,
/// Show all duplicate groups (default: top 20)
#[arg(long)]
show_all: bool,
/// Minimum lines for a duplicate block (default: 6)
#[arg(long, default_value = "6")]
min_lines: usize,
},
/// Analyze indentation complexity (stddev and max depth per file)
Indent {
#[command(flatten)]
common: CommonArgs,
},
/// Analyze Halstead complexity metrics per file
#[command(long_about = cli_help::HAL)]
Hal {
#[command(flatten)]
common: CommonArgs,
/// Show only the top N files (default: 20)
#[arg(long, default_value = "20")]
top: usize,
/// Sort by metric: effort, volume, or bugs (default: effort)
#[arg(long, default_value = "effort", value_parser = ["effort", "volume", "bugs"])]
sort_by: String,
},
/// Analyze cyclomatic complexity per file and per function
Cycom {
#[command(flatten)]
common: CommonArgs,
/// Minimum max-complexity to include a file (default: 1)
#[arg(long, default_value = "1")]
min_complexity: usize,
/// Show only the top N files (default: 20)
#[arg(long, default_value = "20")]
top: usize,
/// Show per-function breakdown
#[arg(long)]
per_function: bool,
/// Sort by metric: total, max, or avg (default: total)
#[arg(long, default_value = "total", value_parser = ["total", "max", "avg"])]
sort_by: String,
},
/// Analyze cognitive complexity per file and per function (SonarSource method)
#[command(long_about = cli_help::COGCOM)]
Cogcom {
#[command(flatten)]
common: CommonArgs,
/// Minimum max-complexity to include a file (default: 1)
#[arg(long, default_value = "1")]
min_complexity: usize,
/// Show only the top N files (default: 20)
#[arg(long, default_value = "20")]
top: usize,
/// Show per-function breakdown
#[arg(long)]
per_function: bool,
/// Sort by metric: total, max, or avg (default: total)
#[arg(long, default_value = "total", value_parser = ["total", "max", "avg"])]
sort_by: String,
},
/// Compute Maintainability Index per file (Visual Studio variant, 0-100 scale)
#[command(long_about = cli_help::MI)]
Mi {
#[command(flatten)]
common: CommonArgs,
/// Show only the top N files (default: 20)
#[arg(long, default_value = "20")]
top: usize,
/// Sort by metric: mi, volume, complexity, or loc (default: mi)
#[arg(long, default_value = "mi", value_parser = ["mi", "volume", "complexity", "loc"])]
sort_by: String,
},
/// Generate a comprehensive report combining all code metrics
Report {
#[command(flatten)]
common: CommonArgs,
/// Show only the top N files per section (default: 20)
#[arg(long, default_value = "20")]
top: usize,
/// Minimum lines for a duplicate block (default: 6)
#[arg(long, default_value = "6")]
min_lines: usize,
/// Show all files instead of truncating to top N
#[arg(long)]
full: bool,
},
/// Compute Maintainability Index per file (verifysoft variant, with comment weight)
#[command(long_about = cli_help::MIV)]
Miv {
#[command(flatten)]
common: CommonArgs,
/// Show only the top N files (default: 20)
#[arg(long, default_value = "20")]
top: usize,
/// Sort by metric: mi, volume, complexity, or loc (default: mi)
#[arg(long, default_value = "mi", value_parser = ["mi", "volume", "complexity", "loc"])]
sort_by: String,
},
/// Find hotspots: files that change frequently and have high complexity
#[command(long_about = cli_help::HOTSPOTS)]
Hotspots {
#[command(flatten)]
common: CommonArgs,
/// Show only the top N files (default: 20)
#[arg(long, default_value = "20")]
top: usize,
/// Sort by metric: score, commits, or complexity (default: score)
#[arg(long, default_value = "score", value_parser = ["score", "commits", "complexity"])]
sort_by: String,
/// Only consider commits since this time (e.g. 6m, 1y, 30d)
#[arg(long)]
since: Option<String>,
/// Complexity metric: indent (default, Thornhill), cycom (cyclomatic), or cogcom (cognitive)
#[arg(long, default_value = "indent", value_parser = ["indent", "cycom", "cogcom"])]
complexity: String,
},
/// Analyze code ownership patterns via git blame (knowledge maps)
#[command(long_about = cli_help::KNOWLEDGE)]
Knowledge {
#[command(flatten)]
common: CommonArgs,
/// Show only the top N files (default: 20)
#[arg(long, default_value = "20")]
top: usize,
/// Sort by: concentration, diffusion, or risk (default: concentration)
#[arg(long, default_value = "concentration", value_parser = ["concentration", "diffusion", "risk"])]
sort_by: String,
/// Only consider recent activity since this time for knowledge loss detection (e.g. 6m, 1y, 30d)
#[arg(long)]
since: Option<String>,
/// Show only files with knowledge loss risk (primary owner inactive)
#[arg(long)]
risk_only: bool,
},
/// Analyze temporal coupling: files that change together in commits
#[command(long_about = cli_help::TC)]
Tc {
#[command(flatten)]
common: CommonArgs,
/// Show only the top N file pairs (default: 20)
#[arg(long, default_value = "20")]
top: usize,
/// Sort by: strength or shared (default: strength)
#[arg(long, default_value = "strength", value_parser = ["strength", "shared"])]
sort_by: String,
/// Only consider commits since this time (e.g. 6m, 1y, 30d)
#[arg(long)]
since: Option<String>,
/// Minimum commits per file to be included (default: 3)
#[arg(long, default_value = "3")]
min_degree: usize,
/// Filter results: show only pairs with strength >= threshold (e.g. 0.5 for strong coupling only)
#[arg(long)]
min_strength: Option<f64>,
},
/// Compute an overall code health score for the project (A++ to F--)
#[command(long_about = cli_help::SCORE)]
Score {
#[command(subcommand)]
subcommand: Option<ScoreCommands>,
#[command(flatten)]
common: CommonArgs,
/// Number of worst files to show in "needs attention" (default: 10)
#[arg(long, default_value = "10")]
bottom: usize,
/// Minimum lines for a duplicate block (default: 6)
#[arg(long, default_value = "6")]
min_lines: usize,
/// Scoring model: cogcom (default, v0.14+) or legacy (MI + cyclomatic, v0.13)
#[arg(long, default_value = "cogcom", value_parser = ["cogcom", "legacy"])]
model: String,
},
/// AI-powered code analysis and tooling
Ai {
#[command(subcommand)]
command: AiCommands,
},
}
/// Score subcommands (diff).
#[derive(Subcommand)]
pub enum ScoreCommands {
/// Compare the current code health score against a git ref
#[command(long_about = cli_help::SCORE_DIFF)]
Diff {
/// Git ref to compare against (default: HEAD)
#[arg(long, value_name = "REF", default_value = "HEAD")]
git_ref: String,
/// Directory to analyze (default: current directory)
path: Option<PathBuf>,
/// Output as JSON
#[arg(long)]
json: bool,
/// Include test files and directories in analysis (excluded by default)
#[arg(long)]
include_tests: bool,
/// Number of worst files to show in "needs attention" (default: 10)
#[arg(long, default_value = "10")]
bottom: usize,
/// Minimum lines for a duplicate block (default: 6)
#[arg(long, default_value = "6")]
min_lines: usize,
/// Scoring model: cogcom (default, v0.14+) or legacy (MI + cyclomatic, v0.13)
#[arg(long, default_value = "cogcom", value_parser = ["cogcom", "legacy"])]
model: String,
},
}
/// AI-powered analysis subcommands (analyze, skill install).
#[derive(Subcommand)]
pub enum AiCommands {
/// Analyze repository using an AI provider
#[command(long_about = cli_help::AI_ANALYZE)]
Analyze {
/// AI provider to use (e.g. claude)
provider: String,
/// Directory to analyze (default: current directory)
path: Option<PathBuf>,
/// Model to use (default: claude-sonnet-4-5-20250929)
#[arg(long)]
model: Option<String>,
/// Save the report to a file
#[arg(short, long)]
output: Option<PathBuf>,
},
/// Install a Claude Code skill for km
#[command(long_about = cli_help::AI_SKILL)]
Skill {
/// Provider for the skill (e.g. claude)
provider: String,
},
}