简体   繁体   中英

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();
}

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM