4 releases
0.2.0 | Jan 31, 2024 |
---|---|
0.1.2 | Jan 12, 2024 |
0.1.1 | Dec 26, 2023 |
0.1.0 | Dec 26, 2023 |
#743 in Command-line interface
Used in 2 crates
(via heartless)
30KB
516 lines
This crate helps to automating command tools by simulating piped io in process.
Why this crate
Interactive command tools utilize stdin, stdout and stderr for communication. If you want to use command tools as libraries(no spawning processes) and tool authors agree, this crate can help to automating input/output, just 3 steps:
-
Define an
Altio
variable e.g.let io = Altio::default();
. -
Replace std APIs with altio's equivalents, e.g. replace
println!(...)
withwriteln!( io.out(), ... )
, replacestd::io::stdin()
withio.input()
. -
Keep main.rs as simple as possible, e.g.
fn main() { the_tool::run( std::env::args_os() )}
.
Example for tool authors
[dependencies]
altio = { version = "0.2", no_default_features = true }
[features]
altio = ["altio/altio"]
// lib.rs
pub struct TheTool {
// fields omitted
pub io: Altio,
}
impl_altio_output!( TheTool );
When building the tool as an application, the "altio" feature is disabled and altio falls back to stdio.
When building the tool as a library, the tool users can invoke send/recv methods
to communicate with the tool, e.g. send_line()
, try_recv_line()
.
Example for tool users
the_tool = { version = "1.0", features = ["altio"] }
let args = std::env::args_os(); // clap::Parser::parse_from()
let tool = the_tool::new();
let tool_io = tool.io.clone();
// `io.input().read_line()` called occasionally
std::thread::spawn( || tool.run( args ));
loop {
if let Some( received ) = tool_io.try_recv_line() {
if received == "Lorum" {
tool_io.send_line( "Ipsum" );
}
}
}
License
Under Apache License 2.0 or MIT License, at your will.