/// <summary>
            /// adds specified <see cref="UserDefinedCommand"/>.
            /// </summary>
            /// <seealso cref="updateUserDefinedCommandContainerDataFile"/>
            /// <seealso cref="UserDefinedCommandContainer.AddUserDefinedCommand(UserDefinedCommand)"/>
            /// <param name="userDefinedCommand"></param>
            /// <exception cref="UserDefinedCommandsUpdateException">
            /// <seealso cref="updateUserDefinedCommandContainerDataFile"/>
            /// </exception>
            /// <exception cref="ConifgurationManagerNotInitializedException">
            /// <seealso cref="assertManagerInitialized(string)"/>
            /// </exception>
            public void AddUserDefinedCommand(UserDefinedCommand userDefinedCommand)
            {
                assertManagerInitialized("AddUserDefinedCommand");

                // write updated UserDefinedCommandContainer data to file
                updateUserDefinedCommandContainerDataFile();

                // add to UserDefinedCommandContainer
                this.userDefinedCommandContainer.AddUserDefinedCommand(userDefinedCommand);
            }
Beispiel #2
0
            /// <summary>
            /// prints data of
            /// <see cref="UserDefinedCommand"/> from <see cref="UserDefinedCommand"/> repository,
            /// corresponding to specified alias, or all <see cref="UserDefinedCommand"/>s in repository
            /// if no alias was specified.
            /// </summary>
            /// <param name="commandArguments"></param>
            /// <returns>
            /// <seealso cref="Command.Execute(string[])"/>
            /// </returns>
            protected override bool Execute(string[] commandArguments)
            {
                bool commandExecutedSuccessfuly;

                int numOfCommandArguments = commandArguments.Length;

                string        notice = null;
                StringBuilder UserDefinedCommandDataStringBuilder = new StringBuilder();

                if (numOfCommandArguments == 0) // user did not specify a particular alias
                {
                    notice = "Existing User Defined Commands:";

                    // display all UserDefinedCommands aliases with their corresponding command strings

                    // no UserDefinedCommands available
                    if (ConfigurationManager.Instance.UserDefinedCommands.Length == 0)
                    {
                        UserDefinedCommandDataStringBuilder.Append("No User Defined Commands were added.");
                    }
                    else // ConfigurationManager.Instance.UserDefinedCommands.Length > 0
                    {
                        for (int i = 0; i < ConfigurationManager.Instance.UserDefinedCommands.Length; i++)
                        {
                            UserDefinedCommand userDefinedCommand =
                                ConfigurationManager.Instance.UserDefinedCommands[i];
                            UserDefinedCommandDataStringBuilder.AppendFormat(
                                "{0}. {1} - '{2}'",
                                i + 1,
                                userDefinedCommand.Alias,
                                userDefinedCommand.CommandString);

                            if (i < ConfigurationManager.Instance.UserDefinedCommands.Length - 1)
                            {
                                UserDefinedCommandDataStringBuilder.Append(Environment.NewLine);
                            }
                        }
                    }

                    commandExecutedSuccessfuly = true;
                }
                else // numOfCommandArguments == 1 - user specified a paritcular alias
                {
                    string userDefinedCommandAlias = commandArguments[0];

                    // UserDefinedCommand with specified alias found
                    if (ConfigurationManager.Instance.UserDefinedCommandExists(userDefinedCommandAlias))
                    {
                        UserDefinedCommand userDefinedCommand =
                            ConfigurationManager.Instance.GetUserDefinedCommand(userDefinedCommandAlias);

                        notice = string.Format(
                            "User Defined Command for alias '{0}':",
                            userDefinedCommandAlias);
                        UserDefinedCommandDataStringBuilder.AppendFormat(
                            "{0} - '{1}'",
                            userDefinedCommand.Alias,
                            userDefinedCommand.CommandString);

                        commandExecutedSuccessfuly = true;
                    }
                    else // UserDefinedCommand with specified alias not found
                    {
                        // log error message
                        string errorMessage = string.Format(
                            "User Defined Command with specified alias '{0}' not found.",
                            userDefinedCommandAlias);
                        Command.LogCommandError(errorMessage);

                        commandExecutedSuccessfuly = false;
                    }
                }

                // UserDefinedCommand corresponding to specified alias exists,
                // or user requested to view all UserDefinedCommands
                if (commandExecutedSuccessfuly)
                {
                    string userDefinedCommandData = UserDefinedCommandDataStringBuilder.ToString();

                    Command.LogCommandNotice(notice);
                    Command.PrintCommandData(userDefinedCommandData);
                }

                return(commandExecutedSuccessfuly);
            }
 /// <summary>
 /// adds specified <see cref="UserDefinedCommand"/>.
 /// </summary>
 /// <param name="userDefinedCommand"></param>
 internal void AddUserDefinedCommand(UserDefinedCommand userDefinedCommand)
 {
     this.userDefinedCommandAliasToUserDefinedCommand[userDefinedCommand.Alias] =
         userDefinedCommand;
 }
            /// <summary>
            /// adds specified <see cref="UserDefinedCommand"/> alias and corresponding command string
            /// to <see cref="UserDefinedCommand"/> repository as a new <see cref="UserDefinedCommand"/>.
            /// </summary>
            /// <param name="commandArguments"></param>
            /// <returns>
            /// <seealso cref="Command.Execute(string[])"/>
            /// </returns>
            protected override bool Execute(string[] commandArguments)
            {
                bool commandExecutedSuccessfuly;

                string userDefinedCommandAlias  = commandArguments[0];
                string userDefinedCommandString = commandArguments[1];

                // returns whether userDefinedCommandAlias starts with a reserved command prefix
                // supplied as argument
                Predicate <string> userDefinedCommandAliasContainsReservedCommandPrefix
                    = reservedCommandPrefix => userDefinedCommandAlias.StartsWith(reservedCommandPrefix);

                // UserDefinedCommand with the specified alias already exists
                if (ConfigurationManager.Instance.UserDefinedCommandExists(userDefinedCommandAlias))
                {
                    string errorMessage = string.Format(
                        "Cannot add command: A user defined command with alias '{0}' already exists.",
                        userDefinedCommandAlias);
                    Command.LogCommandError(errorMessage);

                    commandExecutedSuccessfuly = false;
                }
                // command alias starts with a reserved command prefix
                else if (CommandExecutor.ReservedCommandPrefixes.TrueForAny(
                             userDefinedCommandAliasContainsReservedCommandPrefix))
                {
                    // get reserved command prefix
                    string reservedCommandPrefix =
                        CommandExecutor.ReservedCommandPrefixes.FirstElementWhichSatisfies(
                            userDefinedCommandAliasContainsReservedCommandPrefix);

                    // log error message
                    string errorMessage = string.Format(
                        "Cannot add command: command prefix '{0}' is reserved.",
                        reservedCommandPrefix);
                    Command.LogCommandError(errorMessage);

                    commandExecutedSuccessfuly = false;
                }
                else // legal command alias
                {
                    try
                    {
                        // add UserDefinedCommand to ConfigurationManager
                        UserDefinedCommand savedCommand = new UserDefinedCommand(
                            userDefinedCommandAlias,
                            userDefinedCommandString);
                        ConfigurationManager.Instance.AddUserDefinedCommand(savedCommand);

                        // log success notice
                        string successNotice = string.Format(
                            "User Defined Command with alias '{0}' was added successfully.",
                            userDefinedCommandAlias);
                        Command.LogCommandNotice(successNotice);

                        commandExecutedSuccessfuly = true;
                    }
                    catch (UserDefinedCommandsUpdateException userDefinedCommandsUpdateException)
                    {
                        UserDefinedCommandsCommand.HandleUserDefinedCommandsUpdateException(
                            userDefinedCommandsUpdateException);

                        commandExecutedSuccessfuly = false;
                    }
                }

                return(commandExecutedSuccessfuly);
            }