-
I'm trying to build on the chained command example and add support for a command with variable number of positional args: use bpaf::Bpaf;
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub struct Options {
#[bpaf(external(cmd), many)]
pub commands: Vec<Cmd>,
}
#[derive(Debug, Clone, Bpaf)]
pub enum Cmd {
#[bpaf(command, adjacent)]
Layer {
#[bpaf(positional("LID"))]
id: Vec<isize>,
},
#[bpaf(command, adjacent)]
Print,
}
fn main() {
println!("{:?}", options().run())
} I'm expecting to be able to parse something like
Is there a way to make this pattern work? I guess the workaround would be to use a syntax like |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Trying the workaround suggested by the /// To parse comma separated values it's easier to treat them as strings
use bpaf::*;
use std::{num::ParseIntError, str::FromStr};
fn split_and_parse(s: String) -> Result<Vec<u16>, ParseIntError> {
s.split(',')
.map(u16::from_str)
.collect::<Result<Vec<_>, _>>()
}
fn flatten_vec(vv: Vec<Vec<u16>>) -> Vec<u16> {
vv.into_iter().flatten().collect()
}
#[derive(Debug, Clone, Bpaf)]
#[allow(dead_code)]
struct Opts {
#[bpaf(positional("PORTS"), parse(split_and_parse), many, map(flatten_vec))]
/// Comma separated list of ports
ports: Vec<u16>,
}
fn main() {
println!("{:?}", opts().to_options().run());
} This fails to compile. |
Beta Was this translation helpful? Give feedback.
-
For that parser for ids need to be able to recover from parsing failure. having something like https://docs.rs/bpaf/0.9.11/bpaf/parsers/struct.ParseMany.html#method.catch |
Beta Was this translation helpful? Give feedback.
For that parser for ids need to be able to recover from parsing failure. having something like
#[bpaf(positional("LID"), many, catch)]
might help.https://docs.rs/bpaf/0.9.11/bpaf/parsers/struct.ParseMany.html#method.catch