cmd

Macro cmd 

Source
macro_rules! cmd {
    ($cmd_str:expr) => { ... };
    ($cmd_str:literal, $($arg:expr),*) => { ... };
    ($cmd:expr => []) => { ... };
    ($cmd:expr => {}) => { ... };
    ($cmd:expr => ()) => { ... };
    ($cmd:expr => $args:expr) => { ... };
}
Expand description

Constructs a Command as if receiving the command directly from the CLI. Arguments wrapped in single or double quotes are treated as single arguments, allowing multiple tokens to be passed as a single argument to a command.

§Example

use extel::cmd;

const EXPECTED: &str = "hello world";

let cmd_output = cmd!("echo -n \"hello world\"").output().unwrap();
let cmd_output_fmt = cmd!("echo -n \"{}\"", EXPECTED).output().unwrap();

assert_eq!(
    String::from_utf8_lossy(&cmd_output.stdout),
    String::from_utf8_lossy(&cmd_output_fmt.stdout)
)

It is suggested to use this macro with string literals and passing in arguments, but if you prefer using Path/PathBuf/OsStr (the typical arguments expected by Command), then you can use a special version of this macro that is meant to work with any values and arguments that can be passed into a regular Command.

§Example

use extel::cmd;
use std::path::Path;

const EXPECTED: &str = "hello world";
let exe_path = Path::new("echo");

let cmd_output = cmd!("echo -n \"hello world\"").output().unwrap();
let cmd_output_path = cmd!(exe_path => ["-n", "hello world"]).output().unwrap();

assert_eq!(
    String::from_utf8_lossy(&cmd_output.stdout),
    String::from_utf8_lossy(&cmd_output_path.stdout)
)