Attribute Macro clap_dispatch

Source
#[clap_dispatch]
Expand description

The main macro.

Needs to be attached to an enum and given a function signature as an attribute.

#[clap_dispatch(fn run(self))]
enum MyCommand {
  Foo(FooArgs)
  Bar(BarArgs)
}

It does two things:

  1. It creates a new trait, named like the provided function transformed to UpperCamelCase. The trait will contain only one function, which has exactly the provided signature.

    In this case it will generate:

    trait Run {
        fn run(self);
    }
  2. It implements the trait for the enum. The implementation is just to dispatch onto the different variants. This means the fields of all the enum variants need to implement the generated trait from above. It’s your job to make those implementations by hand.

    In this case it will generate:

    impl Run for MyCommand {
       fn run(self) {
           match self {
               Self::Foo(args) => args.run(),
               Self::Bar(args) => args.run(),
           }
       }
    }