use crate::config::option::OptionValue;
pub fn parse_kv_arg(arg: &str) -> Option<(&str, &str)> {
let stripped = arg.strip_prefix("--")?;
if let Some((k, v)) = stripped.split_once('=') {
return Some((k, v));
}
if let Some((k, v)) = stripped.split_once(':') {
return Some((k, v));
}
None
}
pub fn detect_value_type(value: &str) -> Option<OptionValue> {
let trimmed = value.trim();
if trimmed.is_empty() {
return Some(OptionValue::None);
}
if trimmed == "true" || trimmed == "yes" || trimmed == "on" {
return Some(OptionValue::Bool(true));
}
if trimmed == "false" || trimmed == "no" || trimmed == "off" {
return Some(OptionValue::Bool(false));
}
if trimmed.starts_with('[') && trimmed.ends_with(']') {
let inner = &trimmed[1..trimmed.len() - 1];
let items: Vec<String> = inner
.split(',')
.map(|s| {
let item = s.trim();
if (item.starts_with('\'') && item.ends_with('\''))
|| (item.starts_with('"') && item.ends_with('"'))
{
&item[1..item.len() - 1]
} else {
item
}
.to_string()
})
.filter(|s| !s.is_empty())
.collect();
return Some(OptionValue::List(items));
}
if (trimmed.starts_with('"') && trimmed.ends_with('"'))
|| (trimmed.starts_with('\'') && trimmed.ends_with('\''))
{
return Some(OptionValue::Str(trimmed[1..trimmed.len() - 1].to_string()));
}
if let Some(neg) = trimmed.strip_prefix('-')
&& neg.parse::<i64>().is_ok()
{
return Some(OptionValue::Int(-neg.parse::<i64>().unwrap()));
}
if trimmed.parse::<usize>().is_ok() {
return Some(OptionValue::Usize(trimmed.parse::<usize>().unwrap()));
}
if trimmed.parse::<f64>().is_ok() {
return Some(OptionValue::Float(trimmed.parse::<f64>().unwrap()));
}
Some(OptionValue::Str(trimmed.to_string()))
}