#cmd #process #child-process #bash #shell #log-error #log-level

build devx-cmd

针对 xtask 包的 std::process::Command 的便利包装

6 个版本 (破坏性)

0.5.0 2021 年 6 月 2 日
0.4.0 2021 年 5 月 28 日
0.3.1 2020 年 9 月 22 日
0.3.0 2020 年 8 月 29 日
0.1.0 2020 年 8 月 16 日

#319构建实用工具

Download history 5322/week @ 2024-01-03 5879/week @ 2024-01-10 7622/week @ 2024-01-17 5923/week @ 2024-01-24 4545/week @ 2024-01-31 4962/week @ 2024-02-07 6188/week @ 2024-02-14 5490/week @ 2024-02-21 7073/week @ 2024-02-28 6375/week @ 2024-03-06 7182/week @ 2024-03-13 5840/week @ 2024-03-20 4957/week @ 2024-03-27 6809/week @ 2024-04-03 5997/week @ 2024-04-10 5518/week @ 2024-04-17

每月 24,516 次下载
devx-pre-commit 中使用

MIT/Apache

27KB
412

devx-cmd

devx-cmd 为生成子进程提供了比 std::process 更便利的原始函数,专门用于开发脚本。

有关更多信息,请参阅 crate 级别文档


lib.rs:

devx-cmd 为生成子进程提供了比 std::process 更便利的原始函数,专门用于开发脚本。

该库的主要实体是 [Cmd](可执行命令的构建器)和 Child(表示生成的进程)。

还有一些方便的宏来减少样板代码。以下是一个基本使用示例

use devx_cmd::{read, run, cmd, Cmd};

// Initialize some low-overhead logger implementation for the `log` crate
simple_logger::SimpleLogger::new().init().unwrap();

// Run the program, logging the invocation via [`log`] crate and waiting until it finishes
// This is used only for side-effects.
// Note that if the process ends with a non-zero status code, this will return an error.
run!("ls", "-la")?;

// Same as `run!()`, but captures the stdout and returns it as a `String`
// there is also a `read_bytes!()` for non-utf8 sequences
let output = read!("echo", "foo")?;
assert_eq!(output.trim(), "foo");

let mut cmd = cmd!("rustfmt");
cmd
    // Set `trace` level for logging command invocation and output (`debug` by default)
    .log_cmd(log::Level::Trace)
    // Don't log error if the command fails
    .log_err(None)
    .stdin("fn foo () -> u32 {42}\n");

// Spawn without waiting for its completion, but capturing the stdout
let mut child = cmd.spawn_piped()?;

// Read output line-by-line
let first_line = child.stdout_lines().next().unwrap();

assert_eq!(first_line.trim(), "fn foo() -> u32 {");

// Dropping the child process `kill()`s it (and ignores the `Result`)
// Use `.wait()/.read()` to wait until its completion.
drop(child);

依赖项

~87KB