pub fn extract_model(cmdline: &[String]) -> Option<String> {
for (i, tok) in cmdline.iter().enumerate().skip(1) {
if tok == "--model" {
return cmdline.get(i + 1).map(|s| s.to_string());
}
if let Some(rest) = tok.strip_prefix("--model=") {
return Some(rest.to_string());
}
}
None
}
pub fn display_model(full: &str) -> &str {
match full.rsplit_once('/') {
Some((_, last)) => last,
None => full,
}
}
pub fn resolve_model(cmdline: &[String]) -> Option<String> {
extract_model(cmdline).map(|m| display_model(&m).to_string())
}
#[cfg(test)]
mod tests {
use super::*;
fn argv(parts: &[&str]) -> Vec<String> {
parts.iter().map(|s| s.to_string()).collect()
}
#[test]
fn separate_flag_form() {
let cmd = argv(&["opencode", "--model", "zai-coding-plan/glm-5.2"]);
assert_eq!(
extract_model(&cmd).as_deref(),
Some("zai-coding-plan/glm-5.2")
);
assert_eq!(resolve_model(&cmd).as_deref(), Some("glm-5.2"));
}
#[test]
fn joined_flag_form() {
let cmd = argv(&["claude", "--model=opus"]);
assert_eq!(extract_model(&cmd).as_deref(), Some("opus"));
assert_eq!(resolve_model(&cmd).as_deref(), Some("opus"));
}
#[test]
fn joined_flag_form_with_prefix() {
let cmd = argv(&["opencode", "--model=zai-coding-plan/glm-5.2"]);
assert_eq!(resolve_model(&cmd).as_deref(), Some("glm-5.2"));
}
#[test]
fn model_with_no_prefix_stays_intact() {
assert_eq!(display_model("opus"), "opus");
assert_eq!(display_model("gpt-4o"), "gpt-4o");
}
#[test]
fn model_with_nested_prefix_strips_to_last_segment() {
assert_eq!(display_model("a/b/c"), "c");
assert_eq!(display_model("anthropic/claude-3-opus"), "claude-3-opus");
}
#[test]
fn no_model_flag_returns_none() {
let cmd = argv(&["claude", "--verbose", "-p"]);
assert!(extract_model(&cmd).is_none());
assert!(resolve_model(&cmd).is_none());
}
#[test]
fn model_flag_at_end_with_no_value() {
let cmd = argv(&["opencode", "--model"]);
assert!(extract_model(&cmd).is_none());
}
#[test]
fn model_flag_among_other_flags() {
let cmd = argv(&[
"claude",
"--dangerously-skip-permissions",
"--model",
"opus",
"--effort",
"xhigh",
]);
assert_eq!(resolve_model(&cmd).as_deref(), Some("opus"));
}
#[test]
fn only_first_model_flag_wins() {
let cmd = argv(&["opencode", "--model", "opus", "--model", "sonnet"]);
assert_eq!(resolve_model(&cmd).as_deref(), Some("opus"));
}
#[test]
fn empty_cmdline_returns_none() {
assert!(extract_model(&[]).is_none());
assert!(resolve_model(&[]).is_none());
}
#[test]
fn model_flag_in_argv0_position_ignored() {
let cmd = argv(&["--model"]);
assert!(extract_model(&cmd).is_none());
}
}