Skip to content

Extends rustdoc on how to caputure output #32338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 15, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions src/libstd/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
/// let mut child = Command::new("/bin/cat")
/// .arg("file.txt")
/// .spawn()
/// .unwrap_or_else(|e| { panic!("failed to execute child: {}", e) });
/// .expect("failed to execute child");
///
/// let ecode = child.wait()
/// .unwrap_or_else(|e| { panic!("failed to wait on child: {}", e) });
/// .expect("failed to wait on child");
///
/// assert!(ecode.success());
/// ```
Expand Down Expand Up @@ -195,7 +195,8 @@ impl FromInner<AnonPipe> for ChildStderr {
/// .arg("-c")
/// .arg("echo hello")
/// .output()
/// .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });
/// .expect("failed to execute proces");
///
/// let hello = output.stdout;
/// ```
#[stable(feature = "process", since = "1.0.0")]
Expand Down Expand Up @@ -305,15 +306,16 @@ impl Command {
///
/// # Examples
///
/// ```
/// ```should_panic
/// use std::process::Command;
/// let output = Command::new("cat").arg("foo.txt").output().unwrap_or_else(|e| {
/// panic!("failed to execute process: {}", e)
/// });
/// let output = Command::new("/bin/cat").arg("file.txt").output()
/// .expect("failed to execute process");
///
/// println!("status: {}", output.status);
/// println!("stdout: {}", String::from_utf8_lossy(&output.stdout));
/// println!("stderr: {}", String::from_utf8_lossy(&output.stderr));
///
/// assert!(output.status.success());
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn output(&mut self) -> io::Result<Output> {
Expand All @@ -328,14 +330,15 @@ impl Command {
///
/// # Examples
///
/// ```
/// ```should_panic
/// use std::process::Command;
///
/// let status = Command::new("ls").status().unwrap_or_else(|e| {
/// panic!("failed to execute process: {}", e)
/// });
/// let status = Command::new("/bin/cat").arg("file.txt").status()
/// .expect("failed to execute process");
///
/// println!("process exited with: {}", status);
///
/// assert!(status.success());
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn status(&mut self) -> io::Result<ExitStatus> {
Expand Down Expand Up @@ -499,6 +502,29 @@ impl Child {
/// before waiting. This helps avoid deadlock: it ensures that the
/// child does not block waiting for input from the parent, while
/// the parent waits for the child to exit.
///
/// By default, stdin, stdout and stderr are inherited from the parent.
/// In order to capture the output into this `Result<Output>` it is
/// necessary to create new pipes between parent and child. Use
/// `stdout(Stdio::piped())` or `stderr(Stdio::piped())`, respectively.
///
/// # Examples
///
/// ```should_panic
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you tag this as no_run? I don't think Windows has /bin/cat programs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking with you: The example for output also only runs on windows and is also not tagged.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right because this'll just panic anyway as it couldn't spawn a process. Either way's fine!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexcrichton I prefer consistency, so let me revert my last commit so the PR reflects the initial doc update for std::process::Child::wait_with_output(). I'll create another PR to for the discussed changes.

/// use std::process::{Command, Stdio};
///
/// let mut child = Command::new("/bin/cat")
/// .arg("file.txt")
/// .stdout(Stdio::piped())
/// .spawn()
/// .expect("failed to execute child");
///
/// let ecode = child.wait_with_output()
/// .expect("failed to wait on child");
///
/// assert!(ecode.status.success());
/// ```
///
#[stable(feature = "process", since = "1.0.0")]
pub fn wait_with_output(mut self) -> io::Result<Output> {
drop(self.stdin.take());
Expand Down