tldr-cli 0.4.0

CLI binary for TLDR code analysis tool
Documentation
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
//! Health command - Comprehensive code health dashboard
//!
//! Aggregates multiple sub-analyzers into a unified health report:
//! - Complexity analysis (cyclomatic complexity, hotspots)
//! - Cohesion analysis (LCOM4 class cohesion)
//! - Dead code detection (unreachable functions)
//! - Martin metrics (package coupling: Ca, Ce, I, A, D)
//! - Coupling analysis (pairwise module coupling, full mode)
//! - Similarity analysis (function clone detection, full mode)
//!
//! # Premortem Mitigations
//! - T20: value_parser for --detail validation
//! - T21: All health errors map to exit code 2
//! - T23: Validate --quick + --detail=coupling/similar conflict

use std::path::PathBuf;

use anyhow::Result;
use clap::{Args, ValueEnum};

use tldr_core::quality::health::{run_health, HealthOptions, HealthReport};
use tldr_core::quality::ThresholdPreset;
use tldr_core::Language;

use crate::output::{OutputFormat, OutputWriter};

/// Comprehensive code health analysis
#[derive(Debug, Args)]
pub struct HealthArgs {
    /// Path to analyze (file or directory)
    #[arg(default_value = ".")]
    pub path: PathBuf,

    /// Show detailed sub-analyzer output
    ///
    /// Valid values: complexity, cohesion, dead_code, martin, coupling, similarity, all
    #[arg(long, value_parser = detail_parser)]
    pub detail: Option<String>,

    /// Quick mode (skip coupling and similarity - faster)
    #[arg(long)]
    pub quick: bool,

    /// Threshold preset (strict, default, relaxed)
    #[arg(long, value_enum, default_value = "default")]
    pub preset: PresetArg,

    /// Maximum items to return for coupling and similarity analyses (default: 50)
    #[arg(long, default_value = "50")]
    pub max_items: usize,

    /// Summary mode - omit detail arrays, only include summary metrics
    #[arg(long)]
    pub summary: bool,
}

/// Threshold preset for CLI (mirrors ThresholdPreset)
#[derive(Debug, Clone, Copy, Default, ValueEnum)]
pub enum PresetArg {
    /// Strict thresholds for high-quality codebases
    Strict,
    /// Default thresholds (recommended)
    #[default]
    Default,
    /// Relaxed thresholds for legacy code
    Relaxed,
}

impl From<PresetArg> for ThresholdPreset {
    fn from(arg: PresetArg) -> Self {
        match arg {
            PresetArg::Strict => ThresholdPreset::Strict,
            PresetArg::Default => ThresholdPreset::Default,
            PresetArg::Relaxed => ThresholdPreset::Relaxed,
        }
    }
}

/// T20 Mitigation: value_parser for --detail flag
fn detail_parser(s: &str) -> Result<String, String> {
    let valid = [
        "complexity",
        "cohesion",
        "dead_code",
        "martin",
        "coupling",
        "similarity",
        "all",
    ];
    if valid.contains(&s) {
        Ok(s.to_string())
    } else {
        Err(format!(
            "Invalid detail value '{}'. Valid values: {}",
            s,
            valid.join(", ")
        ))
    }
}

impl HealthArgs {
    /// Validate CLI arguments (T23: check --quick + --detail conflict)
    fn validate(&self) -> Result<()> {
        // T23 Mitigation: --quick + --detail=coupling/similar conflict
        if self.quick {
            if let Some(ref detail) = self.detail {
                if detail == "coupling" || detail == "similarity" {
                    anyhow::bail!(
                        "--detail={} requires full mode. Remove --quick flag to analyze {}.",
                        detail,
                        detail
                    );
                }
            }
        }
        Ok(())
    }

