Expand description
In std the methods used to retrieve the ExitStatus
/Output
from executing a Command
do not consider the exit status
when deciding weather or not the function returns a error.
This creates provides:
-
A
CheckedCommand
andCheckedChild
struct, which wrapstd::process::Command
andstd::process::Child
replacingstatus()
,output()
,wait()
andwait_with_output()
with a version which will check theExitStatus
and if it didn’t succeed they will return aErr(...)
instead of aOk(...)
. -
A
CommandExt
andChildExt
extension trait which provide versions ofstatus()
,output()
etc. which check theExitStatus
returning aErr(...)
if the exit status is non successful (i.e. there is no exit code or it is not equal zero). The checked methods arechecked_status()
,checked_output()
,checked_wait()
,checked_wait_with_output()
. TheCheckedCommand
andCheckedChild
wrapper use this methods as theirstatus()
/output()
etc. methods.
In case of functions originally returning a Output
it might be necessary to process
the Output
even if the command returned a non-successful exit status. For this reason
the Output
is included into the error Error::Failure
variant (as option as not all
functions provide a output)
Note that the provided functions do return their own Output
struct instead of
std::process::Output
which differs in that it does not contain a status
field
(which is also not anymore needed for the new methods). There is use_std_output
feature which will make the crate use the std’s output implementation instead.
§Example
use checked_command::{ Error, CheckedCommand };
let result = CheckedCommand::new("ls")
.arg("--badbadbad").arg("--")
.output();
match result {
Ok(_) => panic!("ls should have failed"),
Err(Error::Io(io_err)) => panic!("unexpected I/O Error: {:?}", io_err),
Err(Error::Failure(ex, output)) => {
println!("failed with exit code: {:?}", ex.code());
if let Some(output) = output {
println!("error output was:\n{}",
String::from_utf8_lossy(&*output.stderr));
}
}
}
§Features
§process_try_wait
Requires nightly rust as the rust feature process_try_wait
is required.
Adds a checked_try_wait
method to ChildExt
as well as a try_wait
method to CheckedChild
(which uses checked_try_wait
internally).
Both methods call the unstable std::process::Child::try_wait
method
internally.
§command_envs
Requires nightly rust as the rust feature command_envs
is required.
Adds a envs
method to CheckedCommand
which calls the unstable
std::process::Command::env
method.
§use_std_output
Works with stable.
This crate uses normally it’s own Output
struct, with this option the
std::process::Output
is used instead, which differs in that it has an
additional status: ExitStatus
field.
§enable_integration_tests
Works with stable.
Enable integration tests for this crate.
Structs§
- Checked
Child - A wrapper around
std::process::Child
which hides the originalwait
/wait_with_output
methods and replaces it with the versions fromchecked_command::ChildExt
- Checked
Command - A wrapper around
std::process::Command
which hides the originalstatus
/output
methods and replaces them with the versions fromchecked_command::CommandExt
- Output
- custom output type which, diferently to
std::process::Output
is not closely coupled withExitStatus
(it does not have astatus
field)
Enums§
- Error
- error type representing either a
io::Error
or a failure caused by a non-successful exit status i.e. without exit code or a exit code not equal zero.
Traits§
- Child
Ext - Extension to
std::process::Child
adding versions of the wait_with_output/wait functions which also fail/error with a non-success exit status - Command
Ext - Extension to
std::process::Command
adding versions of the output/status functions which also fail/error with a non-success exit status