Skip to content

Commit

Permalink
Merge pull request #175 from awilfox/awilfox/fix-version-param-output
Browse files Browse the repository at this point in the history
Quote WrappedCommand arguments with spaces in them
  • Loading branch information
sdroege authored Jan 12, 2025
2 parents 91fcbdb + 3a63f27 commit 3314c8d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,21 @@ impl WrappedCommand {
}
}

/// Quote an argument that has spaces in it.
/// When our `WrappedCommand` is printed to the terminal, arguments that contain spaces needed to be quoted.
/// Otherwise, we will have output such as:
/// `pkg-config --libs --cflags foo foo < 3.11`
/// 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.
/// Using this function, we instead get the correct output:
/// `pkg-config --libs --cflags foo 'foo < 3.11'`
fn quote_if_needed(arg: String) -> String {
if arg.contains(' ') {
format!("'{}'", arg)
} else {
arg
}
}

/// Output a command invocation that can be copy-pasted into the terminal.
/// `Command`'s existing debug implementation is not used for that reason,
/// as it can sometimes lead to output such as:
Expand All @@ -248,7 +263,7 @@ impl Display for WrappedCommand {
let args = self
.args
.iter()
.map(|arg| arg.to_string_lossy().to_string())
.map(|arg| quote_if_needed(arg.to_string_lossy().to_string()))
.collect::<Vec<String>>()
.join(" ");

Expand Down

0 comments on commit 3314c8d

Please sign in to comment.