Exemplo n.º 1
0
        /// <summary>
        /// This method checks if the command line has a valid length and a valid editor command in the first place.
        /// </summary>
        /// <param name="commandLine">The command line the user has written.</param>
        /// <returns>
        /// A full instanced command if the command line is valid or null if the command line is not valid.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If commandLine is null or whitespace.
        /// </exception>
        public IEditorCommand Parse(string commandLine)
        {
            if (string.IsNullOrWhiteSpace(commandLine))
            {
                throw new ArgumentNullException();
            }

            string[] possibleCommands = commandLine.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string   editorCommand    = possibleCommands[0].ToLower();

            if (possibleCommands.Length > 4)
            {
                return(null);
            }
            else
            {
                switch (editorCommand)
                {
                case "add":
                    return(AddCommand.Parse(commandLine));

                case "clear":
                    return(ClearCommand.Parse(commandLine));

                case "insert":
                    return(InsertCommand.Parse(commandLine));

                case "remove":
                    return(RemoveCommand.Parse(commandLine));

                case "new":
                    return(NewCommand.Parse(commandLine));

                case "run":
                    return(RunCommand.Parse(commandLine));

                default:
                    return(null);
                }
            }
        }