many_comma_separated_args_derive/
many_comma_separated_args_derive.rs

1/// To parse comma separated values it's easier to treat them as strings
2use bpaf::*;
3use std::{num::ParseIntError, str::FromStr};
4
5fn split_and_parse(s: String) -> Result<Vec<u16>, ParseIntError> {
6    s.split(',')
7        .map(u16::from_str)
8        .collect::<Result<Vec<_>, _>>()
9}
10
11fn flatten_vec(vv: Vec<Vec<u16>>) -> Vec<u16> {
12    vv.into_iter().flatten().collect()
13}
14
15#[derive(Debug, Clone, Bpaf)]
16#[allow(dead_code)]
17struct Opts {
18    #[bpaf(
19        long,
20        argument::<String>("PORTS"),
21        parse(split_and_parse),
22        many,
23        map(flatten_vec)
24    )]
25    /// Comma separated list of ports
26    ports: Vec<u16>,
27}
28
29fn main() {
30    println!("{:?}", opts().to_options().run());
31}