Looking for a command/subprocess crate whose methods move the value and return Self instead of take/return mutable reference

When working with std::process::Command, I usually create a function that set shared environment variables and arguments.

I wish to do this:

fn clone_my_repo() -> Command {
  Command::new("git")
    .env_remove("GIT_DIR")
    .arg("clone")
    .arg("--depth=1")
    .arg("repo url")
}

but couldn't because env_remove and arg take and return a mutable reference.

I know I could assign Command::new("git") to a temporary mutable variable, modify, and return it, but that's verbose.

I can make a trait or a wrapper type with ideal APIs, but I don't want to reinvent the wheel, so let me know if there's already a crate for that.

Honestly I don't think it's worth a crate. You only need 2 more lines and it's hard to make a mistake for it.

fn clone_my_repo() -> Command {
  let mut cmd = Command::new("git");
  cmd
    .env_remove("GIT_DIR")
    .arg("clone")
    .arg("--depth=1")
    .arg("repo url");
  cmd
}
1 Like