Repository navigation
Nested subcommands? #456
Answered
by
pacak
GoldsteinE
asked this question in
Q&A
|
hi! I’m trying to make an API like this: a natural data type would be something like: but I can’t figure out how to annotate it with bpaf attributes so that it works. |
Answered by
pacak
Jun 22, 2026
Replies: 2 comments 1 reply
|
You want something like this: use bpaf::*;
#[derive(Debug, Copy, Clone, Bpaf)]
enum Foo {
#[bpaf(command)]
Meow,
#[bpaf(command)]
Bark,
}
#[derive(Debug, Copy, Clone, Bpaf)]
#[bpaf(options)]
enum Toplevel {
#[bpaf(command)] // "foo" is a command, but its body is `external(foo)`
Foo(#[bpaf(external(foo))] Foo),
#[bpaf(command)]
Bar,
}
fn main() {
let r = toplevel().fallback_to_usage().run();
match r {
Toplevel::Foo(f) => match f {
Foo::Meow => println!("Meow!"),
Foo::Bark => println!("Bark"),
},
Toplevel::Bar => println!("bar!"),
}
}
Can you share this example? |
0 replies
Answer selected by
GoldsteinE
|
thanks for the response!
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub(crate) enum Foo {
#[bpaf(command)]
Meow,
}
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options)]
pub(crate) enum Outer {
#[bpaf(command)]
Foo(Foo),
} |
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You want something like this: