1use clap::Parser;
2use std::path::PathBuf;
3
4#[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 #[arg(value_name = "PATH")]
12 pub path: Option<PathBuf>,
13
14 #[arg(long)]
16 pub cli: bool,
17
18 #[arg(long, requires = "cli", conflicts_with_all = ["json", "depth"])]
20 pub clean: bool,
21
22 #[arg(short = 'y', long, requires = "clean")]
24 pub yes: bool,
25
26 #[arg(long, requires = "cli")]
28 pub json: bool,
29
30 #[arg(long, requires = "cli", conflicts_with = "json")]
32 pub depth: Option<usize>,
33
34 #[arg(short, long, global = true, value_name = "PATH")]
36 pub config: Option<PathBuf>,
37
38 #[arg(short, long, global = true, action = clap::ArgAction::Count)]
40 pub verbose: u8,
41
42 #[arg(short, long, global = true)]
44 pub quiet: bool,
45}
46
47pub 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 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}