Idiomatic way to set an Option

Hello there! I'm using the clap library to parse command line arguments. Among others, I have an argument t which can be a list of values. Supposing my binary is called "bin", then an example call is bin -t foo bar with the values of -t being the list [foo, bar].

I'm representing such argument as a Option<Vec<String>> and I do initialize it as following:

let tags: Option<Vec<String>> = match arguments.values_of("tag") {
        Some(values) => Some(|s| s.to_string()).collect()),
        None => None

where arguments is the clap App struct. My question is: is this an idiomatic way to set up an Option value? For example, I don't really like the fact that I'm repeating Some/Some and None/None in the respective branches.

This is what the Option::map() method is for.

By the way, don't parse arguments by hand. Use structopt, which will directly parse the arguments into a strongly-typed config struct. (It is also based on clap.)

I didn't know about structopt, many thanks, I'll try using it!
And Option::map() is exactly what I was looking for! Thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.