Changed what and when exceptions are thrown.

These changes were made to better fit the new structure. Parameter checks were added and the type thrown was changed as well.
This commit is contained in:
Harrison Deng 2020-02-29 04:29:40 -05:00
parent de7a11a530
commit 3085416efd

View File

@ -21,8 +21,10 @@ namespace RecrownedGTK.Tools.CommandProcessor
protected EngineCommandArgument[] commandArguments; protected EngineCommandArgument[] commandArguments;
protected string help; protected string help;
public EngineCommand(params string[] invokeStrings) public EngineCommand(string help, params string[] invokeStrings)
{ {
this.help = help ?? throw new ArgumentNullException();
this.invokeStrings = invokeStrings ?? throw new ArgumentNullException(); this.invokeStrings = invokeStrings ?? throw new ArgumentNullException();
for (int i = 0; i < invokeStrings.Length; i++) { for (int i = 0; i < invokeStrings.Length; i++) {
if (invokeStrings[i].Contains(" ")) { if (invokeStrings[i].Contains(" ")) {
@ -92,23 +94,25 @@ namespace RecrownedGTK.Tools.CommandProcessor
/// <param name="argument">The argument to find the index of.</param> /// <param name="argument">The argument to find the index of.</param>
/// <param name="arguments">The array containing all arguments.</param> /// <param name="arguments">The array containing all arguments.</param>
/// <returns>The index or throws argument exception if it doesn't exist.</returns> /// <returns>The index or throws argument exception if it doesn't exist.</returns>
public int IndexOfArgument(string argument, string[] arguments) public static int IndexOfArgument(string argument, string[] arguments)
{ {
if (arguments != null) if (argument == null) throw new ArgumentNullException("Parameter \"argument\" is null.");
if (arguments == null) throw new ArgumentNullException("Parameter \"arguments\" is null.");
for (int i = 0; i < arguments.Length; i++)
{ {
for (int i = 0; i < arguments.Length; i++) if (arguments[i] == argument)
{ {
if (arguments[i] == argument) return i;
{
return i;
}
} }
} }
throw new ArgumentException("Expected argument " + argument + " is missing. Type \"help\" for more information."); throw new ArgumentException(String.Format("The argument \"{0}\" doesn't exist in the given array of arguments.", argument));
} }
public bool HasArgument(string argument, string[] arguments) public static bool HasArgument(string argument, string[] arguments)
{ {
if (argument == null) throw new ArgumentNullException("Parameter \"argument\" is null.");
if (arguments == null) throw new ArgumentNullException("Parameter \"arguments\" is null.");
try try
{ {
IndexOfArgument(argument, arguments); IndexOfArgument(argument, arguments);
@ -120,7 +124,7 @@ namespace RecrownedGTK.Tools.CommandProcessor
return true; return true;
} }
public bool HasArgument(EngineCommandArgument argument, string[] arguments) public static bool HasArgument(EngineCommandArgument argument, string[] arguments)
{ {
return HasArgument(argument.invokeString, arguments); return HasArgument(argument.invokeString, arguments);
} }
@ -131,7 +135,7 @@ namespace RecrownedGTK.Tools.CommandProcessor
/// <param name="argument">The argument to find the index of.</param> /// <param name="argument">The argument to find the index of.</param>
/// <param name="arguments">The array containing all arguments.</param> /// <param name="arguments">The array containing all arguments.</param>
/// <returns>The index or throws argument exception if it doesn't exist.</returns> /// <returns>The index or throws argument exception if it doesn't exist.</returns>
public int IndexOfArgumentIn(EngineCommandArgument argument, string[] arguments) public static int IndexOfArgument(EngineCommandArgument argument, string[] arguments)
{ {
return IndexOfArgument(argument.invokeString, arguments); return IndexOfArgument(argument.invokeString, arguments);
} }
@ -164,7 +168,7 @@ namespace RecrownedGTK.Tools.CommandProcessor
if (commandArguments != null) if (commandArguments != null)
{ {
helpBuilder.AppendLine(); helpBuilder.AppendLine();
helpBuilder.Append("Possible arguments are: "); helpBuilder.Append("Possible arguments: ");
for (int i = 0; i < commandArguments.Length; i++) for (int i = 0; i < commandArguments.Length; i++)
{ {
helpBuilder.Append(commandArguments[i].invokeString); helpBuilder.Append(commandArguments[i].invokeString);
@ -179,7 +183,7 @@ namespace RecrownedGTK.Tools.CommandProcessor
} }
} }
helpBuilder.Append('.'); helpBuilder.Append('.');
helpBuilder.AppendLine(" [* are required arguments.]"); helpBuilder.AppendLine(" (* are required arguments.)");
} }
return helpBuilder.ToString(); return helpBuilder.ToString();
} }