#shell #macro #cli

sh

用于轻松运行命令的宏

2个版本

0.2.1 2024年3月3日
0.2.0 2024年3月3日

#230 in 过程宏

Download history 23/week @ 2024-03-31 6/week @ 2024-04-07 2/week @ 2024-04-14 4/week @ 2024-04-21 1/week @ 2024-04-28 17/week @ 2024-05-19 8/week @ 2024-05-26 40/week @ 2024-06-02 35/week @ 2024-06-09 18/week @ 2024-06-16 10/week @ 2024-06-23 10/week @ 2024-06-30 6/week @ 2024-07-07 61/week @ 2024-07-14

每月 95 次下载

MIT 许可证

10KB
178

sh: 命令运行宏

此crate提供了两个宏,以简化与底层系统的交互。 [cmd] 宏是底层宏,它实现了一个DSL来构建 QCmd。 [sh] 宏是一个薄包装器,它按顺序执行每个命令,如果发生失败则引发恐慌。

该DSL允许从 StringVec<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