rustdupe 0.2.0

Smart duplicate file finder with interactive TUI
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
//! Command-line interface definitions for RustDupe.
//!
//! This module defines all CLI arguments, subcommands, and options using the clap derive API.
//! The CLI follows standard conventions with global options (verbosity, color) and
//! subcommands for different operations.
//!
//! # Example
//!
//! ```bash
//! # Scan a directory with TUI output (default)
//! rustdupe scan ~/Downloads
//!
//! # Scan with JSON output for scripting
//! rustdupe scan ~/Downloads --output json
//!
//! # Scan with size filters
//! rustdupe scan ~/Downloads --min-size 1MB --max-size 1GB
//!
//! # Verbose mode for debugging
//! rustdupe -v scan ~/Downloads
//! ```

use clap::{Args, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;

/// Smart duplicate file finder with interactive TUI.
///
/// RustDupe finds duplicate files using content hashing (BLAKE3), provides an
/// interactive TUI for review, and supports safe deletion via system trash.
#[derive(Debug, Parser)]
#[command(name = "rustdupe")]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
pub struct Cli {
    /// Increase verbosity level (-v for debug, -vv for trace)
    #[arg(short, long, action = clap::ArgAction::Count, global = true)]
    pub verbose: u8,

    /// Suppress all output except errors
    #[arg(short, long, global = true, conflicts_with = "verbose")]
    pub quiet: bool,

    /// Disable colored output
    #[arg(long, global = true, env = "NO_COLOR")]
    pub no_color: bool,

    /// TUI theme (light, dark, auto)
    #[arg(long, value_enum, default_value = "auto", global = true)]
    pub theme: ThemeArg,

    /// Subcommand to execute
    #[command(subcommand)]
    pub command: Commands,
}

/// Available subcommands for RustDupe.
#[derive(Debug, Subcommand)]
pub enum Commands {
    /// Scan a directory for duplicate files
    Scan(Box<ScanArgs>),
    /// Load a previously saved session
    Load(LoadArgs),
}

/// Arguments for the scan subcommand.
#[derive(Debug, Args)]
pub struct ScanArgs {
    /// Directory path to scan for duplicates
    #[arg(value_name = "PATH", required_unless_present = "load_session")]
    pub path: Option<PathBuf>,

    /// Load a previously saved session instead of scanning
    #[arg(
        long,
        value_name = "SESSION_FILE",
        conflicts_with = "path",
        help_heading = "Scanning Options"
    )]
    pub load_session: Option<PathBuf>,

    /// Save scan results to a session file
    #[arg(long, value_name = "PATH", help_heading = "Output Options")]
    pub save_session: Option<PathBuf>,

    /// Output format (tui for interactive, json/csv for scripting, session for persistence, html for report, script for deletion)
    #[arg(
        short,
        long,
        value_enum,
        default_value = "tui",
        help_heading = "Output Options"
    )]
    pub output: OutputFormat,

    /// Write output to a file instead of stdout
    #[arg(long, value_name = "PATH", help_heading = "Output Options")]
    pub output_file: Option<PathBuf>,

    /// Type of deletion script to generate
    #[arg(long, value_enum, value_name = "TYPE", help_heading = "Output Options")]
    pub script_type: Option<ScriptTypeArg>,

    /// Minimum file size to consider (e.g., 1KB, 1MB, 1GB)
    ///
    /// Supports suffixes: B, KB, KiB, MB, MiB, GB, GiB, TB, TiB
    #[arg(long, value_name = "SIZE", value_parser = parse_size, help_heading = "Filtering Options")]
    pub min_size: Option<u64>,

    /// Maximum file size to consider (e.g., 1KB, 1MB, 1GB)
    ///
    /// Supports suffixes: B, KB, KiB, MB, MiB, GB, GiB, TB, TiB
    #[arg(long, value_name = "SIZE", value_parser = parse_size, help_heading = "Filtering Options")]
    pub max_size: Option<u64>,

    /// Only include files modified after this date (YYYY-MM-DD)
    #[arg(long, value_name = "DATE", value_parser = parse_date, help_heading = "Filtering Options")]
    pub newer_than: Option<std::time::SystemTime>,

    /// Only include files modified before this date (YYYY-MM-DD)
    #[arg(long, value_name = "DATE", value_parser = parse_date, help_heading = "Filtering Options")]
    pub older_than: Option<std::time::SystemTime>,

    /// Regex patterns to include (filename must match at least one)
    ///
    /// Example: --regex ".*\.jpg$"
    #[arg(
        long = "regex",
        alias = "regex-include",
        value_name = "PATTERN",
        help_heading = "Filtering Options"
    )]
    pub regex_include: Vec<String>,

    /// Regex patterns to exclude (filename must not match any)
    ///
    /// Example: --regex-exclude "temp_.*"
    #[arg(
        long = "regex-exclude",
        value_name = "PATTERN",
        help_heading = "Filtering Options"
    )]
    pub regex_exclude: Vec<String>,

    /// Filter by file type categories (can be specified multiple times)
    #[arg(
        long = "file-type",
        value_enum,
        value_name = "TYPE",
        help_heading = "Filtering Options"
    )]
    pub file_types: Vec<FileType>,

    /// Glob patterns to ignore (can be specified multiple times)
    ///
    /// These patterns are added to any .gitignore patterns found.
    #[arg(
        short,
        long = "ignore",
        value_name = "PATTERN",
        help_heading = "Filtering Options"
    )]
    pub ignore_patterns: Vec<String>,

    /// Follow symbolic links during scan
    ///
    /// Warning: May cause infinite loops if symlinks form cycles.
    #[arg(long, help_heading = "Scanning Options")]
    pub follow_symlinks: bool,

    /// Skip hidden files and directories (starting with .)
    #[arg(long, help_heading = "Scanning Options")]
    pub skip_hidden: bool,

    /// Number of I/O threads for hashing (default: 4)
    ///
    /// Lower values reduce disk thrashing on HDDs.
    #[arg(
        long,
        value_name = "N",
        default_value = "4",
        help_heading = "Scanning Options"
    )]
    pub io_threads: usize,

    /// Enable paranoid mode: byte-by-byte verification after hash match
    ///
    /// Slower but guarantees no hash collisions.
    #[arg(long, help_heading = "Scanning Options")]
    pub paranoid: bool,

    /// Use permanent deletion instead of moving to trash
    ///
    /// Warning: Files cannot be recovered after permanent deletion.
    #[arg(long, help_heading = "Safety & Deletion Options")]
    pub permanent: bool,

    /// Skip confirmation prompts (required with --permanent in non-interactive mode)
    #[arg(short = 'y', long, help_heading = "Safety & Deletion Options")]
    pub yes: bool,

    /// Path to the hash cache database
    ///
    /// If not specified, a default platform-specific path is used.
    #[arg(long, value_name = "PATH", help_heading = "Cache Options")]
    pub cache: Option<PathBuf>,

    /// Disable hash caching
    #[arg(long, conflicts_with = "cache", help_heading = "Cache Options")]
    pub no_cache: bool,

    /// Clear the hash cache before scanning
    #[arg(long, help_heading = "Cache Options")]
    pub clear_cache: bool,

    /// Do not perform any deletions (read-only mode)
    #[arg(
        long,
        alias = "analyze-only",
        help_heading = "Safety & Deletion Options"
    )]
    pub dry_run: bool,

    /// Reference directories (files here are never selected for deletion)
    ///
    /// Example: --reference /backups/photos
    ///
    /// Can be specified multiple times. Files in these directories will be
    /// marked as protected and cannot be selected for deletion.
    #[arg(
        long = "reference",
        value_name = "PATH",
        help_heading = "Safety & Deletion Options"
    )]
    pub reference_paths: Vec<PathBuf>,
}

