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 HelpCommand CreateCommand() { StringBuilder sb = new StringBuilder(); sb.Append(CanvasCommand.GetHelp()); sb.Append(Environment.NewLine); sb.Append(Environment.NewLine); sb.Append(LineCommand.GetHelp()); sb.Append(Environment.NewLine); sb.Append(Environment.NewLine); sb.Append(RectangleCommand.GetHelp()); sb.Append(Environment.NewLine); sb.Append(Environment.NewLine); sb.Append(QuitCommand.GetHelp()); HelpCommand command = new HelpCommand(sb.ToString()); return(command); }
internal static LineCommand CreateCommand( string[] parameters ) { // Validate Mandatory if (parameters.Length != 4) { throw new InvalidCommandParamsException("Expected 4 parameters but received " + parameters.Length + "\n" + GetHelp()); } if ( parameters[0] != parameters[2] && parameters[1] != parameters[3] ) { throw new InvalidCommandParamsException("Line must be horizontal or vertical. \n" + GetHelp()); } try { int x1 = Utils.FormatPositiveInt(parameters[0]); int y1 = Utils.FormatPositiveInt(parameters[1]); int x2 = Utils.FormatPositiveInt(parameters[2]); int y2 = Utils.FormatPositiveInt(parameters[3]); LineCommand command = new LineCommand( x1, y1, x2, y2 ); return(command); } catch (Exception ex) { throw new InvalidCommandParamsException("Invalid parameter/s " + String.Join(" ", parameters) + ". \n" + GetHelp(), ex); } }