#command #command-arguments #process #string #white-space #boilerplate #macro

humane_commands

Allows you to write more shell-like Commands

3 releases

0.1.2 Mar 23, 2024
0.1.1 Mar 23, 2024
0.1.0 Mar 19, 2024

#1025 in Rust patterns

Download history 31/week @ 2024-07-29 7/week @ 2024-09-23

59 downloads per month

MIT/Apache

20KB
391 lines

Remove a lot of clutter and boilerplate when using std::process::Command.

Macros

Use the cmd! macro to create plain std::process:Command structs, use exec! and run! to execute them directly via status() and output() respectively

run!(echo "Hello World!");
# equivalent to
Command::new("echo").arg("Hello World1").output();

All arguments will be treated as whitespace separated strings by default. If your argument includes whitespace, wrap it in quotes as shown above.

To use an existing variable as an arg, wrap its name in braces. You can mix arguments of different types (unlike the .args() method).

run!(echo (some_str) (some_string) (some_path));
# equivalent to
Command::new("echo").arg(some_str).arg(some_string).arg(some_path).output();

If your variable is iterable you can expand it using ..

run!(echo Hello (worlds..))

If your argument is optional you can use ? to only append it if it is Some

let name = Some("Steve");
run!(echo Hello (name ?));

If you include a literal inside the same braces the literal will also only be included if the value is Some (use this for flags). This also works for iterables.

let packages = vec!["cowsay", "emacs"];
run!(echo Installing ("-p" packages ?));

Use the args! macro to append arguments to an exist Command using the same syntax as cmd!

fn my_function(mut cmd: Command) -> Command {
    cmd.args(args!(some new args here));
    cmd
}

Extensions

Use the cmd_ok() method on the return value of status() or output() to create an Err() on both IO failure and any exit code except 0, the latter will include the contents of stderr if they are available.

run!(echo test).cmd_ok()?

Use opt_arg to pass optional values to your Command and only use them if they are Some.

Use output.stdout(), output.stderr(), output.stdout_lossy() and output.stderr_lossy() instead of String::from_utf8(output.stdout).

Dependencies

~110KB