/// Arguments for the load subcommand.
#[derive(Debug, Args)]
pub struct LoadArgs {
    /// Session file to load
    #[arg(value_name = "SESSION_FILE")]
    pub path: PathBuf,

    /// Output format (tui for interactive, json/csv for scripting, html for report, script for deletion)
    #[arg(
        short,
        long,
        value_enum,
        default_value = "tui",
        help_heading = "Output Options"
    )]
    pub output: OutputFormat,

    /// Write output to a file instead of stdout
    #[arg(long, value_name = "PATH", help_heading = "Output Options")]
    pub output_file: Option<PathBuf>,

    /// Type of deletion script to generate
    #[arg(long, value_enum, value_name = "TYPE", help_heading = "Output Options")]
    pub script_type: Option<ScriptTypeArg>,

    /// Do not perform any deletions (read-only mode)
    #[arg(long, alias = "analyze-only", help_heading = "Safety Options")]
    pub dry_run: bool,
}

/// Output format for scan results.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum OutputFormat {
    /// Interactive terminal user interface
    Tui,
    /// JSON output for scripting
    Json,
    /// CSV output for spreadsheets
    Csv,
    /// HTML report for browser viewing
    Html,
    /// Session file format for persistence
    Session,
    /// Shell script for deletion
    Script,
}

