public void Decode() { FullCommand = FullCommand.Replace("---", "\""); Command = Command.Replace("---", "\""); ArgString = ArgString.Replace("---", "\""); var args = new Dictionary <string, string>(); foreach (var arg in Args) { args.Add(arg.Key.Replace("---", "\""), arg.Value.Replace("---", "\"")); } Args = args; var switches = new Dictionary <string, string>(); foreach (var switch1 in Switches) { switches.Add(switch1.Key.Replace("---", "\""), switch1.Value.Replace("---", "\"")); } Switches = switches; for (var i = 0; i < FreeArgs.Count; i++) { FreeArgs[i] = FreeArgs[i].Replace("---", "\""); } for (var i = 0; i < SimpleArgs.Count; i++) { SimpleArgs[i] = SimpleArgs[i].Replace("---", "\""); } }
public bool TryDivide(string fullCmd) { CommandName = fullCmd.Split(' ')[0].Trim(); ArgString = fullCmd.IndexOf(" ", StringComparison.Ordinal) == -1 ? "" : fullCmd.Substring(fullCmd.IndexOf(" ", StringComparison.Ordinal) + 1, fullCmd.Length - CommandName.Length - 1).Trim(); List <string> splitedParam = new List <string>(); SimpleArgs = ArgString.Split(' ').ToList(); if (ArgString == "") { return(false); } try { splitedParam.AddRange(ArgString.Split(' ')); foreach (var item in splitedParam) { if (Quote.Any(q => ContainsChar(q, item))) { throw new ArgumentException(); } } bool combined = true; foreach (var item in Quote) { for (int i = 0; i < splitedParam.Count - 1; i++) { string cur = splitedParam[i], next = splitedParam[i + 1]; if (cur.StartsWith(item) && !cur.EndsWith(item)) { combined = false; splitedParam[i] = cur + " " + next; splitedParam.Remove(next); if (splitedParam[i].EndsWith(item)) { combined = true; } i--; } } if (!combined) { throw new ArgumentException("Expect '" + item + "'."); } } string tmpKey = null, tmpValue; bool isLastKeyOrValue = false; splitedParam.Add("-"); foreach (var item in splitedParam) { tmpValue = null; if (item.StartsWith('-')) { if (tmpKey != null) { Switches.Add(tmpKey, tmpKey); } tmpKey = item.Remove(0, 1); isLastKeyOrValue = true; } else { foreach (var q in Quote) { tmpValue = tmpValue == null?item.Trim(q) : tmpValue.Trim(q); } if (!isLastKeyOrValue) { FreeArgs.Add(tmpValue); //throw new ArgumentException("Expect key."); } else { Args.Add(tmpKey, tmpValue); tmpKey = null; tmpValue = null; isLastKeyOrValue = false; } } } } catch (Exception ex) { Logger.Debug(ex.Message); return(false); } return(true); }