Skip to content

Instantly share code, notes, and snippets.

@baronfel
Created May 10, 2024 15:41
Show Gist options
  • Save baronfel/013ecc8a26c68664e9c65e4f62db33bc to your computer and use it in GitHub Desktop.
Save baronfel/013ecc8a26c68664e9c65e4f62db33bc to your computer and use it in GitHub Desktop.
Clap example showing multiple options with multiple tokens per opsion
use clap::Arg;
use clap::ArgAction;
use clap::Command;
fn main() {
let cmd = Command::new("hello").arg(
Arg::new("name")
.long("name")
.num_args(1..) // allow multiple tokens
.action(ArgAction::Append), // when you get multiple tokens, jam them together into a Vec (List to us)
);
let names: Vec<String> = cmd
.get_matches_from(vec!["hello", "--name", "a", "b", "c", "--name", "d"])
.get_many::<String>("name")
.unwrap()
.map(String::from)
.collect();
assert_eq!(names, vec!["a", "b", "c", "d"]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment