many_comma_separated_args/
many_comma_separated_args.rs

1/// To parse comma separated values it's easier to treat them as strings
2use bpaf::*;
3use std::str::FromStr;
4
5// --ports 1,2,3 --ports 4,5   => [1,2,3,4,5]
6fn args() -> impl Parser<Vec<u16>> {
7    long("ports")
8        .help("Comma separated list of ports")
9        .argument::<String>("PORTS")
10        .parse(|s| {
11            s.split(',')
12                .map(u16::from_str)
13                .collect::<Result<Vec<_>, _>>()
14        })
15        .many()
16        .map(|nested| nested.into_iter().flatten().collect())
17}
18
19fn main() {
20    println!("{:?}", args().to_options().run());
21}