#process #shell #stable #object #macro-rules #syntax #command

commandspec

Rust macro to build std::process::Command objects with shell syntax. Uses macro_rules! and works on stable.

13 releases (breaking)

Uses old Rust 2015

0.12.2 Nov 23, 2018
0.11.0 Oct 1, 2018
0.10.0 Jul 2, 2018
0.6.1 Mar 13, 2018

#1602 in Rust patterns

Download history 20/week @ 2024-03-09 9/week @ 2024-03-16 2/week @ 2024-03-23 26/week @ 2024-03-30 40/week @ 2024-04-06 10/week @ 2024-04-13 11/week @ 2024-04-20 4/week @ 2024-04-27 20/week @ 2024-05-04 27/week @ 2024-05-11 3/week @ 2024-05-18 194/week @ 2024-06-01 4/week @ 2024-06-08 1/week @ 2024-06-15 4/week @ 2024-06-22

203 downloads per month
Used in cargo-scriptify

MIT/Apache

40KB
832 lines

commandspec

Simple Rust macro for building std::process::Command objects. Uses macro_rules! and works on stable.

[dependencies]
commandspec = "0.10"

Then:

#[macro_use]
extern crate commandspec;

use commandspec::CommandSpec; // .execute() method on Command
use std::process::Command;

let result = execute!(
    r"
        cd path/location
        export RUST_LOG=full
        export RUST_BACKTRACE=1
        cargo run {release_flag} --bin {bin_name} -- {args}
    ",
    release_flag=Some("--release"),
    bin_name="binary",
    args=vec!["arg1", "arg2"],
)?;
// result = Ok(()) on success (error code 0), Err(CommandError) for all else

Format of the commandspec input, in order:

  • (optional) cd <path> to set the current working directory of the command, where path can be a literal, a quoted string, or format variable.
  • (optional) one or more export <name>=<value> lines to set environment variables, with the same formatting options.
  • Last, a command you want to invoke, optionally with format arguments.

Features:

  • format-like invocation makes it easy to interpolate variables, with automatic quoting
  • Equivalent syntax to shell when prototyping
  • Works on stable Rust.

License

MIT or Apache-2.0, at your option.

Dependencies

~2MB
~40K SLoC