Skip to main content

rusty_sweeper/
cli.rs

1use clap::Parser;
2use std::path::PathBuf;
3
4/// Rusty Sweeper - A Linux disk usage management utility
5#[derive(Parser, Debug)]
6#[command(name = "rusty-sweeper")]
7#[command(author, version, about, long_about = None)]
8#[command(propagate_version = true)]
9pub struct Cli {
10    /// Directory to scan (defaults to the current working directory)
11    #[arg(value_name = "PATH")]
12    pub path: Option<PathBuf>,
13
14    /// Use command-line reports and cleanup instead of opening the TUI
15    #[arg(long)]
16    pub cli: bool,
17
18    /// Discover project artifacts, report them, and confirm before cleaning
19    #[arg(long, requires = "cli", conflicts_with_all = ["json", "depth"])]
20    pub clean: bool,
21
22    /// Clean without asking for confirmation (requires --clean)
23    #[arg(short = 'y', long, requires = "clean")]
24    pub yes: bool,
25
26    /// Print the complete scan as JSON (requires --cli)
27    #[arg(long, requires = "cli")]
28    pub json: bool,
29
30    /// Maximum display depth for text reports (root = 0; scans all depths)
31    #[arg(long, requires = "cli", conflicts_with = "json")]
32    pub depth: Option<usize>,
33
34    /// Path to configuration file
35    #[arg(short, long, global = true, value_name = "PATH")]
36    pub config: Option<PathBuf>,
37
38    /// Increase verbosity (-v, -vv, -vvv)
39    #[arg(short, long, global = true, action = clap::ArgAction::Count)]
40    pub verbose: u8,
41
42    /// Suppress non-essential output
43    #[arg(short, long, global = true)]
44    pub quiet: bool,
45}
46
47/// Resolve the starting path, defaulting to the process working directory.
48pub fn starting_path(path: Option<PathBuf>) -> std::io::Result<PathBuf> {
49    match path {
50        Some(path) => Ok(path),
51        None => std::env::current_dir(),
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58    use clap::CommandFactory;
59
60    #[test]
61    fn verify_cli_structure() {
62        // Validates the CLI definition is correct
63        Cli::command().debug_assert();
64    }
65
66    #[test]
67    fn parse_main_cli_without_subcommands() {
68        let cli = Cli::parse_from(["rusty-sweeper"]);
69        assert!(cli.path.is_none());
70        assert_eq!(
71            starting_path(cli.path).unwrap(),
72            std::env::current_dir().unwrap()
73        );
74        assert!(cli.config.is_none());
75        assert!(!cli.quiet);
76    }
77
78    #[test]
79    fn parse_main_cli_with_starting_path() {
80        let cli = Cli::parse_from(["rusty-sweeper", "/tmp/projects"]);
81        assert_eq!(cli.path, Some(PathBuf::from("/tmp/projects")));
82        assert_eq!(
83            starting_path(cli.path).unwrap(),
84            PathBuf::from("/tmp/projects")
85        );
86    }
87
88    #[test]
89    fn parse_main_cli_with_globals() {
90        let cli = Cli::parse_from(["rusty-sweeper", "--quiet", "--config", "/tmp/test.toml"]);
91        assert!(cli.quiet);
92        assert_eq!(cli.config, Some(PathBuf::from("/tmp/test.toml")));
93    }
94
95    #[test]
96    fn global_verbose_flag() {
97        let cli = Cli::parse_from(["rusty-sweeper", "-vvv"]);
98        assert_eq!(cli.verbose, 3);
99    }
100
101    #[test]
102    fn parse_report_mode() {
103        let cli = Cli::parse_from(["rusty-sweeper", "--cli", "--json", "/tmp"]);
104        assert!(cli.cli);
105        assert!(cli.json);
106        assert_eq!(cli.path, Some(PathBuf::from("/tmp")));
107    }
108
109    #[test]
110    fn report_options_require_cli() {
111        assert!(Cli::try_parse_from(["rusty-sweeper", "--json"]).is_err());
112        assert!(Cli::try_parse_from(["rusty-sweeper", "--depth", "1"]).is_err());
113    }
114}