Corel
Corel

Reputation: 633

Function that gets slice of strings and extracts them as arguments to shell command in Rust

I'm trying to automate some installs using std::process::Command. For example, I want to write a "wrapper" function for Ubuntu's apt-get install. This command can take some optional flags like --no-install-recommends etc. How can I use these options if I pass them as a slice to the function? So I would like to have something like that:

fn apt_install(package_name: &str, options: &[&str]) {
    let mut c = Command::new("apt-get")
        .arg("install")
        // Flags here, as many as specified in 'options' argument
        .spawn()
        .expect(format!("Failed to install {}", package_name).as_str());
    c.wait().unwrap();
}

Upvotes: 0

Views: 67

Answers (1)

loops
loops

Reputation: 5635

Use Command::args:

fn apt_install(package_name: &str, options: &[&str]) {
    let mut c = Command::new("apt-get")
        .arg("install")
        .args(options)
        .spawn()
        .expect(format!("Failed to install {}", package_name).as_str());
    c.wait().unwrap();
}

Upvotes: 1

Related Questions