简体   繁体   中英

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 . 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" .

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? Something like: 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. 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("\"", ""));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM