| Crates.io | clap-handler |
| lib.rs | clap-handler |
| version | 0.1.2 |
| created_at | 2022-06-22 05:11:54.310765+00 |
| updated_at | 2025-03-27 15:34:44.330216+00 |
| description | A command handler works with `clap-derive` to generating subcommand handlers. |
| homepage | |
| repository | https://github.com/ProjectAnni/clap-handler |
| max_upload_size | |
| id | 610662 |
| size | 28,640 |
A command handler works with clap-derive to generating subcommand handlers.
use clap::{Parser, AppSettings};
use clap_handler::{Handler, handler, Context};
#[derive(Parser, Handler, Debug, Clone)]
#[clap(name = "Your program", author)]
pub struct Arguments {
#[clap(subcommand)]
subcommand: Subcommand,
}
#[derive(Parser, Handler, Debug, Clone)]
#[handler_inject(add_something)]
pub enum Subcommand {
First(FirstSubcommand),
}
impl Subcommand {
fn add_something(&self, ctx: &mut Context) -> anyhow::Result<()> {
// insert something
// ctx.insert(a_struct);
Ok(())
}
}
#[derive(Args, Debug, Clone)]
pub struct FirstSubcommand {
arg: String,
}
#[handler(FirstSubcommand)]
fn handle_first(me: FirstSubcommand) -> anyhow::Result<()> {
Ok(())
}
fn main() {
let args = Arguments::parse();
log::debug!("{:#?}", args);
args.run().await
}
For more complex examples, see the anni and sswa.
Licensed under either of
at your option.