loctree 0.13.0

Structural code intelligence for AI agents. Scan once, query everything.
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
//! Parsers for context extraction commands: slice, trace, focus, coverage, hotspots.
//!
//! These commands extract specific views/slices of the codebase for analysis.

use std::path::PathBuf;

use super::super::command::{
    Command, ContextOptions, CoverageOptions, FocusOptions, FollowOptions, HotspotsOptions,
    RepoViewOptions, SliceOptions, TraceOptions,
};

/// Parse `loct slice <target> [options]` command - extract file + dependencies.
pub(super) fn parse_slice_command(args: &[String]) -> Result<Command, String> {
    // Check for help flag first
    if args.iter().any(|a| a == "--help" || a == "-h") {
        return Err("loct slice - Extract file + dependencies for AI context

USAGE:
    loct slice <TARGET_PATH> [OPTIONS]

OPTIONS:
    --consumers, -c      Include reverse dependencies (default; compatibility no-op)
    --no-consumers       Hide reverse dependencies (old leaf-only behavior)
    --depth <N>          Maximum dependency depth to traverse (default: unlimited)
    --root <PATH>        Project root for resolving relative imports
    --rescan             Force snapshot update before slicing
    --help, -h           Show this help message

EXAMPLES:
    loct slice src/main.rs
    loct slice src/utils.ts --no-consumers"
            .to_string());
    }

    let mut opts = SliceOptions::default();
    let mut i = 0;

    while i < args.len() {
        let arg = &args[i];
        match arg.as_str() {
            "--consumers" | "-c" => {
                opts.consumers = true;
                i += 1;
            }
            "--no-consumers" => {
                opts.consumers = false;
                i += 1;
            }
            "--depth" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--depth requires a value".to_string())?;
                opts.depth = Some(value.parse().map_err(|_| "--depth requires a number")?);
                i += 2;
            }
            "--root" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--root requires a path".to_string())?;
                opts.root = Some(PathBuf::from(value));
                i += 2;
            }
            "--rescan" => {
                opts.rescan = true;
                i += 1;
            }
            _ if !arg.starts_with('-') => {
                if opts.target.is_empty() {
                    opts.target = arg.clone();
                } else {
                    return Err(format!(
                        "Unexpected argument '{}'. slice takes one target path.",
                        arg
                    ));
                }
                i += 1;
            }
            _ => {
                return Err(format!("Unknown option '{}' for 'slice' command.", arg));
            }
        }
    }

    if opts.target.is_empty() {
        return Err(
            "'slice' command requires a target file path. Usage: loct slice <path>".to_string(),
        );
    }

    Ok(Command::Slice(opts))
}

