Expand description
Hey! Nice you like to write with the cprc
module. It refers to clap, thank you for your great work there!
Unfortunately, you have to import clap in your project yourself with derive feature enabled.
§Quick setup
use command_rpc::crpc_main;
#[crpc_main]
pub mod my_cli_backend {
use command_rpc::{cprc_mod, crpc_fn};
#[cprc_fn]
pub fn greet(
/// The name of the person you want to greet.
name: str
) {
eprintln!("Hello, {}!", name);
}
#[crpc_mod]
pub mod my_cli_backend_sub {
use command_rpc::cprc_fn;
#[crpc_fn]
pub fn friendly_greet(
/// The name of the person you want to greet.
name: str,
/// the adjective you want to use in the greeting.
adjective: str
) {
eprintln!("Hello, {}! You are {}!", name, adjective);
}
}
}
fn main() {
My_cli_mod::parse().delegate();
}
§Prettier requests to endpoint
To make call to your endpoint in your Rust programs nicer, you can insert this snippet that defines a declarative macro (this is why it can´t be just exported from this crate). Calls in your program would look like this then(Same for other languages will be coming):
callback!(my_cli_backend::greet("John"));
macro_rules! callback {
($inp:expr) => {{
let mut cmd = $inp.to_string();
cmd = cmd.replace(";", "").replace(" ", "").replace("\n", "").replace("(", "").replace(")", "").replace("::", " ").replace(",", " ");
std::thread::spawn(move || {
let output = std::process::Command::new(cmd)
.output()
.expect("Failed to execute command");
eprintln!("{}", std::string::String::from_utf8_lossy(&output.stdout));
});
}};
}
Attribute Macros§
- crpc
- crpc_fn
- This attribute can be used to mark functions that should be available in the cli. This works lika a subcommand without nested commands; function parameters are used as arguments for the cli. The expansion consists of a struct that holds the arguments and a function that calls the original function with the arguments.
- crpc_
main - crpc_
mod - This attribute can be used to mark modules that should be available in the cli. This works like a subcommand with nested commands; the module name is used as the subcommand name.