Skip to main content

directory_indexer/cli/
args.rs

1// CLI argument parsing structures and utilities
2
3use clap::Parser;
4
5// This module will contain additional argument parsing utilities
6// when needed for more complex CLI interactions
7
8#[derive(Parser, Debug)]
9pub struct CommonArgs {
10    /// Enable verbose logging
11    #[arg(short, long, global = true)]
12    pub verbose: bool,
13
14    /// Path to configuration file
15    #[arg(short, long, global = true)]
16    pub config: Option<String>,
17}
18
19#[cfg(test)]
20mod tests {
21    use super::*;
22    use clap::CommandFactory;
23
24    #[test]
25    fn test_common_args_default_values() {
26        let args = CommonArgs {
27            verbose: false,
28            config: None,
29        };
30
31        assert!(!args.verbose);
32        assert!(args.config.is_none());
33    }
34
35    #[test]
36    fn test_common_args_with_values() {
37        let args = CommonArgs {
38            verbose: true,
39            config: Some("/path/to/config.toml".to_string()),
40        };
41
42        assert!(args.verbose);
43        assert_eq!(args.config, Some("/path/to/config.toml".to_string()));
44    }
45
46    #[test]
47    fn test_common_args_debug_format() {
48        let args = CommonArgs {
49            verbose: true,
50            config: Some("test.toml".to_string()),
51        };
52
53        let debug_output = format!("{args:?}");
54        assert!(debug_output.contains("verbose: true"));
55        assert!(debug_output.contains("test.toml"));
56    }
57
58    #[test]
59    fn test_common_args_can_build_command() {
60        // Test that the CommonArgs can be used to build a clap command
61        let command = CommonArgs::command();
62        assert_eq!(command.get_name(), "directory-indexer");
63
64        // Check that the arguments are properly defined
65        let args: Vec<_> = command.get_arguments().collect();
66        assert!(args.iter().any(|arg| arg.get_id() == "verbose"));
67        assert!(args.iter().any(|arg| arg.get_id() == "config"));
68    }
69
70    #[test]
71    fn test_verbose_flag_properties() {
72        let command = CommonArgs::command();
73        let verbose_arg = command
74            .get_arguments()
75            .find(|arg| arg.get_id() == "verbose")
76            .expect("verbose argument should exist");
77
78        assert!(verbose_arg.get_short() == Some('v'));
79        assert!(verbose_arg.get_long() == Some("verbose"));
80        assert!(verbose_arg.is_global_set());
81        assert!(verbose_arg.get_action().takes_values() == false); // It's a flag, not a value
82    }
83
84    #[test]
85    fn test_config_arg_properties() {
86        let command = CommonArgs::command();
87        let config_arg = command
88            .get_arguments()
89            .find(|arg| arg.get_id() == "config")
90            .expect("config argument should exist");
91
92        assert!(config_arg.get_short() == Some('c'));
93        assert!(config_arg.get_long() == Some("config"));
94        assert!(config_arg.is_global_set());
95        assert!(config_arg.get_action().takes_values() == true); // It takes a value
96    }
97}