Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

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.

like image 235
counterbeing Avatar asked Feb 05 '26 04:02

counterbeing


1 Answers

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("\"", ""));
like image 196
GrandOpener Avatar answered Feb 06 '26 22:02

GrandOpener



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!