Skip to main content

bindings_generat/
cli.rs

1use clap::Parser;
2use std::path::PathBuf;
3
4/// Automatically generate safe, idiomatic Rust wrapper crates from C/C++ libraries
5#[derive(Parser, Debug, Clone)]
6#[command(name = "bindings-generat")]
7#[command(version, about, long_about = None)]
8pub struct Cli {
9    /// Path or URL to C/C++ library source
10    ///
11    /// Can be:
12    /// - A directory containing headers and libraries
13    /// - A local archive file (.zip, .tar.gz, .tar, .gz)
14    /// - A URL to a remote archive (http:// or https://)
15    ///
16    /// Archives are automatically extracted to a temporary directory.
17    #[arg(value_name = "SOURCE")]
18    pub source: String,
19
20    /// Output directory for generated crate (default: ./bindings-output)
21    #[arg(short = 'o', long, value_name = "PATH")]
22    pub output: Option<PathBuf>,
23
24    /// Specific headers to include (glob pattern)
25    #[arg(long, value_name = "GLOB")]
26    pub headers: Option<String>,
27
28    /// Library name override (default: auto-detect)
29    #[arg(long, value_name = "NAME")]
30    pub lib_name: Option<String>,
31
32    /// LLM model for Ollama (default: deepseek-coder:6.7b)
33    #[arg(long, value_name = "MODEL", default_value = "deepseek-coder:6.7b")]
34    pub model: String,
35
36    /// Skip LLM enhancement (faster, less documentation)
37    #[arg(long)]
38    pub no_llm: bool,
39
40    /// Ask questions for ambiguous cases
41    #[arg(short = 'i', long)]
42    pub interactive: bool,
43
44    /// Make best guesses, no questions
45    #[arg(long)]
46    pub non_interactive: bool,
47
48    /// Code style profile (minimal/ergonomic/zero-cost)
49    #[arg(long, value_name = "PROFILE", default_value = "ergonomic")]
50    pub style: String,
51
52    /// LLM response cache location
53    #[arg(long, value_name = "PATH")]
54    pub cache_dir: Option<PathBuf>,
55
56    /// Show what would be generated without writing files
57    #[arg(long)]
58    pub dry_run: bool,
59
60    /// Verbose output
61    #[arg(short = 'v', long)]
62    pub verbose: bool,
63}
64
65impl Cli {
66    /// Get the output directory, using default if not specified
67    pub fn get_output_dir(&self) -> PathBuf {
68        self.output
69            .clone()
70            .unwrap_or_else(|| PathBuf::from("./bindings-output"))
71    }
72
73    /// Validate CLI arguments
74    pub fn validate(&self) -> anyhow::Result<()> {
75        // Validate source (only if it's not a URL)
76        if !self.source.starts_with("http://") && !self.source.starts_with("https://") {
77            let path = PathBuf::from(&self.source);
78            if !path.exists() {
79                anyhow::bail!("Source path does not exist: {}", self.source);
80            }
81        }
82
83        let output_path = self.get_output_dir();
84        if output_path.exists() && !self.dry_run {
85            anyhow::bail!(
86                "Output path already exists: {}. Please choose a different location or remove the existing directory.",
87                output_path.display()
88            );
89        }
90
91        if self.interactive && self.non_interactive {
92            anyhow::bail!("Cannot use both --interactive and --non-interactive");
93        }
94
95        Ok(())
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    fn test_get_output_dir_default() {
105        let cli = Cli {
106            source: "test.zip".to_string(),
107            output: None,
108            headers: None,
109            lib_name: None,
110            model: "qwen2.5-coder:1.5b".to_string(),
111            no_llm: false,
112            interactive: false,
113            non_interactive: false,
114            style: "ergonomic".to_string(),
115            cache_dir: None,
116            dry_run: false,
117            verbose: false,
118        };
119
120        assert_eq!(cli.get_output_dir(), PathBuf::from("./bindings-output"));
121    }
122
123    #[test]
124    fn test_get_output_dir_custom() {
125        let cli = Cli {
126            source: "test.zip".to_string(),
127            output: Some(PathBuf::from("/custom/path")),
128            headers: None,
129            lib_name: None,
130            model: "qwen2.5-coder:1.5b".to_string(),
131            no_llm: false,
132            interactive: false,
133            non_interactive: false,
134            style: "ergonomic".to_string(),
135            cache_dir: None,
136            dry_run: false,
137            verbose: false,
138        };
139
140        assert_eq!(cli.get_output_dir(), PathBuf::from("/custom/path"));
141    }
142}