using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace pmtest_client { public class CommandLineSplit { public static IEnumerable SplitArgs(string commandLine) { var result = new StringBuilder(); var quoted = false; var escaped = false; var started = false; var allowcaret = false; for (int i = 0; i < commandLine.Length; i++) { var chr = commandLine[i]; if (chr == '^' && !quoted) { if (allowcaret) { result.Append(chr); started = true; escaped = false; allowcaret = false; } else if (i + 1 < commandLine.Length && commandLine[i + 1] == '^') { allowcaret = true; } else if (i + 1 == commandLine.Length) { result.Append(chr); started = true; escaped = false; } } else if (escaped) { result.Append(chr); started = true; escaped = false; } else if (chr == '"') { quoted = !quoted; started = true; } else if (chr == '\\' && i + 1 < commandLine.Length && commandLine[i + 1] == '"') { escaped = true; } else if (chr == ' ' && !quoted) { if (started) yield return result.ToString(); result.Clear(); started = false; } else { result.Append(chr); started = true; } } if (started) yield return result.ToString(); } } }