arg function

String arg(
  1. Object argument
)

Converts argument to a string and escapes it so it's parsed as a single argument with no glob expansion by new Script and related functions.

For example, run("cp -r ${arg(source)} build/").

Implementation

String arg(Object argument) {
  var string = argument.toString();
  if (string.isEmpty) return '""';

  var buffer = StringBuffer();
  for (var i = 0; i < string.length; i++) {
    var codeUnit = string.codeUnitAt(i);
    if (codeUnit == $space ||
        codeUnit == $double_quote ||
        codeUnit == $single_quote ||
        codeUnit == $backslash ||
        codeUnit == $asterisk ||
        codeUnit == $question ||
        codeUnit == $lbracket ||
        codeUnit == $rbracket ||
        codeUnit == $lbrace ||
        codeUnit == $rbrace ||
        codeUnit == $lparen ||
        codeUnit == $rparen ||
        codeUnit == $comma) {
      buffer.writeCharCode($backslash);
    }
    buffer.writeCharCode(codeUnit);
  }
  return buffer.toString();
}