struargs

Crates.iostruargs
lib.rsstruargs
version0.1.1
created_at2025-10-20 10:45:48.47163+00
updated_at2025-10-20 11:38:16.314105+00
descriptionA macro that converts a structure into a Command parameter list
homepage
repositoryhttps://github.com/yk0n9/struargs
max_upload_size
id1891736
size6,210
Ykong (yk0n9)

documentation

https://docs.rs/struargs

README

struargs

A macro that builds a structure into a Command parameter list

Example

use struargs::Args;

#[derive(Debug, Args)]
struct StructArg {
    size: Option<i32>,
    name: Option<String>,
    #[args(rename = "type")]
    ty: Option<String>,
    num: f32,
}

let s = StructArg {
    size: None,
    name: Some("123".to_string()),
    ty: Some("Arg".to_string()),
    num: 100.1,
};

assert_eq!(
    s.args(),
    vec![
        "--name".to_string(),
        123.to_string(),
        "--type".to_string(),
        "Arg".to_string(),
        "--num".to_string(),
        "100.1".to_string(),
    ]
);

it expand to (all field must impl Display)

impl ::struargs::Args for StructArg {
    fn args(&self) -> Vec<String> {
        let mut args = ::alloc::vec::Vec::new();
        if let Some(ref arg) = self.size {
            args.extend(["--size".to_string(), arg.to_string()]);
        }
        if let Some(ref arg) = self.name {
            args.extend(["--name".to_string(), arg.to_string()]);
        }
        if let Some(ref arg) = self.ty {
            args.extend(["--type".to_string(), arg.to_string()]);
        }
        args.extend(["--num".to_string(), self.num.to_string()]);
        args
    }
}

Args

  • rename (custom)
Commit count: 0

cargo fmt