cargo_cmd/
cargo-cmd.rs

1//! When implementing a cargo subcommand parser needs to be able to skip the first argument which
2//! is always the same as the executable name. For this example executable name is `cargo-cmd` so
3//! first argument would be `cmd`. A way to support both cases - when it's present and it's absent
4//! would be to use a combination of `literal`, `optional` and `hide`.
5//! `bpaf` also provides a helper `cargo_helper` that does exactly this.
6
7use bpaf::*;
8
9#[derive(Debug, Clone)]
10#[allow(dead_code)]
11struct Opts {
12    width: usize,
13    height: usize,
14}
15
16fn main() {
17    // defining a parser in a usual way
18    let width = short('w').argument::<usize>("WIDTH").fallback(10);
19    let height = short('h').argument::<usize>("HEIGHT").fallback(10);
20    let parser = construct!(Opts { width, height });
21
22    let cmd = literal("cmd").optional().hide();
23    let combined_parser = construct!(cmd, parser).map(|x| x.1);
24
25    let opts = combined_parser.to_options().run();
26
27    println!("{:?}", opts);
28}