Crates.io | cmd-utils |
lib.rs | cmd-utils |
version | 0.3.1 |
source | src |
created_at | 2022-05-30 15:53:30.889914 |
updated_at | 2022-06-07 15:09:52.380766 |
description | rust Command utility traits, pipe commands |
homepage | https://github.com/steven89/cmd-utils-rs |
repository | https://github.com/steven89/cmd-utils-rs |
max_upload_size | |
id | 597044 |
size | 21,838 |
Safe Rust Command
utility traits
use std::process::Command;
use cmd_utils::CmdRun;
// `spawn` the command and `wait` for child process to end
// note that `run` does not return the command output, just Ok(())
// but you can redirect stdout & stderr where you want
Command::new("test")
// .stdout(cfg)
// .stderr(cfg)
.args(["-n", "a"])
.run()
.unwrap();
example available:
cargo run --example cmd_run
use std::process::Command;
use std::str;
use cmd_utils::CmdPipe;
// pipe echo & wc commands
// equivalent to bash: echo test | wc -c
let mut echo = Command::new("echo");
let mut wc = Command::new("wc");
let output = echo.arg("test")
.pipe(&mut wc.arg("-c"))
.unwrap();
let res = str::from_utf8(&output.stdout).unwrap();
println!("pipe result: {}", &res);
example available:
cargo run --example cmd_pipe
use std::fs::File;
use std::process::Command;
use cmd_utils::CmdToFile;
let stdout = File::create("my_file.stdout").unwrap();
let stderr = File::create("my_file.stderr").unwrap();
let mut cmd = Command::new("echo");
// writes stdout to file
cmd.arg("test").to_file(stdout, Some(stderr));
example available:
cargo run --example cmd_to_file