Skip to content

Commit 6a10a48

Browse files
committed
implement std::process::Command::args_new()
1 parent c420fdc commit 6a10a48

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

library/std/src/process.rs

+42
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,48 @@ impl Command {
714714
self
715715
}
716716

717+
/// Reset the argument list to new content
718+
///
719+
/// Allows to maintain the argument list in a Vec or anything else that can provide
720+
/// a suitable iterator.
721+
///
722+
/// # Examples
723+
///
724+
/// Basic usage:
725+
///
726+
/// ```no_run
727+
/// #![feature(mutate_command_args)]
728+
/// use std::process::Command;
729+
///
730+
/// // Prepare a command
731+
/// let mut command = Command::new("ls");
732+
/// let mut my_args = Vec::from(["-l", "foo"]);
733+
///
734+
/// command
735+
/// .args_new(&my_args)
736+
/// .spawn()
737+
/// .unwrap();
738+
///
739+
/// // Mutate my_args
740+
/// my_args.insert(1, "-a");
741+
/// my_args.pop();
742+
/// my_args.push("bar");
743+
///
744+
/// // Run command again with the mutated arguments
745+
/// command
746+
/// .args_new(&my_args)
747+
/// .spawn()
748+
/// .unwrap();
749+
/// ```
750+
#[unstable(feature = "mutate_command_args", issue = "87379")]
751+
pub fn args_new<I, S>(&mut self, args: I) -> &mut Command
752+
where
753+
I: IntoIterator<Item = S>,
754+
S: AsRef<OsStr>,
755+
{
756+
self.args_clear().args(args)
757+
}
758+
717759
/// Inserts or updates an environment variable mapping.
718760
///
719761
/// Note that environment variable names are case-insensitive (but case-preserving) on Windows,

0 commit comments

Comments
 (0)