/// <summary> /// clears all <see cref="UserDefinedCommand"/>s /// from <see cref="UserDefinedCommand"/> repository. /// </summary> /// <param name="commandArguments"></param> /// <returns> /// <seealso cref="Command.Execute(string[])"/> /// </returns> protected override bool Execute(string[] commandArguments) { bool commandExecutedSuccessfuly; try { // remove all UserDefinedCommands ConfigurationManager.Instance.ClearUserDefinedCommands(); // log success notice string successNotice = "All User Defined Commands were successfully removed."; LogCommandNotice(successNotice); commandExecutedSuccessfuly = true; } catch (UserDefinedCommandsUpdateException userDefinedCommandsUpdateException) { UserDefinedCommandsCommand.HandleUserDefinedCommandsUpdateException( userDefinedCommandsUpdateException); commandExecutedSuccessfuly = false; } return(commandExecutedSuccessfuly); }
/// <summary> /// removes <see cref="UserDefinedCommand"/> corresponding to alias /// specified as a command argument /// from <see cref="UserDefinedCommand"/> repository. /// </summary> /// <param name="commandArguments"></param> /// <returns> /// <seealso cref="Command.Execute(string[])"/> /// </returns> protected override bool Execute(string[] commandArguments) { bool commandExecutedSuccessfuly; string userDefinedCommandAlias = commandArguments[0]; // UserDefinedCommand with the specified alias exists if (ConfigurationManager.Instance.UserDefinedCommandExists(userDefinedCommandAlias)) { try { // remove UserDefinedCommand with specified alias ConfigurationManager.Instance.RemoveUserDefinedCommand(userDefinedCommandAlias); // log successful removal notice string successNotice = string.Format( "User Defined Command with alias '{0}' removed successfully.", userDefinedCommandAlias); LogCommandNotice(successNotice); commandExecutedSuccessfuly = true; } catch (UserDefinedCommandsUpdateException userDefinedCommandsUpdateException) { UserDefinedCommandsCommand.HandleUserDefinedCommandsUpdateException( userDefinedCommandsUpdateException); commandExecutedSuccessfuly = false; } } else // UserDefinedCommand with the specified alias does not exist { string errorMessage = string.Format( "User Defined Command with alias '{0}' not found.", userDefinedCommandAlias); Command.LogCommandError(errorMessage); commandExecutedSuccessfuly = false; } return(commandExecutedSuccessfuly); }
/// <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); }