/// Script type for deletion script generation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum ScriptTypeArg {
    /// POSIX-compliant shell script (sh/bash/zsh)
    Posix,
    /// Windows PowerShell script
    Powershell,
}

/// File type categories for filtering.
#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum FileType {
    /// Image files (jpg, png, etc.)
    Images,
    /// Video files (mp4, mkv, etc.)
    Videos,
    /// Audio files (mp3, wav, etc.)
    Audio,
    /// Document files (pdf, docx, etc.)
    Documents,
    /// Archive files (zip, tar, etc.)
    Archives,
}

/// TUI theme options.
#[derive(
    Debug, Clone, Copy, PartialEq, Eq, ValueEnum, Default, serde::Serialize, serde::Deserialize,
)]
pub enum ThemeArg {
    /// Use terminal's default color scheme or detect automatically
    #[default]
    Auto,
    /// High-contrast light theme
    Light,
    /// High-contrast dark theme
    Dark,
}

impl From<FileType> for crate::scanner::FileCategory {
    fn from(t: FileType) -> Self {
        match t {
            FileType::Images => crate::scanner::FileCategory::Images,
            FileType::Videos => crate::scanner::FileCategory::Videos,
            FileType::Audio => crate::scanner::FileCategory::Audio,
            FileType::Documents => crate::scanner::FileCategory::Documents,
            FileType::Archives => crate::scanner::FileCategory::Archives,
        }
    }
}

impl std::fmt::Display for OutputFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            OutputFormat::Tui => write!(f, "tui"),
            OutputFormat::Json => write!(f, "json"),
            OutputFormat::Csv => write!(f, "csv"),
            OutputFormat::Html => write!(f, "html"),
            OutputFormat::Session => write!(f, "session"),
            OutputFormat::Script => write!(f, "script"),
        }
    }
}

/// Parse a human-readable size string into bytes.
///
/// Supports suffixes: B, KB, KiB, MB, MiB, GB, GiB, TB, TiB
/// Case-insensitive. Numbers without suffix are treated as bytes.
///
/// # Examples
///
/// ```
/// use rustdupe::cli::parse_size;
///
/// assert_eq!(parse_size("1024").unwrap(), 1024);
/// assert_eq!(parse_size("1KB").unwrap(), 1000);
/// assert_eq!(parse_size("1KiB").unwrap(), 1024);
/// assert_eq!(parse_size("1MB").unwrap(), 1_000_000);
/// assert_eq!(parse_size("1MiB").unwrap(), 1_048_576);
/// ```
/// # Errors
///
/// Returns an error if the string is empty, contains an invalid number,
/// a negative number, or an unknown size suffix.
pub fn parse_size(s: &str) -> Result<u64, String> {
    let s = s.trim();
    if s.is_empty() {
        return Err("Size cannot be empty".to_string());
    }

    // Find where the number ends and the suffix begins
    let (num_str, suffix) = match s.find(|c: char| !c.is_ascii_digit() && c != '.') {
        Some(idx) => (&s[..idx], s[idx..].trim().to_uppercase()),
        None => (s, String::new()),
    };

    let num: f64 = num_str
        .parse()
        .map_err(|_| format!("Invalid number: '{num_str}'"))?;

    if num < 0.0 {
        return Err("Size cannot be negative".to_string());
    }

    let multiplier: u64 = match suffix.as_str() {
        "" | "B" => 1,
        "KB" | "K" => 1_000,
        "KIB" => 1_024,
        "MB" | "M" => 1_000_000,
        "MIB" => 1_048_576,
        "GB" | "G" => 1_000_000_000,
        "GIB" => 1_073_741_824,
        "TB" | "T" => 1_000_000_000_000,
        "TIB" => 1_099_511_627_776,
        _ => return Err(format!("Unknown size suffix: '{suffix}'")),
    };

    Ok((num * multiplier as f64) as u64)
}