    /// Run the health command
    ///
    /// `lang` is passed from the global CLI `--lang` / `-l` flag (already parsed as `Language` enum).
    pub fn run(&self, format: OutputFormat, quiet: bool, lang: Option<Language>) -> Result<()> {
        // Validate arguments first (T23)
        self.validate()?;

        let writer = OutputWriter::new(format, quiet);

        // Validate path exists
        if !self.path.exists() {
            anyhow::bail!("Path not found: {}", self.path.display());
        }

        // schema-cleanup-v2 (P2.BUG-10): empty/no-source-file directories
        // are a valid edge case (e.g. a fresh `mktemp -d` or a tree with
        // only docs/configs), not a real error. Pre-fix the autodetect
        // path raised `TldrError::NoSupportedFiles` → exit code 23,
        // breaking parity with `structure` (which already returns exit 0
        // + a `warnings` field for the same input). Now we short-circuit
        // when the user did not pass `--lang` AND the directory contains
        // no analyzable files, returning a stub report consistent with
        // `structure`.
        if lang.is_none()
            && self.path.is_dir()
            && Language::from_directory(&self.path).is_none()
        {
            let stub = serde_json::json!({
                "wrapper": "health",
                "root": self.path.display().to_string(),
                "language": null,
                "quick_mode": self.quick,
                "summary": serde_json::Value::Null,
                "details": {},
                "warnings": ["Empty directory: no source files to analyze"],
            });
            if writer.is_text() {
                writer.write_text(&format!(
                    "Health Report: {} (no source files found)",
                    self.path.display()
                ))?;
            } else {
                writer.write(&stub)?;
            }
            return Ok(());
        }

        writer.progress(&format!(
            "Analyzing code health in {}{}...",
            self.path.display(),
            if self.quick { " (quick mode)" } else { "" }
        ));

        // Language comes from global CLI flag (already parsed)
        let language = lang;

        // Build options
        let mut options = HealthOptions {
            quick: self.quick,
            preset: self.preset.into(),
            max_items: self.max_items,
            summary: self.summary,
            ..HealthOptions::with_preset(self.preset.into())
        };
        options.max_items = self.max_items;
        options.summary = self.summary;

        // Run health analysis
        let report = run_health(&self.path, language, options)?;

        // Output based on format, --detail flag, and --summary flag
        if self.summary && self.detail.is_none() {
            // Summary mode: only output summary metrics
            output_summary(&writer, &report, format)?;
        } else {
            output_report(&writer, &report, format, self.detail.as_deref())?;
        }

        Ok(())
    }
}

/// Parse language string to Language enum (T27: error with suggestions)
#[allow(dead_code)]
fn parse_language(lang: &str) -> Result<Language> {
    match lang.to_lowercase().as_str() {
        "python" | "py" => Ok(Language::Python),
        "typescript" | "ts" => Ok(Language::TypeScript),
        "javascript" | "js" => Ok(Language::JavaScript),
        "rust" | "rs" => Ok(Language::Rust),
        "go" => Ok(Language::Go),
        "java" => Ok(Language::Java),
        "c" => Ok(Language::C),
        "cpp" | "c++" => Ok(Language::Cpp),
        "ruby" | "rb" => Ok(Language::Ruby),
        "php" => Ok(Language::Php),
        "swift" => Ok(Language::Swift),
        "kotlin" | "kt" => Ok(Language::Kotlin),
        "scala" => Ok(Language::Scala),
        "csharp" | "cs" | "c#" => Ok(Language::CSharp),
        "lua" => Ok(Language::Lua),
        "luau" => Ok(Language::Luau),
        "elixir" | "ex" => Ok(Language::Elixir),
        "ocaml" | "ml" => Ok(Language::Ocaml),
        _ => anyhow::bail!(
            "Unsupported language: '{}'. Supported: python, typescript, javascript, rust, go, java, c, cpp, ruby, php, swift, kotlin, scala, csharp, lua, luau, elixir, ocaml",
            lang
        ),
    }
}

/// Output the health report based on format and detail flag
fn output_report(
    writer: &OutputWriter,
    report: &HealthReport,
    _format: OutputFormat,
    detail: Option<&str>,
) -> Result<()> {
    match detail {
        Some("all") => {
            // Output entire report
            if writer.is_text() {
                writer.write_text(&report.to_text())?;
            } else {
                writer.write(report)?;
            }
        }
        Some(sub_name) => {
            // Output only the specified sub-analysis
            if let Some(details) = report.detail(sub_name) {
                if writer.is_text() {
                    // For text, show a formatted version of the sub-analysis
                    let text = format!(
                        "{} Analysis\n{}\n{}",
                        sub_name,
                        "=".repeat(40),
                        serde_json::to_string_pretty(details).unwrap_or_default()
                    );
                    writer.write_text(&text)?;
                } else {
                    writer.write(details)?;
                }
            } else if let Some(result) = report.sub_results.get(sub_name) {
                // Sub-analysis exists but has no details (e.g., failed or skipped)
                if writer.is_text() {
                    let msg = result.error.as_deref().unwrap_or("No details available");
                    writer.write_text(&format!("{}: {}", sub_name, msg))?;
                } else {
                    writer.write(result)?;
                }
            } else {
                anyhow::bail!(
                    "Sub-analysis '{}' not found. Available: {}",
                    sub_name,
                    report
                        .sub_results
                        .keys()
                        .cloned()
                        .collect::<Vec<_>>()
                        .join(", ")
                );
            }
        }
        None => {
            // Default: output full report
            if writer.is_text() {
                writer.write_text(&report.to_text())?;
            } else {
                writer.write(report)?;
            }
        }
    }
    Ok(())
}

