I execute commands:
use std::process::Command;
pub fn search(query: &str, flag: &str) -> Vec<String> {
let command = format!("xdotool search --onlyvisible {} {}", flag, query);
let output = Command::new("sh").arg("-c").arg(command).output();
match output {
Ok(o) => {
println!("Success message: {:?}", o);
String::from_utf8_lossy(&o.stdout)
.lines()
.map(|s| s.to_owned())
.collect()
}
Err(err) => {
eprintln!(
"Error message: {:?}",
String::from_utf8_lossy(&err.to_string().as_bytes())
.trim()
.to_owned()
);
Vec::new()
}
}
}
If I use a wrong flag
, I get an error message, but it's being handled of Ok()
in the search()
function:
Success message: Output { status: ExitStatus(unix_wait_status(256)), stdout: "", stderr: "search: unrecognized ...
Why is this? And how to make Err()
handle the error message instead?
Err
returned from output()
means spawning the process failed. It means nothing about the success/failure of the process itself. You need to check the ExitStatus
:
if !o.status.success() {
// Probably print stderr and bail.
}