shared_args/
shared_args.rs

1//! It is possible to have shared args returned alongside a subcommand
2
3use bpaf::*;
4
5#[derive(Debug, Clone, Bpaf)]
6#[allow(dead_code)]
7struct Action {
8    verbose: bool,
9    number: u32,
10}
11
12#[derive(Debug, Clone, Bpaf)]
13#[allow(dead_code)]
14struct Build {
15    verbose: bool,
16}
17
18#[derive(Debug, Clone)]
19enum Command {
20    Action(Action),
21    Build(Build),
22}
23
24fn shared() -> impl Parser<Vec<String>> {
25    positional("ARG").many()
26}
27
28fn parse_command() -> impl Parser<(Command, Vec<String>)> {
29    let action = action().map(Command::Action);
30    let action = construct!(action, shared()).to_options().command("action");
31    let build = build().map(Command::Build);
32    let build = construct!(build, shared()).to_options().command("build");
33    construct!([action, build])
34}
35
36fn main() {
37    let opts = parse_command().to_options().run();
38
39    println!("{:?}", opts);
40}