public static CommandEntity GetCommand( string commandLine ) { string cleanedString = System.Text.RegularExpressions.Regex.Replace(commandLine.Trim(), @"\s+", " "); string[] split = cleanedString.Split(' '); int paramcount = split.Length - 1; string[] parameters = new string[paramcount]; Array.Copy(split, 1, parameters, 0, paramcount); switch (split[0].ToUpper()) { case "Q": return(QuitCommand.CreateCommand(parameters)); case "C": return(CanvasCommand.CreateCommand(parameters)); case "L": return(LineCommand.CreateCommand(parameters)); case "R": return(RectangleCommand.CreateCommand(parameters)); case "B": return(BucketCommand.CreateCommand(parameters)); default: string helpMessage = HelpCommand.CreateCommand().Message; throw new InvalidCommandException("Unknown command \n" + helpMessage); } }
internal static BucketCommand CreateCommand( string[] parameters ) { // Validate Mandatory if (parameters.Length != 3) { throw new InvalidCommandParamsException("Expected 3 parameters but received " + parameters.Length + "\n" + GetHelp()); } int xPos = -1; int yPos = -1; try { xPos = Utils.FormatPositiveInt(parameters[0]); } catch (Exception ex) { throw new InvalidCommandParamsException("Invalid x position: " + parameters[0] + ".\n" + GetHelp(), ex); } try { yPos = Utils.FormatPositiveInt(parameters[1]); } catch (Exception ex) { throw new InvalidCommandParamsException("Invalid y position: " + parameters[1] + ".\n" + GetHelp(), ex); } char[] cArray = parameters[2].ToCharArray(); if (cArray.Length > 1) { throw new InvalidCommandParamsException("Expected fill charcter parameter to be a single letter. Received: " + parameters[2] + "\n" + GetHelp()); } BucketCommand command = new BucketCommand( xPos, yPos, cArray[0] ); return(command); }