简体   繁体   English

如何将 std::process::Command 格式化为用于调试的字符串

[英]How to format the std::process::Command as a string for debugging

Working on my first rust app, and it issues a number of commands via std::process::Command .在我的第一个 rust 应用程序上工作,它通过std::process::Command发出许多命令。 If one of these is incorrect I'd like to see what it is, and to have it look pretty on the command line.如果其中一个不正确,我想看看它是什么,并让它在命令行上看起来很漂亮。

Currently I have code that looks like this (simplified):目前我的代码看起来像这样(简化):

let mut command = std::process::Command::new("ls");
command.arg("-la");
println!("{:?}", command)

This is alright, but it encloses everything in quotes when it prints the string.这没关系,但它在打印字符串时将所有内容都括在引号中。 The output looks like: "ls" "-la" . output 看起来像: "ls" "-la"

How can I format this so that it doesn't enclose each arg in double quotes, but instead produces a command that is easy to read?我该如何格式化它,以便它不会将每个 arg 括在双引号中,而是生成一个易于阅读的命令? Something like: ls -la .像: ls -la

I saw a related issue , but it comes to the same mediocre solution.我看到了一个相关的问题,但它涉及到同样平庸的解决方案。

Command adds the quotes because, while they're "ugly," they never break a command. Command 添加引号是因为,虽然它们“丑陋”,但它们从不破坏命令。 On the other hand, not having them can, Parsing whether you need them or not, including all edge cases.另一方面,没有它们可以,解析你是否需要它们,包括所有边缘情况。 can get surprisingly complicated.会变得异常复杂。

If you have a tightly controlled set of use cases, and you are certain that you don't need them, then just remove them:如果您有一组严格控制的用例,并且您确定不需要它们,那么只需删除它们:

let mut command = std::process::Command::new("ls");
command.arg("-la");
println!("{}", format!("{:?}", command).replace("\"", ""));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将std :: process :: Command转换为命令行字符串? - How can I convert a std::process::Command into a command line string? 如何在 Rust 中以字符串形式获取完整的 std::process::Command 调用? - How to get full std::process::Command invocation as string in Rust? 如何将stderr和stdin交错到std :: process :: Command? - How to interleave stderr and stdin to a std::process::Command? 如何将带有多个参数的单个字符串传递给 std::process::Command? - How do I pass a single string with multiple arguments to std::process::Command? 如何在 Rust 中获取 std::process::Command 后面的命令? - How to get the command behind std::process::Command in Rust? 如何在 rust 中向 std::process::Command 发送提示输入? - How to send prompt input to std::process::Command in rust? 没有来自 std::process::Command 的 output - No output from std::process::Command 是否有比`std :: process :: Command`更低级别的接口来将单个字符串作为进程执行? - Is there a lower level interface than `std::process::Command` to execute a single string as a process? 使用Rust std :: process :: Command安装NPM软件包 - Installing NPM package with Rust std::process::Command 无法使用 std::process::Command - 没有这样的文件或目录 - Unable to use std::process::Command - No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM