/// <summary> /// Attempts to execute a command with the specified argument text. /// <para>This text should be the contents of the command line, without this <see cref="GlobalCommand"/>'s command name.</para> /// <para>Example: 'sampleCommand verb defaultValue --flag --operand operandValue'</para> /// <para>This allows commands to be executed from a source other than the command prompt.</para> /// </summary> /// <param name="args">The command line arguments, including the command, verb, defaultValue, operands, and flags.</param> public void ExecuteCommand(string args) { try { var command = commandRegex.Match(args); if (command.Success) { var cmdName = command.Groups["name"].Value.ToLower(System.Globalization.CultureInfo.InvariantCulture).Trim(); var verb = command.Groups["verb"].Value.ToLower(System.Globalization.CultureInfo.InvariantCulture).Trim(); var defaultValue = command.Groups["defaultValue"].Value.Trim(); var result = parametersRegex.Matches(command.Groups["parameters"].Value.Trim()); var operands = new Dictionary <string, string>(); for (var i = 0; i < result.Count; i++) { if (result[i].Success && result[i].Groups["name"].Length > 0) { var val = result[i].Groups["name"].Value.ToLower().Trim(); if (!operands.ContainsKey(val)) { operands.Add(val, result[i].Groups["value"].Value); } else { WriteErrorMethod(string.Format("The '{0}' operand or flag was used more than once.\r\n", result[i].Groups["name"].Value)); WriteErrorMethod(string.Format("Duplicate operand or flag names are not allowed!")); return; } } else if (result[i].Success && result[i].Groups["shortName"].Length > 0) { var values = result[i].Groups["shortName"].Value.ToCharArray(); for (var j = 0; j < values.Length; j++) { if (!operands.ContainsKey(values[j].ToString())) { operands.Add(values[j].ToString(), string.Empty); } } } } ProcessCommand(cmdName, verb, defaultValue, operands); } else if (args.ToLower().Contains("--help") || args.ToLower().Contains("-h")) { PrintGlobalCommandsHelp(); } else { WriteErrorMethod("You must enter the name of a command. Enter '--help' for a list of available commands."); } } catch (TerminalCommandException e) { var cee = CommandExceptionEncountered; if (cee != null) { cee.Invoke(this, new TerminalCommandExceptionEventArgs(e, args)); } else { throw e; } } catch (Exception ex) { var e = new TerminalCommandException(ex, string.Format("Exception encountered while executing Terminal Command: '{0}'.", args)); var cee = CommandExceptionEncountered; if (cee != null) { cee.Invoke(this, new TerminalCommandExceptionEventArgs(e, args)); } else { ProConsole.WriteLine(); ProConsole.WriteError("Exception encountered while executing Terminal Command: '{0}'.\r\n{1}", args, ex.ToString()); } } }
/// <summary> /// Initializes a new instance of the <see cref="TerminalCommandExceptionEventArgs"/> class. /// </summary> /// <param name="exception">The <see cref="TerminalCommandException"/> to provide to the event.</param> /// <param name="commandContent">The content of the command that was being executed when the exception occurred.</param> public TerminalCommandExceptionEventArgs(TerminalCommandException exception, string commandContent) { Exception = exception; CommandContent = commandContent; }