/// Parse `loct context [options]` command - produce an agent context pack.
pub(super) fn parse_context_command(args: &[String]) -> Result<Command, String> {
    let mut opts = ContextOptions::default();
    let mut i = 0;

    while i < args.len() {
        let arg = &args[i];
        match arg.as_str() {
            "--file" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--file requires a path".to_string())?;
                opts.file = Some(PathBuf::from(value));
                i += 2;
            }
            "--changed" => {
                opts.changed = true;
                i += 1;
            }
            "--task" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--task requires a value".to_string())?;
                opts.task = Some(value.clone());
                i += 2;
            }
            "--scope" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--scope requires a selector or named scope".to_string())?;
                opts.scopes.push(value.clone());
                i += 2;
            }
            "--with-aicx" => {
                opts.with_aicx = true;
                opts.no_aicx = false;
                i += 1;
            }
            "--no-aicx" => {
                opts.no_aicx = true;
                opts.with_aicx = false;
                i += 1;
            }
            "--project" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--project requires a path".to_string())?;
                opts.project = Some(PathBuf::from(value));
                i += 2;
            }
            "--aicx-project" | "--aicx-bucket" => {
                // Plan L04 / Finding #16 — operator override for the AICX
                // project bucket. Distinct from `--project` (path);
                // `--aicx-project` takes the bucket NAME aicx itself uses.
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--aicx-project requires a bucket name".to_string())?;
                opts.aicx_project_override = Some(value.clone());
                i += 2;
            }
            "--json" => {
                opts.json = true;
                i += 1;
            }
            "--full" => {
                // Cut 11: opt in to the full ContextPack. JSON is the default
                // machine format; `--full --markdown` renders the same pack
                // for humans. Pill markdown remains the zero-flag default.
                opts.full = true;
                i += 1;
            }
            "--markdown" | "--md" => {
                opts.markdown = true;
                i += 1;
            }
            "--format" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--format requires a value (markdown or json)".to_string())?;
                match value.as_str() {
                    "markdown" | "md" => opts.markdown = true,
                    "json" => opts.json = true,
                    other => {
                        return Err(format!(
                            "Unknown format '{}', expected markdown or json",
                            other
                        ));
                    }
                }
                i += 2;
            }
            _ if !arg.starts_with('-') => {
                if opts.file.is_none() {
                    let path = PathBuf::from(arg);
                    if opts.project.is_none() && path.is_dir() {
                        opts.project = Some(path);
                    } else {
                        opts.file = Some(path);
                    }
                    i += 1;
                } else {
                    return Err(format!(
                        "Unexpected argument '{}'. Use --task for task text or --file for a target file.",
                        arg
                    ));
                }
            }
            _ => {
                return Err(format!("Unknown option '{}' for 'context' command.", arg));
            }
        }
    }

    Ok(Command::Context(opts))
}

/// Parse `loct repo-view [project]` command - agent-ready repo overview.
pub(super) fn parse_repo_view_command(args: &[String]) -> Result<Command, String> {
    if args.iter().any(|a| a == "--help" || a == "-h") {
        return Err("loct repo-view - Repository overview for AI agents

USAGE:
    loct repo-view [PROJECT]

ARGUMENTS:
    [PROJECT]       Project root to analyze (default: current directory)

DESCRIPTION:
    First-class CLI counterpart of the MCP repo-view tool, with an optional
    project path. For full agent-ready context prefer `loct context`.

EXAMPLES:
    loct repo-view
    loct repo-view /path/to/project"
            .to_string());
    }

    let mut opts = RepoViewOptions::default();
    for arg in args {
        if arg.starts_with('-') {
            return Err(format!("Unknown option '{}' for 'repo-view' command.", arg));
        }
        if opts.project.is_some() {
            return Err(format!(
                "Unexpected argument '{}'. repo-view takes at most one project path.",
                arg
            ));
        }
        opts.project = Some(PathBuf::from(arg));
    }

    Ok(Command::RepoView(opts))
}

/// Parse `loct follow [scope] [options] [roots...]` command.
pub(super) fn parse_follow_command(args: &[String]) -> Result<Command, String> {
    if args.iter().any(|a| a == "--help" || a == "-h") {
        return Err("loct follow - Unified signal follower

USAGE:
    loct follow [SCOPE] [OPTIONS] [PATHS...]

SCOPES:
    all, dead, cycles, twins, hotspots, trace, commands, events, pipelines

OPTIONS:
    --handler <NAME>     Handler name for trace scope
    --limit <N>          Limit results where the underlying scope supports it
    --help, -h           Show this help message

EXAMPLES:
    loct follow
    loct follow dead
    loct follow cycles --limit 20
    loct follow trace --handler my_command"
            .to_string());
    }

    let valid_scopes = [
        "all",
        "dead",
        "cycles",
        "twins",
        "hotspots",
        "trace",
        "commands",
        "events",
        "pipelines",
    ];
    let mut opts = FollowOptions::default();
    let mut scope_seen = false;
    let mut i = 0;

    while i < args.len() {
        let arg = &args[i];
        match arg.as_str() {
            "--handler" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--handler requires a value".to_string())?;
                opts.handler = Some(value.clone());
                i += 2;
            }
            "--limit" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--limit requires a number".to_string())?;
                opts.limit = Some(value.parse().map_err(|_| "--limit requires a number")?);
                i += 2;
            }
            _ if !arg.starts_with('-') && !scope_seen && valid_scopes.contains(&arg.as_str()) => {
                opts.scope = arg.clone();
                scope_seen = true;
                i += 1;
            }
            _ if !arg.starts_with('-') => {
                opts.roots.push(PathBuf::from(arg));
                i += 1;
            }
            _ => {
                return Err(format!("Unknown option '{}' for 'follow' command.", arg));
            }
        }
    }

    if opts.scope == "trace" && opts.handler.is_none() {
        return Err("follow trace requires --handler <name>".to_string());
    }

    Ok(Command::Follow(opts))
}

