using System; using System.Collections.Generic; using System.Linq; using System.Text; using RsenkTest.Commands; namespace RsenkTest { class Interpreter { private List _commands; public Interpreter(List commands) { _commands = commands; } /// /// Parses the command off. Reads everything up to the first space. /// /// /// Returns the command if it exists, otherwise null. public CommandBase ParseCommand(string line) { string command = line.Substring(0, line.IndexOf(' ')); CommandBase commandToRet = CheckCommand(command); return commandToRet; } /// /// Checks to see if the command is valid. /// /// /// Returns the command in the form of CommandBase if found, otherwise returns null. private CommandBase CheckCommand(string comm) { CommandBase commandToRet = _commands.Find(delegate(CommandBase command) { return command.Name.Equals(comm); }); return commandToRet; } public ParameterBase[] ParseParameters(CommandBase command, string line) { ParameterBase[] parameters = new ParameterBase[0]; line = line.Substring(command.Name.Length + 1).Trim(); bool invalidArg = false; if ((command != null) && (String.IsNullOrEmpty(line))) { List paramsTemp = new List(); while (line.Length > 0) { string param = line.Substring(0, line.IndexOf(' ')); Console.WriteLine(param); line = line.Substring(line.IndexOf(' ')); Console.WriteLine(line); if (param.Equals(line)) { break; } ParameterBase temp = ValidParam(command, param); if (temp != null) paramsTemp.Add(temp); else { invalidArg = true; break; } } if (line.Trim().Length > 0) { ParameterBase temp = ValidParam(command, line); if (temp != null) paramsTemp.Add(temp); else invalidArg = true; } if (!invalidArg) { parameters = paramsTemp.ToArray(); } } return parameters; } private ParameterBase ValidParam(CommandBase command, string paramToCheck) { ParameterBase param = command.Parameters.Find(delegate(CommandBase parameter) { return parameter.Name.Equals(paramToCheck); }) as ParameterBase; return param; } } }