macro_rules! cmd {
(sh status $cmd:expr; dir: $dir:expr) => { ... };
(sh status $cmd:expr) => { ... };
(sh $cmd:expr; dir: $dir:expr) => { ... };
(sh $cmd:expr) => { ... };
(status bash $cmd:expr; dir: $dir:expr) => { ... };
(status bash $cmd:expr) => { ... };
(bash status $cmd:expr; dir: $dir:expr) => { ... };
(bash status $cmd:expr) => { ... };
(bash $cmd:expr; dir: $dir:expr) => { ... };
(bash $cmd:expr) => { ... };
(status pwsh $cmd:expr; dir: $dir:expr) => { ... };
(status pwsh $cmd:expr) => { ... };
(pwsh status $cmd:expr; dir: $dir:expr) => { ... };
(pwsh status $cmd:expr) => { ... };
(pwsh $cmd:expr; dir: $dir:expr) => { ... };
(pwsh $cmd:expr) => { ... };
(try sh $cmd:expr; dir: $dir:expr) => { ... };
(try sh $cmd:expr) => { ... };
(try bash $cmd:expr; dir: $dir:expr) => { ... };
(try bash $cmd:expr) => { ... };
(try pwsh $cmd:expr; dir: $dir:expr) => { ... };
(try pwsh $cmd:expr) => { ... };
(status $binary:literal $($arg:literal)*; dir: $dir:expr) => { ... };
(status $binary:literal $($arg:literal)*) => { ... };
(status $binary:expr, [ $($arg:expr),* $(,)? ]; dir: $dir:expr) => { ... };
(status $binary:expr, [ $($arg:expr),* $(,)? ]) => { ... };
(status $binary:expr, $args:expr; dir: $dir:expr) => { ... };
(status $binary:expr, $args:expr) => { ... };
(try $binary:literal $($arg:literal)*; dir: $dir:expr) => { ... };
(try $binary:literal $($arg:literal)*) => { ... };
(try $binary:expr, [ $($arg:expr),* $(,)? ]; dir: $dir:expr) => { ... };
(try $binary:expr, [ $($arg:expr),* $(,)? ]) => { ... };
(try $binary:expr, $args:expr; dir: $dir:expr) => { ... };
(try $binary:expr, $args:expr) => { ... };
($binary:literal $($arg:literal)*; dir: $dir:expr) => { ... };
($binary:literal $($arg:literal)*) => { ... };
($binary:expr, [ $($arg:expr),* $(,)? ]; dir: $dir:expr) => { ... };
($binary:expr, [ $($arg:expr),* $(,)? ]) => { ... };
($binary:expr, $args:expr; dir: $dir:expr) => { ... };
($binary:expr, $args:expr) => { ... };
}Expand description
Execute a command and capture its output.
Simplifies Command::new(binary).args(args).output() patterns.
Thread safe — Command is Send + Sync.
Supports several calling conventions:
| Prefix | Returns | Description |
|---|---|---|
| (none) | io::Result<Output> | Run and capture output |
status | io::Result<ExitStatus> | Exit status only (no output) |
sh | io::Result<Output> | Parse string, capture output |
sh status | io::Result<ExitStatus> | Parse string, exit status only |
bash | io::Result<Output> | Run via bash -c <command> |
pwsh | io::Result<Output> | Run via pwsh -NoProfile -Command ... |
try | Result<String, String> | Run, return stdout or error message |
All forms support an optional ; dir: path suffix to set the working directory.
The sh variants use shell-aware word splitting (handles quoted arguments via
the shell-words crate).
§Syntax
ⓘ
// String form (shell-aware quoting)
cmd!(sh "git diff --name-only")
cmd!(sh format!("git diff --name-only {branch}"))
cmd!(sh "echo 'hello world'") // handles quotes
cmd!(bash "echo 'hello world'")
cmd!(pwsh "Write-Output 'hello world'")
// CLI-style literals (fastest)
cmd!("git" "diff-tree" "--no-commit-id" "--name-only")
cmd!("git" "log" "--oneline"; dir: repo_path)
// Array literal (dynamic types)
cmd!("git", ["branch", "--show-current"])
// Variable args
cmd!("git", args)
// Try form — returns Result<String, String>
cmd!(try "git" "rev-parse" "HEAD")
cmd!(try "git", args)
// With working directory
cmd!("git" "status"; dir: project_dir)
cmd!(try sh "npm test"; dir: project_dir)
cmd!(status bash "echo hello"; dir: project_dir)
cmd!(try pwsh "Write-Output 'hello'"; dir: project_dir)§Examples
ⓘ
use acorn::cmd;
use acorn::prelude::CommandOutput;
// String form with shell-aware quoting
let branch = "main";
match cmd!(sh format!("git diff --name-only {branch}")) {
Ok(output) if output.status.success() => {
println!("{}", output.stdout());
}
_ => {},
}
// CLI-style
match cmd!("git" "branch" "--show-current") {
Ok(output) if output.status.success() => {
println!("{}", output.stdout());
}
Ok(output) => eprintln!("{}", output.stderr()),
Err(why) => eprintln!("Error: {}", why),
}
// Try form — simplified error handling
match cmd!(try "git" "rev-parse" "HEAD") {
Ok(hash) => println!("{hash}"),
Err(msg) => eprintln!("failed: {msg}"),
}
// Explicit shell selection
let output = cmd!(bash "echo bash-mode")?;
let output = cmd!(pwsh "Write-Output 'pwsh-mode'")?;