Skip to content

Commit bdb9aa2

Browse files
authored
Rollup merge of rust-lang#123726 - jieyouxu:command-new-docs, r=Nilstrieb
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.
2 parents 9adf702 + 02bf152 commit bdb9aa2

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

std/src/process.rs

+19
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,25 @@ impl Command {
629629
/// .spawn()
630630
/// .expect("sh command failed to start");
631631
/// ```
632+
///
633+
/// # Caveats
634+
///
635+
/// [`Command::new`] is only intended to accept the path of the program. If you pass a program
636+
/// path along with arguments like `Command::new("ls -l").spawn()`, it will try to search for
637+
/// `ls -l` literally. The arguments need to be passed separately, such as via [`arg`] or
638+
/// [`args`].
639+
///
640+
/// ```no_run
641+
/// use std::process::Command;
642+
///
643+
/// Command::new("ls")
644+
/// .arg("-l") // arg passed separately
645+
/// .spawn()
646+
/// .expect("ls command failed to start");
647+
/// ```
648+
///
649+
/// [`arg`]: Self::arg
650+
/// [`args`]: Self::args
632651
#[stable(feature = "process", since = "1.0.0")]
633652
pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
634653
Command { inner: imp::Command::new(program.as_ref()) }

0 commit comments

Comments
 (0)