macro_rules! rumtk_cli_process {
(
@collect [$($cli_args:tt)*]
$(#[$field_meta:meta])+
$field_name:ident : $field_typ:ty,
$($rest:tt)*
) => { ... };
(
@collect [$($cli_args:tt)*]
$field_name:ident : $field_typ:ty,
$($rest:tt)*
) => { ... };
(
@collect [$($cli_args:tt)*]
Direct($logic:expr) $(,)?
) => { ... };
(
@collect [$($cli_args:tt)*]
Message($logic:expr) $(,)?
) => { ... };
(
$($tokens:tt)*
) => { ... };
}Expand description
Defines the CLI arguments of the program, parses the command line, and wires the message processing logic.
The macro expects an optional list of CLI argument definitions (same syntax accepted by rumtk_cli_define_args) followed by either the two message handlers:
ok=> Called when the incoming message could be deserialized into the expected type.err=> Called with the raw bytes when deserialization was not possible.
or a single piece of logic receiving the parsed arguments. In the latter case, the logic owns the whole interface lifecycle and is called once with a reference to the parsed arguments.
The generated argument struct is named RUMTKInterfaceArgs and is available to the
handlers. If the --daemon flag is passed, the ok/err handlers are called in a loop for
every message coming through stdin. Otherwise, a single message is processed.
§Example
§Example 1 - With Parsed and Unparsed Message Closures
use rumtk_core::base::{RUMResult, RUMVec};
use rumtk_core::strings::RUMString;
use rumtk_core::types::RUMCLIParser;
use rumtk_core::rumtk_cli_process;
fn main() {
rumtk_cli_process!(
///
/// Pretty print the outgoing message.
///
#[arg(short, long)]
pretty: bool,
///
/// Run in debug mode?
///
#[arg(short, long)]
debug: bool,
Message(|data: RUMVec<u8>, args: &RUMTKInterfaceArgs| -> RUMResult<RUMString> {
Ok(RUMString::default())
})
);
}§Example 2 - With Raw Logic
use rumtk_core::base::{RUMResult, RUMVec};
use rumtk_core::strings::RUMString;
use rumtk_core::types::RUMCLIParser;
use rumtk_core::rumtk_cli_process;
fn main() {
rumtk_cli_process!(
///
/// Pretty print the outgoing message.
///
#[arg(short, long)]
pretty: bool,
///
/// Run in debug mode?
///
#[arg(short, long)]
debug: bool,
Direct(|args: &RUMTKInterfaceArgs| -> RUMResult<()> {
Ok(())
})
);
}§Notes
I let Claude Opus 5 help complete this macro. I did not understand how to wire the
token tree muncher into the argument pattern to pass to rumtk_cli_define_args
and the two additional arguments representing the closures to be executed if message parsing is successful or not.