splitCommand static method
Splits a shell command string into tokens.
Handles single-quoted ('…') and double-quoted ("…") groups so that
arguments containing spaces can be expressed as a single string:
'nginx -c "/etc/nginx/nginx.conf"' → ['nginx', '-c', '/etc/nginx/nginx.conf'].
Exposed for testing only. Prefer withCommand with a List<String>
for production use.
Implementation
@visibleForTesting
static List<String> splitCommand(String command) {
final result = <String>[];
final current = StringBuffer();
var inSingleQuote = false;
var inDoubleQuote = false;
for (var i = 0; i < command.length; i++) {
final char = command[i];
if (inSingleQuote) {
if (char == "'") {
inSingleQuote = false;
} else {
current.write(char);
}
} else if (inDoubleQuote) {
if (char == '"') {
inDoubleQuote = false;
} else {
current.write(char);
}
} else if (char == "'") {
inSingleQuote = true;
} else if (char == '"') {
inDoubleQuote = true;
} else if (char == ' ' || char == '\t') {
if (current.isNotEmpty) {
result.add(current.toString());
current.clear();
}
} else {
current.write(char);
}
}
if (current.isNotEmpty) {
result.add(current.toString());
}
return result;
}