checked_command/lib.rs
1//! In std the methods used to retrieve the `ExitStatus`/`Output`
2//! from executing a `Command` do not consider the exit status
3//! when deciding weather or not the function returns a error.
4//!
5//! This creates provides:
6//!
7//! 1. A `CheckedCommand` and `CheckedChild` struct, which wrap `std::process::Command` and
8//! `std::process::Child` replacing `status()`,`output()`,`wait()` and `wait_with_output()`
9//! with a version which will check the `ExitStatus` and if it didn't succeed
10//! they will return a `Err(...)` instead of a `Ok(...)`.
11//!
12//! 2. A `CommandExt` and `ChildExt` extension trait which provide versions of `status()`,
13//! `output()` etc. which check the `ExitStatus` returning a `Err(...)` if the exit status
14//! is non successful (i.e. there is no exit code or it is not equal zero). The
15//! checked methods are `checked_status()`, `checked_output()`, `checked_wait()`,
16//! `checked_wait_with_output()`. The `CheckedCommand` and `CheckedChild` wrapper use
17//! this methods as their `status()`/`output()` etc. methods.
18//!
19//! In case of functions originally returning a `Output` it might be necessary to process
20//! the `Output` even if the command returned a non-successful exit status. For this reason
21//! the `Output` is included into the error `Error::Failure` variant (as option as not all
22//! functions provide a output)
23//!
24//! Note that the provided functions do return their own `Output` struct instead of
25//! `std::process::Output` which differs in that it does not contain a `status` field
26//! (which is also not anymore needed for the new methods). There is `use_std_output`
27//! feature which will make the crate use the std's output implementation instead.
28//!
29//! # Example
30//!
31//! ```
32//! use checked_command::{ Error, CheckedCommand };
33//!
34//! let result = CheckedCommand::new("ls")
35//! .arg("--badbadbad").arg("--")
36//! .output();
37//!
38//! match result {
39//! Ok(_) => panic!("ls should have failed"),
40//! Err(Error::Io(io_err)) => panic!("unexpected I/O Error: {:?}", io_err),
41//! Err(Error::Failure(ex, output)) => {
42//! println!("failed with exit code: {:?}", ex.code());
43//! if let Some(output) = output {
44//! println!("error output was:\n{}",
45//! String::from_utf8_lossy(&*output.stderr));
46//! }
47//! }
48//! }
49//! ```
50//!
51//!
52//! # Features
53//!
54//! ## process_try_wait
55//!
56//! Requires nightly rust as the rust feature `process_try_wait` is required.
57//!
58//! Adds a `checked_try_wait` method to `ChildExt` as well as a `try_wait`
59//! method to `CheckedChild` (which uses `checked_try_wait` internally).
60//! Both methods call the unstable `std::process::Child::try_wait` method
61//! internally.
62//!
63//!
64//! ## command_envs
65//!
66//! Requires nightly rust as the rust feature `command_envs` is required.
67//!
68//! Adds a `envs` method to `CheckedCommand` which calls the unstable
69//! `std::process::Command::env` method.
70//!
71//!
72//! ## use_std_output
73//!
74//! Works with stable.
75//!
76//! This crate uses normally it's own `Output` struct, with this option the
77//! `std::process::Output` is used instead, which differs in that it has an
78//! additional `status: ExitStatus` field.
79//!
80//!
81//! ## enable_integration_tests
82//!
83//! Works with stable.
84//!
85//! Enable integration tests for this crate.
86//!
87#![cfg_attr(feature = "process_try_wait", feature(process_try_wait))]
88#![cfg_attr(feature = "command_envs", feature(command_envs))]
89#![deny(missing_docs)]
90
91#[macro_use]
92extern crate quick_error;
93
94/// internal module containing the CommandExt/ChildExt traints and implementation
95#[doc(hidden)]
96mod ext;
97
98/// internal module containing the CheckedCommand and CheckedChild wrappers
99#[doc(hidden)]
100mod wrapper;
101
102pub use wrapper::CheckedChild;
103pub use wrapper::CheckedCommand;
104
105pub use ext::ChildExt;
106pub use ext::CommandExt;
107pub use ext::Error;
108pub use ext::Output;