1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- 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<string> 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();
- }
- }
- }
|