Skip to content

Commit 3314c8d

Browse files
authored
Merge pull request #175 from awilfox/awilfox/fix-version-param-output
Quote WrappedCommand arguments with spaces in them
2 parents 91fcbdb + 3a63f27 commit 3314c8d

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,21 @@ impl WrappedCommand {
227227
}
228228
}
229229

230+
/// Quote an argument that has spaces in it.
231+
/// When our `WrappedCommand` is printed to the terminal, arguments that contain spaces needed to be quoted.
232+
/// Otherwise, we will have output such as:
233+
/// `pkg-config --libs --cflags foo foo < 3.11`
234+
/// which cannot be used in a terminal - it will attempt to read a file named 3.11 and provide it as stdin for pkg-config.
235+
/// Using this function, we instead get the correct output:
236+
/// `pkg-config --libs --cflags foo 'foo < 3.11'`
237+
fn quote_if_needed(arg: String) -> String {
238+
if arg.contains(' ') {
239+
format!("'{}'", arg)
240+
} else {
241+
arg
242+
}
243+
}
244+
230245
/// Output a command invocation that can be copy-pasted into the terminal.
231246
/// `Command`'s existing debug implementation is not used for that reason,
232247
/// as it can sometimes lead to output such as:
@@ -248,7 +263,7 @@ impl Display for WrappedCommand {
248263
let args = self
249264
.args
250265
.iter()
251-
.map(|arg| arg.to_string_lossy().to_string())
266+
.map(|arg| quote_if_needed(arg.to_string_lossy().to_string()))
252267
.collect::<Vec<String>>()
253268
.join(" ");
254269

0 commit comments

Comments
 (0)