/// Parse `loct trace <handler> [roots]` command - trace Tauri/IPC handler.
pub(super) fn parse_trace_command(args: &[String]) -> Result<Command, String> {
    // Check for help flag first
    if args.iter().any(|a| a == "--help" || a == "-h") {
        return Err("loct trace - Trace a Tauri/IPC handler end-to-end

USAGE:
    loct trace <handler> [ROOTS...]
    loct trace --handler <handler> [ROOTS...]

ARGUMENTS:
    <handler>         Handler name to trace (required)
    [ROOTS...]        Root directories to scan (default: current directory)

EXAMPLES:
    loct trace toggle_assistant
    loct trace --handler toggle_assistant
    loct trace standard_command apps/desktop"
            .to_string());
    }

    let mut opts = TraceOptions::default();
    let mut i = 0;

    while i < args.len() {
        let arg = &args[i];
        match arg.as_str() {
            "--handler" => {
                i += 1;
                let Some(handler) = args.get(i) else {
                    return Err("--handler requires a value".to_string());
                };
                opts.handler = handler.clone();
            }
            flag if flag.starts_with('-') => {
                return Err(format!("Unknown option '{}' for 'trace' command.", arg));
            }
            positional if opts.handler.is_empty() => {
                opts.handler = positional.to_string();
            }
            root => {
                opts.roots.push(PathBuf::from(root));
            }
        }
        i += 1;
    }

    if opts.handler.is_empty() {
        return Err(
            "trace requires a handler name. Usage: loct trace <handler> [ROOTS...]".to_string(),
        );
    }

    if opts.roots.is_empty() {
        opts.roots.push(PathBuf::from("."));
    }

    Ok(Command::Trace(opts))
}

/// Parse `loct focus <dir> [options]` command - focus on specific directory.
pub(super) fn parse_focus_command(args: &[String]) -> Result<Command, String> {
    if args.is_empty() {
        return Err("focus requires a target directory. Usage: loct focus <dir>".to_string());
    }

    // Check for --help first
    if args.iter().any(|a| a == "--help" || a == "-h")
        && let Some(help) = Command::format_command_help("focus")
    {
        println!("{}", help);
        std::process::exit(0);
    }

    let mut opts = FocusOptions::default();

    // First positional argument is the target directory
    if !args[0].starts_with('-') {
        opts.target = args[0].clone();
    } else {
        return Err("focus requires a target directory as first argument".to_string());
    }

    let mut i = 1;

    while i < args.len() {
        let arg = &args[i];
        match arg.as_str() {
            "--consumers" | "-c" => {
                opts.consumers = true;
                i += 1;
            }
            "--no-consumers" => {
                opts.consumers = false;
                i += 1;
            }
            "--depth" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--depth requires a value".to_string())?;
                opts.depth =
                    Some(value.parse::<usize>().map_err(|_| {
                        format!("Invalid depth value '{}', expected a number", value)
                    })?);
                i += 2;
            }
            "--root" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--root requires a path".to_string())?;
                opts.root = Some(PathBuf::from(value));
                i += 2;
            }
            "--json" => {
                opts.json = true;
                i += 1;
            }
            "--markdown" | "--md" => {
                opts.markdown = true;
                i += 1;
            }
            "--format" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--format requires a value (markdown or json)".to_string())?;
                match value.as_str() {
                    "markdown" | "md" => opts.markdown = true,
                    "json" => opts.json = true,
                    other => {
                        return Err(format!(
                            "Unknown format '{}', expected markdown or json",
                            other
                        ));
                    }
                }
                i += 2;
            }
            _ => {
                return Err(format!("Unknown option '{}' for 'focus' command.", arg));
            }
        }
    }

    Ok(Command::Focus(opts))
}

