use bpaf::{any, construct, doc::Style, short, OptionParser, Parser};
use std::str::FromStr;
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct Options {
magic: bool,
in_file: String,
out_file: String,
block_size: usize,
}
fn tag<T>(name: &'static str, meta: &str, help: &'static str) -> impl Parser<T>
where
T: FromStr,
<T as std::str::FromStr>::Err: std::fmt::Display,
{
any("", move |s: String| Some(s.strip_prefix(name)?.to_owned()))
.metavar(&[(name, Style::Literal), (meta, Style::Metavar)][..])
.help(help)
.anywhere()
.parse(|s| s.parse())
}
fn in_file() -> impl Parser<String> {
tag::<String>("if=", "FILE", "read from FILE")
.fallback(String::from("-"))
.display_fallback()
}
fn out_file() -> impl Parser<String> {
tag::<String>("of=", "FILE", "write to FILE")
.fallback(String::from("-"))
.display_fallback()
}
fn block_size() -> impl Parser<usize> {
tag::<usize>("bs=", "SIZE", "read/write SIZE blocks at once")
.fallback(512)
.display_fallback()
}
pub fn options() -> OptionParser<Options> {
let magic = short('m')
.long("magic")
.help("a usual switch still works")
.switch();
construct!(Options {
magic,
in_file(),
out_file(),
block_size(),
})
.to_options()
}
fn main() {
println!("{:#?}", options().run());
}