#[derive(Debug, Clone)]
pub struct Args {
pub tool: Option<String>,
pub subcommand: Option<String>,
pub key: Option<String>,
pub url: Option<String>,
pub model: Option<String>,
pub preset: Option<String>,
pub ccswitch: bool,
pub overwrite: bool,
pub switch: Option<bool>,
pub help: bool,
pub version: bool,
}
pub fn parse(args: &[String]) -> Args {
let mut result = Args {
tool: None,
subcommand: None,
key: None,
url: None,
model: None,
preset: None,
ccswitch: true,
overwrite: false,
switch: None,
help: false,
version: false,
};
let mut i = 0;
if let Some(a) = args.first().filter(|a| !a.starts_with('-')) {
result.tool = Some(a.to_lowercase());
i = 1;
}
if let Some(a) = args.get(i).filter(|a| !a.starts_with('-')) {
result.subcommand = Some(a.to_lowercase());
i += 1;
}
while i < args.len() {
let next = args.get(i + 1).cloned();
match args[i].as_str() {
"--help" | "-h" => result.help = true,
"--version" | "-v" | "-V" => result.version = true,
"--no-ccswitch" => result.ccswitch = false,
"--overwrite" => result.overwrite = true,
"--switch" => result.switch = Some(true),
"--no-switch" => result.switch = Some(false),
"--key" | "-k" if next.is_some() => {
result.key = next;
i += 1;
}
"--url" | "-u" if next.is_some() => {
result.url = next;
i += 1;
}
"--model" | "-m" if next.is_some() => {
result.model = next;
i += 1;
}
"--preset" | "-p" if next.is_some() => {
result.preset = next;
i += 1;
}
_ => {}
}
i += 1;
}
result
}
#[cfg(test)]
mod tests {
use super::*;
fn p(s: &str) -> Args {
parse(&s.split_whitespace().map(String::from).collect::<Vec<_>>())
}
#[test]
fn parses_tool_and_flags() {
let a = p("CC test -m claude-x --overwrite --no-ccswitch --switch");
assert_eq!(a.tool.as_deref(), Some("cc"));
assert_eq!(a.subcommand.as_deref(), Some("test"));
assert_eq!(a.model.as_deref(), Some("claude-x"));
assert!(a.overwrite && !a.ccswitch && a.switch == Some(true));
assert_eq!(p("cc --no-switch").switch, Some(false));
let b = p("cx -k sk -u https://x");
assert_eq!(b.subcommand, None);
assert_eq!(b.key.as_deref(), Some("sk"));
assert_eq!(b.url.as_deref(), Some("https://x"));
assert!(p("--help").help && p("-V").version && p("").tool.is_none());
}
}