/// Parse `loct coverage [options]` command - analyze test coverage gaps.
pub(super) fn parse_coverage_command(args: &[String]) -> Result<Command, String> {
    // Check for help flag first
    if args.iter().any(|a| a == "--help" || a == "-h") {
        return Err("loct coverage - Analyze test coverage gaps

USAGE:
    loct coverage [OPTIONS] [PATHS...]

OPTIONS:
    --handlers       Show only handler coverage gaps
    --events         Show only event coverage gaps
    --tests          Show structural test coverage report
    --gaps           Show coverage gap analysis (default)
    --min-severity <LEVEL>
                     Filter by minimum severity (critical/high/medium/low)
    --include-artifacts
                     Disable the artifact fence (show findings from
                     vendored/minified/fixture/generated/template files)
    --json           Output as JSON
    --help, -h       Show this help message

EXAMPLES:
    loct coverage
    loct coverage --handlers"
            .to_string());
    }

    let mut opts = CoverageOptions::default();
    let mut i = 0;

    while i < args.len() {
        let arg = &args[i];
        match arg.as_str() {
            "--handlers" | "--handlers-only" => {
                opts.handlers_only = true;
                i += 1;
            }
            "--events" | "--events-only" => {
                opts.events_only = true;
                i += 1;
            }
            "--tests" => {
                opts.tests = true;
                i += 1;
            }
            "--gaps" => {
                opts.gaps = true;
                i += 1;
            }
            "--include-artifacts" => {
                opts.include_artifacts = true;
                i += 1;
            }
            "--min-severity" => {
                let value = args.get(i + 1).ok_or_else(|| {
                    "--min-severity requires a value (critical/high/medium/low)".to_string()
                })?;
                opts.min_severity = Some(value.clone());
                i += 2;
            }
            _ if !arg.starts_with('-') => {
                opts.roots.push(PathBuf::from(arg));
                i += 1;
            }
            _ => {
                return Err(format!("Unknown option '{}' for 'coverage' command.", arg));
            }
        }
    }

    if opts.roots.is_empty() {
        opts.roots.push(PathBuf::from("."));
    }

    Ok(Command::Coverage(opts))
}

