7

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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Marvin Mednick
  • 179
  • 1
  • 7
  • Try adding `use_value_delimiter = true` – PitaJ Aug 04 '22 at 18:50
  • 1
    Thank you! -- that did it. I was able to use either use_value_delimiter=true or value_delimiter=',' for comma separated (--list 1,2,3) , and value_delimter=" " allowed --list "1 2 3" – Marvin Mednick Aug 04 '22 at 19:12

2 Answers2

10

You're looking for the use_value_delimiter setting. Set use_value_delimiter = true and set the actual delimiter to use with value_delimiter = ','.

PitaJ
  • 12,969
  • 6
  • 36
  • 55
6

In clap 4.2.7, use_value_delimiter is deprecated, which means the accepted answer no longer works, instead, you should use num_args = 1..

use clap::Parser; // 4.2.7

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
pub struct Cli {
    #[clap(short, long, value_delimiter = ' ', num_args = 1..)]
    pub list: Option<Vec<i32>>,
}

fn main() {
    let cli = Cli::parse();
    println!("CLI is {:#?}", cli);
}
$ cargo b
$ ./target/debug/rust --list 1 2 3
CLI is Cli {
    list: Some(
        [
            1,
            2,
            3,
        ],
    ),
}
Steve Lau
  • 658
  • 7
  • 13