6 releases
Uses new Rust 2024
| 0.2.5 | Sep 18, 2025 |
|---|---|
| 0.2.4 | Sep 15, 2025 |
| 0.2.0 | Aug 24, 2025 |
#31 in #tonic
42KB
890 lines
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.
Dependencies
~16–32MB
~499K SLoC