rfcs icon indicating copy to clipboard operation
rfcs copied to clipboard

Add a method to reset the arguments to `std::process::Command`

Open UltiRequiem opened this issue 3 years ago • 6 comments

std::process::Command docs

It would be cool if this struct has an option to reset all the current arguments, this would be useful fo example here:

use std::{
    io::Result,
    process::{Child, Command},
};

pub fn get_killer() -> Box<dyn FnMut(&str) -> Result<Child>> {
    let is_windows = cfg!(target_os = "windows");

    let mut cmd = Command::new(if is_windows { "cmd" } else { "pkill" });

    Box::new(move |program| {
        cmd.reset_args(); // removing previous args from previous closure calls

        if is_windows {
            cmd.args(["taskkill", "/IM", program, "/F"]);
        } else {
            cmd.arg(program);
        }

        cmd.spawn()
    })
}

UltiRequiem avatar Feb 01 '22 20:02 UltiRequiem

Using tools possible today:

    let make_cmd = move || Command::new(if is_windows { "cmd" } else { "pkill" });

    Box::new(move |program| {
        let mut cmd = make_cmd();

shepmaster avatar Feb 01 '22 21:02 shepmaster

Using tools possible today:

    let make_cmd = move || Command::new(if is_windows { "cmd" } else { "pkill" });

    Box::new(move |program| {
        let mut cmd = make_cmd();

This will require instantiating Command multiple times when we could use the same instance all the time.

UltiRequiem avatar Feb 01 '22 22:02 UltiRequiem

when we could use the same instance all the time

What is the expected benefit of using one instance? I believe you will need to prove that benefit.

shepmaster avatar Feb 02 '22 00:02 shepmaster

I feel like it is a fairly safe assumption to make that, on all operating systems, on all platforms and in all environments, the total overhead associated with each call to Command::spawn (when spawning multiple processes) should far exceed the cost of allocating the Command's argument vector.

ExpHP avatar Feb 04 '22 18:02 ExpHP

It's weird that Command have env_clear() while args_clear() is missing 🙁

I propose to add arg_clear() and arg_remove(arg) so it have the same parity with env_clear() and env_remove(env)

DrSensor avatar Jan 02 '24 21:01 DrSensor

It's weird that Command have env_clear() while args_clear() is missing

env_clear says (emphasis mine):

Clears all explicitly set environment variables and prevents inheriting any parent process environment variables.

There are no "inherited parent process arguments", so all arguments were explicitly set. The two are not directly comparable.

shepmaster avatar Jan 02 '24 21:01 shepmaster