pub struct Command { /* private fields */ }
Expand description
Build a command to execute. This has an API which is deliberately
similar to std::process::Command
.
let err = exec::Command::new("echo")
.arg("hello")
.arg("world")
.exec();
println!("Error: {}", err);
If the exec
function succeeds, it will never return.
Implementations§
Source§impl Command
impl Command
Sourcepub fn new<S: AsRef<OsStr>>(program: S) -> Command
pub fn new<S: AsRef<OsStr>>(program: S) -> Command
Create a new command builder, specifying the program to run. The
program will be searched for using the usual rules for PATH
.
Examples found in repository?
examples/exec.rs (line 15)
6fn main() {
7 let argv: Vec<String> = env::args().skip(1).collect();
8 if argv.len() < 1 {
9 println!("Must specify command to execute");
10 process::exit(1);
11 }
12
13 // Exec the specified program. If all goes well, this will never
14 // return. If it does return, it will always retun an error.
15 let err = exec::Command::new(&argv[0]).args(&argv).exec();
16 println!("Error: {}", err);
17 process::exit(1);
18}
Sourcepub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command
Add an argument to the command builder. This can be chained.
Sourcepub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Command
pub fn args<S: AsRef<OsStr>>(&mut self, args: &[S]) -> &mut Command
Add multiple arguments to the command builder. This can be chained.
let err = exec::Command::new("echo")
.args(&["hello", "world"])
.exec();
println!("Error: {}", err);
Examples found in repository?
examples/exec.rs (line 15)
6fn main() {
7 let argv: Vec<String> = env::args().skip(1).collect();
8 if argv.len() < 1 {
9 println!("Must specify command to execute");
10 process::exit(1);
11 }
12
13 // Exec the specified program. If all goes well, this will never
14 // return. If it does return, it will always retun an error.
15 let err = exec::Command::new(&argv[0]).args(&argv).exec();
16 println!("Error: {}", err);
17 process::exit(1);
18}
Sourcepub fn exec(&mut self) -> Error
pub fn exec(&mut self) -> Error
Execute the command we built. If this function succeeds, it will never return.
Examples found in repository?
examples/exec.rs (line 15)
6fn main() {
7 let argv: Vec<String> = env::args().skip(1).collect();
8 if argv.len() < 1 {
9 println!("Must specify command to execute");
10 process::exit(1);
11 }
12
13 // Exec the specified program. If all goes well, this will never
14 // return. If it does return, it will always retun an error.
15 let err = exec::Command::new(&argv[0]).args(&argv).exec();
16 println!("Error: {}", err);
17 process::exit(1);
18}
Auto Trait Implementations§
impl Freeze for Command
impl RefUnwindSafe for Command
impl Send for Command
impl Sync for Command
impl Unpin for Command
impl UnwindSafe for Command
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more