@@ -29,20 +29,58 @@ use crate::{
2929#[ derive( Debug ) ]
3030pub struct Command {
3131 cmd : StdCommand ,
32- stdin : Option < Box < [ u8 ] > > ,
32+ // Convience for providing a quick stdin buffer.
33+ stdin_buf : Option < Box < [ u8 ] > > ,
34+
35+ // Configurations for child process's std{in,out,err} handles.
36+ stdin : Option < Stdio > ,
37+ stdout : Option < Stdio > ,
38+ stderr : Option < Stdio > ,
39+
3340 drop_bomb : DropBomb ,
3441}
3542
3643impl Command {
3744 #[ track_caller]
3845 pub fn new < P : AsRef < OsStr > > ( program : P ) -> Self {
3946 let program = program. as_ref ( ) ;
40- Self { cmd : StdCommand :: new ( program) , stdin : None , drop_bomb : DropBomb :: arm ( program) }
47+ Self {
48+ cmd : StdCommand :: new ( program) ,
49+ stdin_buf : None ,
50+ drop_bomb : DropBomb :: arm ( program) ,
51+ stdin : None ,
52+ stdout : None ,
53+ stderr : None ,
54+ }
55+ }
56+
57+ /// Specify a stdin input buffer. This is a convenience helper,
58+ pub fn stdin_buf < I : AsRef < [ u8 ] > > ( & mut self , input : I ) -> & mut Self {
59+ self . stdin_buf = Some ( input. as_ref ( ) . to_vec ( ) . into_boxed_slice ( ) ) ;
60+ self
61+ }
62+
63+ /// Configuration for the child process’s standard input (stdin) handle.
64+ ///
65+ /// See [`std::process::Command::stdin`].
66+ pub fn stdin < T : Into < Stdio > > ( & mut self , cfg : T ) -> & mut Self {
67+ self . stdin = Some ( cfg. into ( ) ) ;
68+ self
69+ }
70+
71+ /// Configuration for the child process’s standard output (stdout) handle.
72+ ///
73+ /// See [`std::process::Command::stdout`].
74+ pub fn stdout < T : Into < Stdio > > ( & mut self , cfg : T ) -> & mut Self {
75+ self . stdout = Some ( cfg. into ( ) ) ;
76+ self
4177 }
4278
43- /// Specify a stdin input
44- pub fn stdin < I : AsRef < [ u8 ] > > ( & mut self , input : I ) -> & mut Self {
45- self . stdin = Some ( input. as_ref ( ) . to_vec ( ) . into_boxed_slice ( ) ) ;
79+ /// Configuration for the child process’s standard error (stderr) handle.
80+ ///
81+ /// See [`std::process::Command::stderr`].
82+ pub fn stderr < T : Into < Stdio > > ( & mut self , cfg : T ) -> & mut Self {
83+ self . stderr = Some ( cfg. into ( ) ) ;
4684 self
4785 }
4886
@@ -105,6 +143,8 @@ impl Command {
105143 }
106144
107145 /// Run the constructed command and assert that it is successfully run.
146+ ///
147+ /// By default, std{in,out,err} are [`Stdio::piped()`].
108148 #[ track_caller]
109149 pub fn run ( & mut self ) -> CompletedProcess {
110150 let output = self . command_output ( ) ;
@@ -115,6 +155,8 @@ impl Command {
115155 }
116156
117157 /// Run the constructed command and assert that it does not successfully run.
158+ ///
159+ /// By default, std{in,out,err} are [`Stdio::piped()`].
118160 #[ track_caller]
119161 pub fn run_fail ( & mut self ) -> CompletedProcess {
120162 let output = self . command_output ( ) ;
@@ -124,10 +166,10 @@ impl Command {
124166 output
125167 }
126168
127- /// Run the command but do not check its exit status.
128- /// Only use if you explicitly don't care about the exit status.
129- /// Prefer to use [`Self::run`] and [`Self::run_fail`]
130- /// whenever possible.
169+ /// Run the command but do not check its exit status. Only use if you explicitly don't care
170+ /// about the exit status.
171+ ///
172+ /// Prefer to use [`Self::run`] and [`Self::run_fail`] whenever possible.
131173 #[ track_caller]
132174 pub fn run_unchecked ( & mut self ) -> CompletedProcess {
133175 self . command_output ( )
@@ -137,11 +179,11 @@ impl Command {
137179 fn command_output ( & mut self ) -> CompletedProcess {
138180 self . drop_bomb . defuse ( ) ;
139181 // let's make sure we piped all the input and outputs
140- self . cmd . stdin ( Stdio :: piped ( ) ) ;
141- self . cmd . stdout ( Stdio :: piped ( ) ) ;
142- self . cmd . stderr ( Stdio :: piped ( ) ) ;
182+ self . cmd . stdin ( self . stdin . take ( ) . unwrap_or ( Stdio :: piped ( ) ) ) ;
183+ self . cmd . stdout ( self . stdout . take ( ) . unwrap_or ( Stdio :: piped ( ) ) ) ;
184+ self . cmd . stderr ( self . stderr . take ( ) . unwrap_or ( Stdio :: piped ( ) ) ) ;
143185
144- let output = if let Some ( input) = & self . stdin {
186+ let output = if let Some ( input) = & self . stdin_buf {
145187 let mut child = self . cmd . spawn ( ) . unwrap ( ) ;
146188
147189 {
0 commit comments