fix: implement FromStr for NumberOrString and KeyValue to fix clap parsing errors

Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-08-14 17:03:55 -03:00
parent 6af1ac30df
commit 2dfaed38b8

View File

@@ -1,7 +1,8 @@
use std::path::PathBuf; use std::path::PathBuf;
use std::str::FromStr;
use clap::*; use clap::*;
use serde::{Deserialize, Serialize}; use anyhow::{Error, anyhow};
/** /**
* Main struct for command-line arguments. * Main struct for command-line arguments.
@@ -17,6 +18,7 @@ pub struct Args {
pub options: OptionsArgs, pub options: OptionsArgs,
#[arg(help("A list of either item IDs or tags"))] #[arg(help("A list of either item IDs or tags"))]
#[arg(value_parser = clap::value_parser!(NumberOrString))]
pub ids_or_tags: Vec<NumberOrString>, pub ids_or_tags: Vec<NumberOrString>,
} }
@@ -75,6 +77,7 @@ pub struct ItemArgs {
#[arg(help( #[arg(help(
"Set metadata for the item using the format KEY=[VALUE], the metadata will be removed if VALUE is not provided" "Set metadata for the item using the format KEY=[VALUE], the metadata will be removed if VALUE is not provided"
))] ))]
#[arg(value_parser = clap::value_parser!(KeyValue))]
pub meta: Vec<KeyValue>, pub meta: Vec<KeyValue>,
#[arg(help_heading("Item Options"), long, env("KEEP_DIGEST"))] #[arg(help_heading("Item Options"), long, env("KEEP_DIGEST"))]
@@ -141,6 +144,15 @@ pub enum NumberOrString {
Str(String), Str(String),
} }
impl FromStr for NumberOrString {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(s.parse::<i64>()
.map(NumberOrString::Number)
.unwrap_or_else(|_| NumberOrString::Str(s.to_string())))
}
}
/** /**
* Struct for key-value pairs. * Struct for key-value pairs.
*/ */
@@ -149,3 +161,16 @@ pub struct KeyValue {
pub key: String, pub key: String,
pub value: String, pub value: String,
} }
impl FromStr for KeyValue {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.split_once('=') {
Some(kv) => Ok(KeyValue {
key: kv.0.to_string(),
value: kv.1.to_string(),
}),
None => Err(anyhow!("Unable to parse key=value pair")),
}
}
}