rustclap

Using multiple values of an argument in Clap


I am using Clap and my YAML file has the following:

args:
- DIRECTORY
    help: one or more directories
    required: true
    multiple: true

In my main.rs, I want to get the name of each of the directory passed as an argument and do something like

dir_names.push(name_of_the_directory);

where dir_names is a vector and name_of_the_directory is a string slice.

How do I proceed?


Solution

  • You can do it by using the values_of method:

    let dir_names: Vec<&str> = m.values_of("output").unwrap().collect();
    

    In current version of clap (4.4.7), this has been replaced by get_many:

    let dir_names: Vec<&str> = m.get_many ("output").unwrap().collect();