use bpaf::*;
#[derive(Debug, Clone)]
enum Command {
Simple,
Complex1(String, i32),
Complex2(String, i16),
}
#[derive(Debug, Clone)]
enum PreCommand {
Simple,
Complex1(i32),
Complex2(i16),
}
fn main() {
let token = long("token")
.help("Token used for complex commands")
.argument::<String>("TOKEN")
.optional();
let simple_parser = pure(PreCommand::Simple).to_options();
let simple = command("simple", simple_parser);
let complex1_parser = positional::<i32>("ARG");
let complex1 = command(
"complex1",
construct!(PreCommand::Complex1(complex1_parser))
.to_options()
.descr("This is complex command 1"),
);
let complex2_parser = positional::<i16>("ARG");
let complex2 = command(
"complex1",
construct!(PreCommand::Complex2(complex2_parser))
.to_options()
.descr("This is complex command 2"),
);
let preparser = construct!([simple, complex1, complex2]);
let parser = construct!(token, preparser).parse(|(token, cmd)| match cmd {
PreCommand::Simple => Ok(Command::Simple),
PreCommand::Complex1(a) => match token {
Some(token) => Ok(Command::Complex1(token, a)),
None => Err("You must specify token to use with --token"),
},
PreCommand::Complex2(a) => match token {
Some(token) => Ok(Command::Complex2(token, a)),
None => Err("You must specify token to use with --token"),
},
});
let cmd = parser.to_options().run();
println!("{:?}", cmd);
}