/// Parse a date string in YYYY-MM-DD format into SystemTime.
pub fn parse_date(s: &str) -> Result<std::time::SystemTime, String> {
    use chrono::{NaiveDate, TimeZone, Utc};
    NaiveDate::parse_from_str(s, "%Y-%m-%d")
        .map(|d| {
            // Use 00:00:00 UTC for the date
            let dt = Utc.from_utc_datetime(&d.and_hms_opt(0, 0, 0).unwrap());
            std::time::SystemTime::from(dt)
        })
        .map_err(|e| format!("Invalid date format (expected YYYY-MM-DD): {e}"))
}

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

    #[test]
    fn test_parse_size_bytes() {
        assert_eq!(parse_size("1024").unwrap(), 1024);
        assert_eq!(parse_size("1024B").unwrap(), 1024);
        assert_eq!(parse_size("0").unwrap(), 0);
    }

    #[test]
    fn test_parse_size_kilobytes() {
        assert_eq!(parse_size("1KB").unwrap(), 1_000);
        assert_eq!(parse_size("1K").unwrap(), 1_000);
        assert_eq!(parse_size("1KiB").unwrap(), 1_024);
        assert_eq!(parse_size("1kib").unwrap(), 1_024); // Case insensitive
    }

    #[test]
    fn test_parse_size_megabytes() {
        assert_eq!(parse_size("1MB").unwrap(), 1_000_000);
        assert_eq!(parse_size("1MiB").unwrap(), 1_048_576);
        assert_eq!(parse_size("10MB").unwrap(), 10_000_000);
    }

    #[test]
    fn test_parse_size_gigabytes() {
        assert_eq!(parse_size("1GB").unwrap(), 1_000_000_000);
        assert_eq!(parse_size("1GiB").unwrap(), 1_073_741_824);
    }

    #[test]
    fn test_parse_size_terabytes() {
        assert_eq!(parse_size("1TB").unwrap(), 1_000_000_000_000);
        assert_eq!(parse_size("1TiB").unwrap(), 1_099_511_627_776);
    }

    #[test]
    fn test_parse_size_fractional() {
        assert_eq!(parse_size("1.5MB").unwrap(), 1_500_000);
        assert_eq!(parse_size("0.5GB").unwrap(), 500_000_000);
    }

    #[test]
    fn test_parse_size_with_whitespace() {
        assert_eq!(parse_size("  1024  ").unwrap(), 1024);
        assert_eq!(parse_size("1 MB").unwrap(), 1_000_000);
    }

    #[test]
    fn test_parse_size_errors() {
        assert!(parse_size("").is_err());
        assert!(parse_size("abc").is_err());
        assert!(parse_size("1XB").is_err());
        assert!(parse_size("-1MB").is_err());
    }

    #[test]
    fn test_cli_parse_help() {
        // Verify that help can be parsed without panicking
        let result = Cli::try_parse_from(["rustdupe", "--help"]);
        // --help causes an early exit, which is an error in try_parse_from
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_parse_scan_basic() {
        let cli = Cli::try_parse_from(["rustdupe", "scan", "/some/path"]).unwrap();
        assert_eq!(cli.verbose, 0);
        match cli.command {
            Commands::Scan(args) => {
                assert_eq!(args.path, Some(PathBuf::from("/some/path")));
                assert_eq!(args.output, OutputFormat::Tui);
            }
            _ => panic!("Expected Scan command"),
        }
    }

    #[test]
    fn test_cli_parse_scan_with_options() {
        let cli = Cli::try_parse_from([
            "rustdupe",
            "-v",
            "scan",
            "/path",
            "--output",
            "json",
            "--min-size",
            "1MB",
            "--max-size",
            "1GB",
            "--newer-than",
            "2026-01-01",
            "--older-than",
            "2026-12-31",
            "--ignore",
            "*.tmp",
            "--ignore",
            "node_modules",
            "--regex",
            "foo.*",
            "--regex-exclude",
            "bar.*",
        ])
        .unwrap();

        assert_eq!(cli.verbose, 1);

        match cli.command {
            Commands::Scan(args) => {
                assert_eq!(args.output, OutputFormat::Json);
                assert_eq!(args.min_size, Some(1_000_000));
                assert_eq!(args.max_size, Some(1_000_000_000));
                assert!(args.newer_than.is_some());
                assert!(args.older_than.is_some());
                assert_eq!(args.ignore_patterns, vec!["*.tmp", "node_modules"]);
                assert_eq!(args.regex_include, vec!["foo.*"]);
                assert_eq!(args.regex_exclude, vec!["bar.*"]);
            }
            _ => panic!("Expected Scan command"),
        }
    }

    #[test]
    fn test_cli_parse_scan_file_types() {
        let cli = Cli::try_parse_from([
            "rustdupe",
            "scan",
            "/path",
            "--file-type",
            "images",
            "--file-type",
            "documents",
        ])
        .unwrap();

        match cli.command {
            Commands::Scan(args) => {
                assert_eq!(args.file_types, vec![FileType::Images, FileType::Documents]);
            }
            _ => panic!("Expected Scan command"),
        }
    }

    #[test]
    fn test_parse_date() {
        assert!(parse_date("2026-02-01").is_ok());
        assert!(parse_date("2026-02-31").is_err()); // Invalid day
        assert!(parse_date("not-a-date").is_err());
    }

    #[test]
    fn test_cli_parse_scan_script() {
        let cli = Cli::try_parse_from([
            "rustdupe",
            "scan",
            "/path",
            "--output",
            "script",
            "--script-type",
            "posix",
        ])
        .unwrap();
        match cli.command {
            Commands::Scan(args) => {
                assert_eq!(args.output, OutputFormat::Script);
                assert_eq!(args.script_type, Some(ScriptTypeArg::Posix));
            }
            _ => panic!("Expected Scan command"),
        }
    }

    #[test]
    fn test_cli_parse_load_script() {
        let cli = Cli::try_parse_from([
            "rustdupe",
            "load",
            "session.json",
            "--output",
            "script",
            "--script-type",
            "powershell",
        ])
        .unwrap();
        match cli.command {
            Commands::Load(args) => {
                assert_eq!(args.output, OutputFormat::Script);
                assert_eq!(args.script_type, Some(ScriptTypeArg::Powershell));
            }
            _ => panic!("Expected Load command"),
        }
    }

    #[test]
    fn test_cli_quiet_conflicts_with_verbose() {
        let result = Cli::try_parse_from(["rustdupe", "-v", "-q", "scan", "/path"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_parse_scan_csv() {
        let cli = Cli::try_parse_from(["rustdupe", "scan", "/path", "--output", "csv"]).unwrap();
        match cli.command {
            Commands::Scan(args) => {
                assert_eq!(args.output, OutputFormat::Csv);
            }
            _ => panic!("Expected Scan command"),
        }
    }

    #[test]
    fn test_cli_parse_quiet() {
        let cli = Cli::try_parse_from(["rustdupe", "-q", "scan", "/path"]).unwrap();
        assert!(cli.quiet);
        assert_eq!(cli.verbose, 0);
    }

    #[test]
    fn test_cli_parse_scan_all_flags() {
        let cli = Cli::try_parse_from([
            "rustdupe",
            "scan",
            "/path",
            "--follow-symlinks",
            "--skip-hidden",
            "--io-threads",
            "8",
            "--paranoid",
            "--permanent",
            "--yes",
        ])
        .unwrap();

        match cli.command {
            Commands::Scan(args) => {
                assert!(args.follow_symlinks);
                assert!(args.skip_hidden);
                assert_eq!(args.io_threads, 8);
                assert!(args.paranoid);
                assert!(args.permanent);
                assert!(args.yes);
            }
            _ => panic!("Expected Scan command"),
        }
    }

    #[test]
    fn test_cli_no_color_env() {
        // Use a lock if we had one, but since we don't, we'll just be careful.
        // Note: This test may fail if run in parallel with other CLI tests.
        std::env::set_var("NO_COLOR", "true");
        let cli = Cli::try_parse_from(["rustdupe", "scan", "/path"]).unwrap();
        assert!(cli.no_color);
        std::env::remove_var("NO_COLOR");
    }

    #[test]
    fn test_cli_invalid_subcommand() {
        let result = Cli::try_parse_from(["rustdupe", "invalid", "/path"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_missing_path() {
        let result = Cli::try_parse_from(["rustdupe", "scan"]);
        assert!(result.is_err());
    }

    #[test]
    fn test_cli_version_flag() {
        let result = Cli::try_parse_from(["rustdupe", "--version"]);
        assert!(result.is_err()); // clap exits on --version
    }

    #[test]
    fn test_cli_parse_scan_session_flags() {
        let cli = Cli::try_parse_from([
            "rustdupe",
            "scan",
            "/path",
            "--save-session",
            "session.json",
            "--reference",
            "/ref1",
            "--reference",
            "/ref2",
            "--dry-run",
        ])
        .unwrap();

        match cli.command {
            Commands::Scan(args) => {
                assert_eq!(args.path, Some(PathBuf::from("/path")));
                assert_eq!(args.save_session, Some(PathBuf::from("session.json")));
                assert_eq!(
                    args.reference_paths,
                    vec![PathBuf::from("/ref1"), PathBuf::from("/ref2")]
                );
                assert!(args.dry_run);
            }
            _ => panic!("Expected Scan command"),
        }

        let cli = Cli::try_parse_from(["rustdupe", "scan", "/path", "--analyze-only"]).unwrap();
        match cli.command {
            Commands::Scan(args) => {
                assert!(args.dry_run);
            }
            _ => panic!("Expected Scan command"),
        }

        let cli =
            Cli::try_parse_from(["rustdupe", "scan", "--load-session", "session.json"]).unwrap();
        match cli.command {
            Commands::Scan(args) => {
                assert_eq!(args.path, None);
                assert_eq!(args.load_session, Some(PathBuf::from("session.json")));
            }
            _ => panic!("Expected Scan command"),
        }
    }

    #[test]
    fn test_cli_parse_load_subcommand() {
        let cli =
            Cli::try_parse_from(["rustdupe", "load", "session.json", "--output", "json"]).unwrap();
        match cli.command {
            Commands::Load(args) => {
                assert_eq!(args.path, PathBuf::from("session.json"));
                assert_eq!(args.output, OutputFormat::Json);
            }
            _ => panic!("Expected Load command"),
        }
    }

    #[test]
    fn test_cli_parse_cache_flags() {
        let cli = Cli::try_parse_from([
            "rustdupe",
            "scan",
            "/path",
            "--cache",
            "mycache.db",
            "--clear-cache",
        ])
        .unwrap();

        match cli.command {
            Commands::Scan(args) => {
                assert_eq!(args.cache, Some(PathBuf::from("mycache.db")));
                assert!(args.clear_cache);
                assert!(!args.no_cache);
            }
            _ => panic!("Expected Scan command"),
        }

        let cli = Cli::try_parse_from(["rustdupe", "scan", "/path", "--no-cache"]).unwrap();
        match cli.command {
            Commands::Scan(args) => {
                assert!(args.no_cache);
                assert!(args.cache.is_none());
            }
            _ => panic!("Expected Scan command"),
        }
    }
}