#execute #redirect #process #command #pipe

execute

A library for extending Command in order to execute programs more easily

11 releases

0.2.9 Apr 21, 2021
0.2.8 Jul 29, 2020
0.2.6 Jun 27, 2020
0.1.1 Jun 8, 2020

#70 in Parser implementations

Download history 92/week @ 2021-08-11 60/week @ 2021-08-18 88/week @ 2021-08-25 77/week @ 2021-09-01 163/week @ 2021-09-08 175/week @ 2021-09-15 207/week @ 2021-09-22 487/week @ 2021-09-29 638/week @ 2021-10-06 874/week @ 2021-10-13 2389/week @ 2021-10-20 796/week @ 2021-10-27 672/week @ 2021-11-03 761/week @ 2021-11-10 934/week @ 2021-11-17 691/week @ 2021-11-24

3,152 downloads per month
Used in 30 crates (14 directly)

MIT license

41KB
451 lines

Execute

CI

This library is used for extending Command in order to execute programs more easily.

Usage

extern crate execute;

use std::process::Command;

use execute::Execute;

// ...

Verify the Program

Since Command is used for spawning a process of a command and the executed progrom is external which may not exist or may not be the program that we expected, we usually need to verify the external program at runtime.

The execute_check_exit_status_code method can be used to execute a command and check its exit status. For example,

extern crate execute;

use std::process::Command;

use execute::Execute;

const FFMPEG_PATH: &str = "/path/to/ffmpeg";

let mut first_command = Command::new(FFMPEG_PATH);

first_command.arg("-version");

if first_command.execute_check_exit_status_code(0).is_err() {
    eprintln!("The path `{}` is not a correct FFmpeg executable binary file.", FFMPEG_PATH);
}

Execute and Get the Exit Status

extern crate execute;

use std::process::Command;

use execute::Execute;

const FFMPEG_PATH: &str = "/path/to/ffmpeg";

let mut command = Command::new(FFMPEG_PATH);

command.arg("-i");
command.arg("/path/to/media-file");
command.arg("/path/to/output-file");

if let Some(exit_code) = command.execute().unwrap() {
    if exit_code == 0 {
        println!("Ok.");
    } else {
        eprintln!("Failed.");
    }
} else {
    eprintln!("Interrupted!");
}

Execute and Get the Output

Output to the Screen

extern crate execute;

use std::process::Command;

use execute::Execute;

const FFMPEG_PATH: &str = "/path/to/ffmpeg";

let mut command = Command::new(FFMPEG_PATH);

command.arg("-i");
command.arg("/path/to/media-file");
command.arg("/path/to/output-file");

let output = command.execute_output().unwrap();

if let Some(exit_code) = output.status.code() {
    if exit_code == 0 {
        println!("Ok.");
    } else {
        eprintln!("Failed.");
    }
} else {
    eprintln!("Interrupted!");
}

Output to Memory (Captured)

extern crate execute;

use std::process::{Command, Stdio};

use execute::Execute;

const FFMPEG_PATH: &str = "/path/to/ffmpeg";

let mut command = Command::new(FFMPEG_PATH);

command.arg("-i");
command.arg("/path/to/media-file");
command.arg("/path/to/output-file");

command.stdout(Stdio::piped());
command.stderr(Stdio::piped());

let output = command.execute_output().unwrap();

if let Some(exit_code) = output.status.code() {
    if exit_code == 0 {
        println!("Ok.");
    } else {
        eprintln!("Failed.");
    }
} else {
    eprintln!("Interrupted!");
}

println!("{}", String::from_utf8(output.stdout).unwrap());
println!("{}", String::from_utf8(output.stderr).unwrap());

Execute and Input Data

Input In-memory Data

extern crate execute;

use std::process::{Command, Stdio};

use execute::Execute;

let mut bc_command = Command::new("bc");

bc_command.stdout(Stdio::piped());

let output = bc_command.execute_input_output("2^99\n").unwrap();

println!("Answer: {}", String::from_utf8(output.stdout).unwrap().trim_end());

Input from a Reader

extern crate execute;

use std::process::{Command, Stdio};
use std::fs::File;

use execute::Execute;

let mut cat_command = Command::new("cat");

cat_command.stdout(Stdio::piped());

let mut file = File::open("Cargo.toml").unwrap();

let output = cat_command.execute_input_reader_output(&mut file).unwrap();

println!("{}", String::from_utf8(output.stdout).unwrap());

By default, the buffer size is 256 bytes. If you want to change that, you can use the _reader_output2 or _reader2 methods and define a length explicitly.

For example, to change the buffer size to 4096 bytes,

extern crate execute;

use std::process::{Command, Stdio};
use std::fs::File;

use execute::generic_array::typenum::U4096;
use execute::Execute;

let mut cat_command = Command::new("cat");

cat_command.stdout(Stdio::piped());

let mut file = File::open("Cargo.toml").unwrap();

let output = cat_command.execute_input_reader_output2::<U4096>(&mut file).unwrap();

println!("{}", String::from_utf8(output.stdout).unwrap());

Execute Multiple Commands and Pipe Them Together

extern crate execute;

use std::process::{Command, Stdio};

use execute::Execute;

let mut command1 = Command::new("echo");
command1.arg("HELLO WORLD");

let mut command2 = Command::new("cut");
command2.arg("-d").arg(" ").arg("-f").arg("1");

let mut command3 = Command::new("tr");
command3.arg("A-Z").arg("a-z");

command3.stdout(Stdio::piped());

let output = command1.execute_multiple_output(&mut [&mut command2, &mut command3]).unwrap();

assert_eq!(b"hello\n", output.stdout.as_slice());

Run a Command String in the Current Shell

The shell function can be used to create a Command instance with a single command string instead of a program name and scattered arguments.

extern crate execute;

use std::process::{Command, Stdio};

use execute::{Execute, shell};

let mut command = shell("cat /proc/meminfo");

command.stdout(Stdio::piped());

let output = command.execute_output().unwrap();

println!("{}", String::from_utf8(output.stdout).unwrap());

Parse a Command String at Runtime

The command function can be used to create a Command instance with a single command string instead of a program name and scattered arguments. The difference between the shell function and the command function is that the former is interpreted by the current shell while the latter is parsed by this crate.

extern crate execute;

use std::process::{Command, Stdio};

use execute::{Execute, command};

let mut command = command("cat '/proc/meminfo'");

command.stdout(Stdio::piped());

let output = command.execute_output().unwrap();

println!("{}", String::from_utf8(output.stdout).unwrap());

Parse a Command String at Compile Time

The command! macro can be used to create a Command instance with a single command string literal instead of a program name and scattered arguments.

extern crate execute;

use std::process::{Command, Stdio};

use execute::Execute;

let mut command = execute::command!("cat '/proc/meminfo'");

command.stdout(Stdio::piped());

let output = command.execute_output().unwrap();

println!("{}", String::from_utf8(output.stdout).unwrap());

Create a Command Instance by Providing Arguments Separately

The command_args! macro can be used to create a Command instance with a program name and arguments separately. The program name and arguments can be non-literal.

extern crate execute;

use std::process::{Command, Stdio};

use execute::Execute;

let mut command = execute::command_args!("cat", "/proc/meminfo");

command.stdout(Stdio::piped());

let output = command.execute_output().unwrap();

println!("{}", String::from_utf8(output.stdout).unwrap());

Crates.io

https://crates.io/crates/execute

Documentation

https://docs.rs/execute

License

MIT

Dependencies

~0.5–1MB
~24K SLoC