#clap #subcommand #handler #context #multi-level

clap-handler

A command handler works with clap-derive to generating subcommand handlers

3 releases

Uses new Rust 2024

0.1.2 Mar 27, 2025
0.1.1 Jun 22, 2022
0.1.0 Jun 22, 2022

#46 in #clap

Download history 9/week @ 2025-01-29 56/week @ 2025-02-05 13/week @ 2025-02-12 49/week @ 2025-02-19 33/week @ 2025-02-26 1/week @ 2025-03-12 185/week @ 2025-03-26 26/week @ 2025-04-02 13/week @ 2025-04-09 11/week @ 2025-04-16

235 downloads per month

MIT/Apache

11KB
114 lines

clap-handler

A command handler works with clap-derive to generating subcommand handlers.

Example

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.

License

Licensed under either of

at your option.

Dependencies

~2MB
~41K SLoC