tonic-clap

Crates.iotonic-clap
lib.rstonic-clap
version0.2.5
created_at2025-08-24 21:59:07.890522+00
updated_at2025-09-18 04:42:07.54219+00
descriptiongRPC tonic cli generator core implementation.
homepage
repositoryhttps://github.com/youyuanwu/tonic-clap
max_upload_size
id1808820
size85,432
(youyuanwu)

documentation

https://docs.rs/tonic-clap-build/latest/tonic_clap_build/

README

tonic-clap

Auto generate tonic and clap gRPC CLI (commandline) tool from proto definition.

Features:

  • Each grpc service is a cli verb
  • Each grpc method is a cli verb under the service verb
  • Each field in the proto Request is a cli option
  • Nested field is a cli option with a path joined by "."

Option construction relies on bevy-reflect dynamic reflection. Proc macro does not quite work, and prost code gen is much more difficult to write.

This is experimental is only suitable for testing or debugging you app.

Get started

Add dependency:

[dependencies]
serde_json = "*"
tonic-clap = "*"
bevy_reflect = "*"

[build-dependencies]
tonic-clap-build = "*"

Add to your build.rs:

let mut builder = tonic_clap_build::configure().with_tonic_server(false);
let proto_file = Path::new("../../protos/helloworld.proto");
let proto_dir = proto_file.parent().unwrap();
builder.compile(&[proto_file], &[proto_dir]).unwrap();

Include generated file in your app:

pub mod helloworld {
    tonic::include_proto!("helloworld");
}

Add generated subcommand to your clap:

#[derive(clap::Parser)]
struct Args {
    #[command(subcommand)]
    command: helloworld::cli::CommandServices,
}

Example

See example: proto generated-cli-app

See the clap help of the example app

cargo run --bin hwgencli -q -- greeter say-hello2 --name hi --help
Usage: hwgencli.exe greeter say-hello2 [OPTIONS]

Options:
      --name <NAME>             Arg: String
      --field1.fname <FNAME>    Arg: String
      --field1.fcount <FCOUNT>  Arg: i32
      --field2 <FIELD2>         Arg: Vec<String>
      --field3 <FIELD3>         Arg: i32
  -h, --help                    Print help

Liscense

This project is licensed under the MIT license.

Commit count: 28

cargo fmt