[英]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.