dazzle/
args.rs

1//! Command-line argument parsing
2
3use clap::Parser;
4use std::path::PathBuf;
5
6/// Dazzle: A Rust-based code generation tool powered by Scheme templates
7#[derive(Parser, Debug)]
8#[command(name = "dazzle")]
9#[command(about, long_about = None, disable_version_flag = true)]
10pub struct Args {
11    /// Template file (.scm)
12    #[arg(short = 'd', long = "template", required = true)]
13    pub template: PathBuf,
14
15    /// Backend selection (xml or text)
16    #[arg(short = 't', long = "backend", default_value = "text")]
17    pub backend: String,
18
19    /// Template variables (key=value pairs)
20    #[arg(short = 'V', long = "var", value_parser = parse_key_val)]
21    pub variables: Vec<(String, String)>,
22
23    /// Template search directories
24    #[arg(short = 'D', long = "dir")]
25    pub search_dirs: Vec<PathBuf>,
26
27    /// Input XML file
28    #[arg(required = true)]
29    pub input: PathBuf,
30
31    /// Enable verbose output
32    #[arg(short, long)]
33    pub verbose: bool,
34}
35
36/// Parse a single key=value pair
37fn parse_key_val(s: &str) -> Result<(String, String), String> {
38    let pos = s
39        .find('=')
40        .ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
41    Ok((s[..pos].to_string(), s[pos + 1..].to_string()))
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47
48    #[test]
49    fn test_parse_key_val() {
50        let (k, v) = parse_key_val("foo=bar").unwrap();
51        assert_eq!(k, "foo");
52        assert_eq!(v, "bar");
53    }
54
55    #[test]
56    fn test_parse_key_val_with_equals_in_value() {
57        let (k, v) = parse_key_val("key=val=ue").unwrap();
58        assert_eq!(k, "key");
59        assert_eq!(v, "val=ue");
60    }
61}