rust-shell-script / rust_cmd_lib

Common rust command-line macros and utilities, to write shell-script like tasks in a clean, natural and rusty way
https://docs.rs/cmd_lib/
Apache License 2.0
1.05k stars 37 forks source link

How to catch all output and return code? #56

Closed manfredlotz closed 1 year ago

manfredlotz commented 1 year ago

Assume I run a find command and its output shows found items and also some errors like this

find testdir/
testdir/
testdir/testfile
testdir/fstab
testdir/not_readable
find: ‘testdir/not_readable’: Permission denied

Here I like to catch

How can I do this? Thanks.

rust-shell-script commented 1 year ago
➜  rust_cmd_lib git:(master) ✗ cat examples/catch_all.rs    
use cmd_lib::*;

#[cmd_lib::main]
fn main() -> MainResult {
    let (cmd_res, stdout_res, stderr_res) = spawn_with_output!(ls -d / /xxx)?.wait_with_all();
    info!("cmd err: {}", cmd_res.unwrap_err());
    info!("stdout: {}", stdout_res.unwrap());
    info!("stderr: {}", stderr_res.unwrap());
    Ok(())
}
➜  rust_cmd_lib git:(master) ✗ cargo run --example catch_all
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target/debug/examples/catch_all`
[INFO ] cmd err: Running ["ls" "-d" "/" "/xxx"] exited with error; status code: 2
[INFO ] stdout: /
[INFO ] stderr: ls: cannot access '/xxx': No such file or directory
manfredlotz commented 1 year ago

Thanks.