/// Parse `loct hotspots [options]` command - find high-impact files.
pub(super) fn parse_hotspots_command(args: &[String]) -> Result<Command, String> {
    // Check for --help first
    if args.iter().any(|a| a == "--help" || a == "-h")
        && let Some(help) = Command::format_command_help("hotspots")
    {
        println!("{}", help);
        std::process::exit(0);
    }

    let mut opts = HotspotsOptions::default();
    let mut i = 0;

    while i < args.len() {
        let arg = &args[i];
        match arg.as_str() {
            "--min" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--min requires a value".to_string())?;
                opts.min_imports =
                    Some(value.parse::<usize>().map_err(|_| {
                        format!("Invalid min value '{}', expected a number", value)
                    })?);
                i += 2;
            }
            "--limit" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--limit requires a value".to_string())?;
                opts.limit =
                    Some(value.parse::<usize>().map_err(|_| {
                        format!("Invalid limit value '{}', expected a number", value)
                    })?);
                i += 2;
            }
            "--leaves" => {
                opts.leaves_only = true;
                i += 1;
            }
            "--coupling" => {
                opts.coupling = true;
                i += 1;
            }
            "--root" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--root requires a path".to_string())?;
                opts.root = Some(PathBuf::from(value));
                i += 2;
            }
            _ => {
                return Err(format!("Unknown option '{}' for 'hotspots' command.", arg));
            }
        }
    }

    Ok(Command::Hotspots(opts))
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_parse_slice_command() {
        let args = vec!["src/main.rs".into(), "--consumers".into()];
        let result = parse_slice_command(&args).unwrap();
        if let Command::Slice(opts) = result {
            assert_eq!(opts.target, "src/main.rs");
            assert!(opts.consumers);
        } else {
            panic!("Expected Slice command");
        }
    }

    #[test]
    fn test_parse_trace_command() {
        let args = vec!["toggle_assistant".into(), "app".into()];
        let result = parse_trace_command(&args).unwrap();
        if let Command::Trace(opts) = result {
            assert_eq!(opts.handler, "toggle_assistant");
            assert_eq!(opts.roots, vec![PathBuf::from("app")]);
        } else {
            panic!("Expected Trace command");
        }
    }

    #[test]
    fn test_parse_trace_command_handler_flag() {
        let args = vec!["--handler".into(), "toggle_assistant".into(), "app".into()];
        let result = parse_trace_command(&args).unwrap();
        if let Command::Trace(opts) = result {
            assert_eq!(opts.handler, "toggle_assistant");
            assert_eq!(opts.roots, vec![PathBuf::from("app")]);
        } else {
            panic!("Expected Trace command");
        }
    }

    #[test]
    fn test_parse_context_command() {
        let args = vec![
            "--file".into(),
            "Cargo.toml".into(),
            "--changed".into(),
            "--task".into(),
            "fix exports".into(),
            "--with-aicx".into(),
            "--project".into(),
            ".".into(),
            "--json".into(),
        ];
        let result = parse_context_command(&args).unwrap();
        if let Command::Context(opts) = result {
            assert_eq!(opts.file, Some(PathBuf::from("Cargo.toml")));
            assert!(opts.changed);
            assert_eq!(opts.task.as_deref(), Some("fix exports"));
            assert!(opts.with_aicx);
            assert!(!opts.no_aicx);
            assert_eq!(opts.project, Some(PathBuf::from(".")));
            assert!(opts.json);
            assert!(!opts.full);
        } else {
            panic!("Expected Context command");
        }
    }

    #[test]
    fn test_parse_context_command_accepts_format_flag_for_cli_mcp_parity() {
        // W5.5 regression: --format (markdown|json) must work for CLI/MCP flag parity
        // (previously "Unknown option --format" while MCP context supported format).
        let args = vec![
            "--format".into(),
            "markdown".into(),
            "--file".into(),
            "src/lib.rs".into(),
        ];
        let result = parse_context_command(&args).unwrap();
        if let Command::Context(opts) = result {
            assert!(opts.markdown);
            assert_eq!(opts.file, Some(PathBuf::from("src/lib.rs")));
        } else {
            panic!("Expected Context command");
        }

        let argsj = vec!["--format".into(), "json".into()];
        let rj = parse_context_command(&argsj).unwrap();
        if let Command::Context(opts) = rj {
            assert!(opts.json);
        } else {
            panic!("Expected Context command");
        }
    }

    #[test]
    fn test_parse_context_scope_flags_are_repeatable() {
        let args = vec![
            "--scope".into(),
            "path:loctree-rs/src/cli/".into(),
            "--scope".into(),
            "tag:cli".into(),
        ];
        let result = parse_context_command(&args).unwrap();
        if let Command::Context(opts) = result {
            assert_eq!(
                opts.scopes,
                vec![
                    "path:loctree-rs/src/cli/".to_string(),
                    "tag:cli".to_string(),
                ]
            );
        } else {
            panic!("Expected Context command");
        }
    }

    #[test]
    fn test_parse_context_full_flag() {
        let args = vec!["--full".into(), "--markdown".into()];
        let result = parse_context_command(&args).unwrap();
        if let Command::Context(opts) = result {
            assert!(opts.full);
            assert!(opts.markdown);
        } else {
            panic!("Expected Context command");
        }
    }

    #[test]
    fn test_parse_context_no_aicx() {
        let result = parse_context_command(&["--no-aicx".into()]).unwrap();
        if let Command::Context(opts) = result {
            assert!(opts.no_aicx);
            assert!(!opts.with_aicx);
        } else {
            panic!("Expected Context command");
        }
    }

    #[test]
    fn test_parse_context_aicx_project_override() {
        // Plan L04 / Finding #16 — operator-supplied AICX bucket override.
        let args = vec![
            "--with-aicx".into(),
            "--aicx-project".into(),
            "loctree-suite".into(),
        ];
        let result = parse_context_command(&args).unwrap();
        if let Command::Context(opts) = result {
            assert!(opts.with_aicx);
            assert_eq!(
                opts.aicx_project_override.as_deref(),
                Some("loctree-suite"),
                "operator override flag must populate aicx_project_override"
            );
            // --project (path) is independent and still None.
            assert!(opts.project.is_none());
        } else {
            panic!("Expected Context command");
        }
    }

    #[test]
    fn test_parse_context_aicx_bucket_alias() {
        // `--aicx-bucket` alias accepted for the same intent.
        let args = vec!["--aicx-bucket".into(), "monorepo-frontend".into()];
        let result = parse_context_command(&args).unwrap();
        if let Command::Context(opts) = result {
            assert_eq!(
                opts.aicx_project_override.as_deref(),
                Some("monorepo-frontend")
            );
        } else {
            panic!("Expected Context command");
        }
    }

    #[test]
    fn test_parse_repo_view_command_with_project() {
        let args = vec!["/tmp/project".into()];
        let result = parse_repo_view_command(&args).unwrap();
        if let Command::RepoView(opts) = result {
            assert_eq!(opts.project, Some(PathBuf::from("/tmp/project")));
        } else {
            panic!("Expected RepoView command");
        }
    }

    #[test]
    fn test_parse_follow_command_with_scope_handler_and_limit() {
        let args = vec![
            "trace".into(),
            "--handler".into(),
            "my_command".into(),
            "--limit".into(),
            "20".into(),
            "app".into(),
        ];
        let result = parse_follow_command(&args).unwrap();
        if let Command::Follow(opts) = result {
            assert_eq!(opts.scope, "trace");
            assert_eq!(opts.handler.as_deref(), Some("my_command"));
            assert_eq!(opts.limit, Some(20));
            assert_eq!(opts.roots, vec![PathBuf::from("app")]);
        } else {
            panic!("Expected Follow command");
        }
    }

    #[test]
    fn test_parse_focus_command() {
        let args = vec!["src/ui".into(), "--consumers".into()];
        let result = parse_focus_command(&args).unwrap();
        if let Command::Focus(opts) = result {
            assert_eq!(opts.target, "src/ui");
            assert!(opts.consumers);
        } else {
            panic!("Expected Focus command");
        }
    }

    #[test]
    fn test_parse_focus_command_accepts_format_for_cli_mcp_parity() {
        // W5.5 regression fixture: --format must not be "Unknown option" (drift vs MCP)
        let args = vec!["src/ui".into(), "--format".into(), "markdown".into()];
        let result = parse_focus_command(&args).unwrap();
        if let Command::Focus(opts) = result {
            assert_eq!(opts.target, "src/ui");
            assert!(opts.markdown);
        } else {
            panic!("Expected Focus command");
        }

        let args2 = vec![".".into(), "--format".into(), "json".into()];
        let result2 = parse_focus_command(&args2).unwrap();
        if let Command::Focus(opts) = result2 {
            assert!(opts.json);
        } else {
            panic!("Expected Focus command");
        }
    }

    #[test]
    fn test_parse_coverage_command() {
        let args = vec!["--handlers".into()];
        let result = parse_coverage_command(&args).unwrap();
        if let Command::Coverage(opts) = result {
            assert!(opts.handlers_only);
        } else {
            panic!("Expected Coverage command");
        }
    }

    #[test]
    fn test_parse_hotspots_command() {
        let args = vec!["--min".into(), "5".into(), "--limit".into(), "10".into()];
        let result = parse_hotspots_command(&args).unwrap();
        if let Command::Hotspots(opts) = result {
            assert_eq!(opts.min_imports, Some(5));
            assert_eq!(opts.limit, Some(10));
        } else {
            panic!("Expected Hotspots command");
        }
    }
}