I would like to have a command such that do_something --list 1 2 3
would result in the field in the struct being set to [1, 2, 3]
.
The following code works for do_something --list 1 --list 2 --list 3
:
use clap::Parser; // 3.2.8
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct Cli {
#[clap(short, long, value_parser)]
pub list: Option<Vec<i32>>,
}
fn main() {
let cli = Cli::parse();
println!("CLI is {:#?}", cli);
}
When I use --list 1 2 3
, it gives me the error:
error: Found argument '2' which wasn't expected, or isn't valid in this context
I've also tried --list "1 2 3"
and --list 1,2,3
but get errors for those as well.
I was also able to get multiple values to work as a positional argument, but not as a Option
with a flag.
Is --list 1 2 3
something that clap supports? I thought this was supported by clap's multiple values. Is there something missing in my setup/code or my command line input?
You're looking for the use_value_delimiter
setting. Set use_value_delimiter = true
and set the actual delimiter to use with value_delimiter = ','
.