2个版本
0.2.1 | 2024年3月3日 |
---|---|
0.2.0 | 2024年3月3日 |
#230 in 过程宏
每月 95 次下载
10KB
178 行
sh
: 命令运行宏
此crate提供了两个宏,以简化与底层系统的交互。 [cmd
] 宏是底层宏,它实现了一个DSL来构建 QCmd
。 [sh
] 宏是一个薄包装器,它按顺序执行每个命令,如果发生失败则引发恐慌。
该DSL允许从 String
和 Vec<u8>
将数据轻松地管道输入和输出到命令中。
示例
# use sh::sh;
# #[cfg(target_os = "linux")]
# fn run() {
let world = "world";
let mut out = String::new();
// We can use expressions as arguments
// and pipe the cmd output to a String or Vec<u8>
sh!(echo hello {world} > {&mut out});
assert_eq!(out, "hello world\n");
// We can also pipe a String/&str or Vec<u8>/&[u8] to a command
out.clear();
let input = "foo bar baz";
sh!(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();
sh! {
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();
有关更多信息,请参阅 [cmd
] 的文档。
未来目标
- 支持从/到文件的管道
- 支持从一个命令到下一个命令的管道
依赖项
~0.5–1MB
~22K SLoC