/// Output summary-only mode (omits detail arrays)
fn output_summary(
    writer: &OutputWriter,
    report: &HealthReport,
    _format: OutputFormat,
) -> Result<()> {
    // Create a summary-only output structure
    let summary_output = serde_json::json!({
        "wrapper": "health",
        "path": report.path.display().to_string(),
        "language": report.language,
        "quick_mode": report.quick_mode,
        "total_elapsed_ms": report.total_elapsed_ms,
        "summary": report.summary,
        "errors": report.errors,
    });

    if writer.is_text() {
        writer.write_text(&report.to_text())?;
    } else {
        writer.write(&summary_output)?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_detail_parser_valid() {
        assert!(detail_parser("complexity").is_ok());
        assert!(detail_parser("cohesion").is_ok());
        assert!(detail_parser("dead_code").is_ok());
        assert!(detail_parser("martin").is_ok());
        assert!(detail_parser("coupling").is_ok());
        assert!(detail_parser("similarity").is_ok());
        assert!(detail_parser("all").is_ok());
    }

    #[test]
    fn test_detail_parser_invalid() {
        let result = detail_parser("invalid");
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("Invalid detail value"));
        assert!(err.contains("complexity"));
    }

    #[test]
    fn test_parse_language_valid() {
        assert!(matches!(parse_language("python"), Ok(Language::Python)));
        assert!(matches!(parse_language("py"), Ok(Language::Python)));
        assert!(matches!(parse_language("Python"), Ok(Language::Python)));
        assert!(matches!(
            parse_language("typescript"),
            Ok(Language::TypeScript)
        ));
        assert!(matches!(parse_language("ts"), Ok(Language::TypeScript)));
        assert!(matches!(parse_language("rust"), Ok(Language::Rust)));
        assert!(matches!(parse_language("go"), Ok(Language::Go)));
    }

    #[test]
    fn test_parse_language_invalid() {
        let result = parse_language("unknown");
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("Unsupported language"));
        assert!(err.contains("python"));
    }

    #[test]
    fn test_validate_quick_coupling_conflict() {
        let args = HealthArgs {
            path: PathBuf::from("."),
            detail: Some("coupling".to_string()),
            quick: true,
            preset: PresetArg::Default,
            max_items: 50,
            summary: false,
        };
        let result = args.validate();
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("--detail=coupling requires full mode"));
    }

    #[test]
    fn test_validate_quick_similarity_conflict() {
        let args = HealthArgs {
            path: PathBuf::from("."),
            detail: Some("similarity".to_string()),
            quick: true,
            preset: PresetArg::Default,
            max_items: 50,
            summary: false,
        };
        let result = args.validate();
        assert!(result.is_err());
        let err = result.unwrap_err().to_string();
        assert!(err.contains("--detail=similarity requires full mode"));
    }

    #[test]
    fn test_validate_quick_complexity_ok() {
        let args = HealthArgs {
            path: PathBuf::from("."),
            detail: Some("complexity".to_string()),
            quick: true,
            preset: PresetArg::Default,
            max_items: 50,
            summary: false,
        };
        assert!(args.validate().is_ok());
    }

    #[test]
    fn test_preset_conversion() {
        assert!(matches!(
            ThresholdPreset::from(PresetArg::Strict),
            ThresholdPreset::Strict
        ));
        assert!(matches!(
            ThresholdPreset::from(PresetArg::Default),
            ThresholdPreset::Default
        ));
        assert!(matches!(
            ThresholdPreset::from(PresetArg::Relaxed),
            ThresholdPreset::Relaxed
        ));
    }
}