Skip to content

Commit

Permalink
Rollup merge of rust-lang#123726 - jieyouxu:command-new-docs, r=Nilst…
Browse files Browse the repository at this point in the history
…rieb

Clarify `Command::new` behavior for programs with arguments

I mistakenly passed program path along arguments as the same string into `Command::new` a couple of times now. It might be useful to explicitly highlight that `Command::new` intends to accept path to a program, not path to a program plus arguments. Also nudge the user to use `Command::arg` or `Command::args` if they wish to pass arguments.
  • Loading branch information
matthiaskrgr authored Jun 13, 2024
2 parents 9adf702 + 02bf152 commit bdb9aa2
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,25 @@ impl Command {
/// .spawn()
/// .expect("sh command failed to start");
/// ```
///
/// # Caveats
///
/// [`Command::new`] is only intended to accept the path of the program. If you pass a program
/// path along with arguments like `Command::new("ls -l").spawn()`, it will try to search for
/// `ls -l` literally. The arguments need to be passed separately, such as via [`arg`] or
/// [`args`].
///
/// ```no_run
/// use std::process::Command;
///
/// Command::new("ls")
/// .arg("-l") // arg passed separately
/// .spawn()
/// .expect("ls command failed to start");
/// ```
///
/// [`arg`]: Self::arg
/// [`args`]: Self::args
#[stable(feature = "process", since = "1.0.0")]
pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
Command { inner: imp::Command::new(program.as_ref()) }
Expand Down

0 comments on commit bdb9aa2

Please sign in to comment.