Parse Vector of Vector arguments using Clap

⚓ Rust    📅 2025-06-29    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 41 days ago. The information described in this article may have changed.

Hi folks,

I figured out that using Clap I can parse a vector of enum (code below) and then run the program with --myarg first --myarg second --myarg first.

But is it possible to parse vector of vectors (Vec<Vec<MyEnum>> or Vec<Vec<String>>)? I'm not sure how exactly this would be specified as arguments but I'm OK with any style as long as I can get group of groups (vector of vectors).

Thank you.

#[derive(Clone, Debug, clap::ValueEnum)]
enum MyEnum {
    First,
    Second,
    Third,
}

#[derive(Clone, Debug, clap::Parser)]
#[command(name = "program", about = "program", rename_all = "snake_case")]
struct Xyz {
    #[arg(long, value_enum)]
    myarg: Vec<MyEnum>,
}

pub async fn runit() {
    let Xyz { myarg, .. } = <Xyz as clap::Parser>::parse();
    myarg.iter().enumerate().for_each(|(i, inp)| {
        println!("    {i}. {inp:?}");
    });
}

1 post - 1 participant

Read full topic

🏷️ rust_feed