/// <summary> /// Parse string line to array of argument /// </summary> /// <param name="commandLine"></param> /// <returns>Array from string that represent arguments</returns> public static string[] Parse(string commandLine) { var args = new Arguments(); char lastChar = default(char); var enumerator = commandLine.ToArray().GetEnumerator(); while (enumerator.MoveNext()) { var curChar = (char)enumerator.Current; var isQuoteChar = IsQuoteChar(curChar, out var quoteType); var lastIsScapeChar = IsScapeChar(lastChar); // check if is a phase cenary; have quotes // 1) Is quote char (" or ') // 2) AND the last is not a scape char (\) var beginQuote = isQuoteChar && !lastIsScapeChar; if (!beginQuote) { if (curChar == ' ') { args.CloseArgument(); } else { if (lastIsScapeChar && isQuoteChar) { args.RemoveLastChar(); } args.AddInCurrent(curChar); } } else { // if here, the current char is a quote " or ' while (enumerator.MoveNext()) { curChar = (char)enumerator.Current; if (curChar == quoteType) { if (IsScapeChar(lastChar)) { args.RemoveLastChar(); args.AddInCurrent(quoteType); } else { // prevent empty quote // cmd> --arg1 -> 1 args // cmd> --arg1 "" -> 2 args if (args.GetCurrentChars().Length == 0) { args.AddInCurrent('\0'); } break; } } else { args.AddInCurrent(curChar); } lastChar = curChar; } } lastChar = curChar; } args.CloseArgument(); return(args.GetArguments()); }