argumentToString function
Helper to run a process and connect the input/output for verbosity
Helper to run a process and connect the input/output for verbosity
Use to safely enclose an argument if needed
argument must not be null
Implementation
/// Helper to run a process and connect the input/output for verbosity
///
/// Use to safely enclose an argument if needed
///
/// argument must not be null
String argumentToString(String argument) {
var hasWhitespace = false;
var singleQuoteCount = 0;
var doubleQuoteCount = 0;
if (argument.isEmpty) {
return '""';
}
for (final rune in argument.runes) {
if ((!hasWhitespace) && (isWhitespace(rune))) {
hasWhitespace = true;
} else if (rune == 0x0027) {
// '
singleQuoteCount++;
} else if (rune == 0x0022) {
// "
doubleQuoteCount++;
}
}
if (singleQuoteCount > 0) {
if (doubleQuoteCount > 0) {
// simply escape all double quotes
argument = '"${argument.replaceAll('"', '\\"')}"';
} else {
argument = '"$argument"';
}
} else if (doubleQuoteCount > 0) {
argument = "'$argument'";
} else if (hasWhitespace) {
argument = '"$argument"';
}
return argument;
}