-
I am trying to make a cli with a two level command structure. something like: prog game new [--opponent=] each level should be mutually exclusive (if new is given, no other commands are possible) I think I need a toplevel enum (game/login/server) followed by a second level enum (for game: new/status/etc) following by the options for that command. I struggle how to approach this |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
This is a great start. You analyze how bits of the result relate to each other and come up with a structure - top level enum with some enums and structs inside. This is how #[derive(Debug, Clone)]
enum Server {
Status,
Restart,
} You want it to be command #[derive(Debug, Clone, Bpaf)]
#[bpaf(command)]
enum Server {
#[bpaf(command)] // implicit command name - it gets derived from enum field name
Status,
#[bpaf(command("restart"))] // explicit command name
Restart,
} Here I'm using a mix of explicit and implicit names - up to you what you prefer. This gives you a function use bpaf::*;
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options, version, fallback(Options::Dummy))]
enum Options {
Game(#[bpaf(external(game))] Game), // explicit name with tuple struct
Server {
#[bpaf(external)] // or implicit name with named struct
server: Server,
},
#[bpaf(hide)] // if you want to run something by default, or you can drop this branch
Dummy,
}
#[derive(Debug, Clone, Bpaf)]
#[bpaf(command("game"))]
enum Game {
#[bpaf(command("new"))]
New { opponent: Option<String> },
#[bpaf(command("status"))]
Status,
}
#[derive(Debug, Clone, Bpaf)]
#[bpaf(command)]
enum Server {
#[bpaf(command)]
Status,
#[bpaf(command)]
Restart,
}
fn main() {
println!("{:?}", options().run());
} Add some doc comments on top of that, any missing fields and it should be good to go. |
Beta Was this translation helpful? Give feedback.
-
#[bpaf(short, long, argument("NOTIFY"))]
/// mail notifications when game changes occur
notify: Option<bool>, This bit looks a bit odd. As far as I remember deriving logic it can only return either |
Beta Was this translation helpful? Give feedback.
This is a great start. You analyze how bits of the result relate to each other and come up with a structure - top level enum with some enums and structs inside.
This is how
enum
for server might look likeYou want it to be command
"server"
on the outside so any bit of the parser can succeed and"status"
/"restart"
on the inside so you can passprog server status
/prog server restart
: