#shell #command #process #env #set #string #variables

command-builder

std::process::Command 的包装器,允许组合/链式连接不同的命令

2 个不稳定版本

0.2.0 2020年8月4日
0.1.0 2020年7月15日

#260 in #env

Download history 23/week @ 2024-04-01 1/week @ 2024-04-22

每月下载 64
用于 brew

MIT/Apache 许可证

19KB
490

Command-Builder

我认为 std::process::Command 使用起来不够方便,主要是因为它的临时性质。这个包提供了一个方便且可重复使用的包装器,用于围绕 Command 进程。我实现了 Debug(以 sh 脚本的形式显示)以及一组常见的逻辑运算符(&&|||;)来使用 shell 命令。

动机示例

// I was interested in bundling a set of commands, then exporting them with certain environmental variables set. 
// For debugging purposes, I wanted to see what commands had been run . I ended up with functions like this:
fn call_brew(primary_arg: String, opts: &[String], env: &HashMap);
// This would call a command like so:
// brew install (opts)* primary_arg 
// plus the env configuration. I was also handling logic like 
if test_for_brew()? {
    call_brew()
} else {
    Err(BrewNotFound)
}
// when my mental model was 
brew 2&> /null && brew command

// The last pain point was debug. I wanted non-transient commands to exist. This would allow me to collect 
// and search previous commands. 

这个库使用一个结构体包装了 std::process::Command,该结构体包含计算命令所需的信息。这个结构体是可克隆的,可打印(带有 debug)

使用 command-builder

use command_builder::{Command, Single};
let grep_for = Single::new("grep").a("ip").a("-c");
let direct_input = "lorim ipsum, spelling in latin is hard.";
let latin_file = Single::new("cat").a("file_name");
let searched_file = latin_file.pipe(grep_for);
let direct_search = grep_for.input(direct_input);
// Is the file what we expect?
searched_file.run()?.stdout() == direct_seach.run()?.stdout()
// confirm the commands were right
println!("searched_file: {:?}", searched_file);
// cat file_name | grep ip -c
println!("direct_search: {:?}", direct_search);
// grep ip -c < "lorim ipsum, spelling in latin is hard."

无运行时依赖