Created
May 10, 2024 15:41
-
-
Save baronfel/013ecc8a26c68664e9c65e4f62db33bc to your computer and use it in GitHub Desktop.
Clap example showing multiple options with multiple tokens per opsion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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