Function bpaf::cargo_helper
source · [−]pub fn cargo_helper<P, T>(cmd: &'static str, parser: P) -> impl Parser<T> where
T: 'static,
P: Parser<T>,
Expand description
Strip a command name if present at the front when used as a cargo
command
When implementing a cargo subcommand parser needs to be able to skip the first argument which
is always the same as the executable name without cargo-
prefix. For example if executable name is
cargo-cmd
so first argument would be cmd
. cargo_helper
helps to support both invocations:
with name present when used via cargo and without it when used locally.
Combinatoric usage
fn options() -> OptionParser<(u32, u32)> {
let width = short('w').argument("PX").from_str::<u32>();
let height = short('h').argument("PX").from_str::<u32>();
let parser = construct!(width, height);
cargo_helper("cmd", parser).to_options()
}
Derive usage
If you pass a cargo command name as a parameter to options
annotation bpaf_derive
would generate cargo_helper
.
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options("cmd"))]
struct Options {
#[bpaf(short, argument("PX"))]
width: u32,
#[bpaf(short, argument("PX"))]
height: u32,
}
fn main() {
println!("{:?}", options().run());
}
Example
$ cargo cmd -w 3 -h 5
(3, 5)
$ cargo run --bin cargo-cmd -- -w 3 -h 5
(3, 5)