#shell #macro #cli

shx

Utilities for quick shell scripting in Rust

1 unstable release

new 0.0.1 Nov 2, 2024

#535 in #shell

MIT license

10KB
181 lines

shx: Making Rust Command a Better Bash

The original motivation is qshell and xshell development.

This crate provides two macros for facilitating interactions with the underlying system.

The [cmd] macro is the lower level macro that implements a DSL to construct [Cmd]s.

The [shx] macro is a thin wrapper on top that executes each command in sequence, panicking if there's a failure.

The DSL allows for easily piping data into and out of the commands from Strings and Vec<u8>s.

Examples

# use sh::sh;
# #[cfg(target_os = "linux")]
# fn run() {
// We can use expressions as arguments
// and pipe the cmd output to a String or Vec<u8>
let world = "world";
let mut out = String::new();
shx!(echo hello {world} > {&mut out});
assert_eq!(out, "hello world\n");
out.clear();

// We can use the variadic expression (`...{iter_expr}`) syntax 
// that works with any iterable, and in Rust options are iterable.
// This means that `...` can be used to implement optional arguments. 
// For example:
let path = ".";
let option = Some("..");
let list = &[".", ".."];
shx!(ls {path} ...{option} ...{list})

// We can also pipe a String/&str or Vec<u8>/&[u8] to a command
let input = "foo bar baz";
shx!(cat < {input} > {&mut out});
assert_eq!(&out, input);

// We can execute many commands at once
let mut out1 = String::new();
let mut out2 = String::new();
let mut out3 = String::new();
shx! {
  echo hello world 1 > {&mut out1}; // Note the `;`
  echo hello world 2 > {&mut out2};
  echo hello world 3 > {&mut out3};
}
assert_eq!(&out1, "hello world 1\n");
assert_eq!(&out2, "hello world 2\n");
assert_eq!(&out3, "hello world 3\n");
# }
# run();

For more information, see the documentation for [cmd].

Inspirations

  • qshell: Command-running macro
  • xshell: ergonomic "bash" scripting in Rust
  • xtask: a way to add free-form automation to a Rust project, a-la make, npm run or bespoke bash scripts.

Dependencies

~0.4–1MB
~20K SLoC