Clap: custom collection instead of Vec<T>

Hello Everybody,

I know I can parse multiple arguments in Clap into Vec<T> - for example clap/examples/escaped-positional-derive.rs at master · clap-rs/clap · GitHub

I also know I can use #[arg(last = true, value_parser = |inp: &str| ... with a function that will be applied on each T in Vec<T>.

But I can't figure out if I can somehow pass the whole collection/iterator to a function. For example if I want to get VecDeque<T> without converting it afterwards from Vec<T>. Is that possible?

Thank you.

I managed to come up with this, so it's at least possible. There may be a better/cleaner approach that I don't know about, though.

3 Likes

Thank you @2e71828 , it's great to see how to implement this.

Here's a generic variant based on the one from @2e71828:

The bound on Any is not necessary. By definition, everything implements it :grinning_face_with_smiling_eyes:.

I know, most types implement it. I just copied the trait bounds from get_many().

Most types implement Any. However, any type which contains a non-'static reference does not.

1 Like

Any has 'static as a bound on Self, and is implemented for all T: ?Sized + 'static, so you only need one of + Any or + 'static and the other can be implied.

1 Like

Indeed. Alas, I'm not a maintainer of clap. (This is my private account)

It's no big deal, just throwing a tidbit out there.

2 Likes