internal static IEnumerable <string> GetArgDescriptions(this CommandProperty cmdProperty) { string[] desc = null; try { desc = JsonConvert.DeserializeObject <string[]>(cmdProperty.ArgDescriptionArray); } catch (Exception) { ConsoleX.WriteError(new ArgumentException($"The commands Argument Description of \"{ cmdProperty.ArgDescriptionArray }\" is not of string format containing an array or strings")); } for (var i = 0; i < cmdProperty._propertyInfo.PropertyType.GetGenericArguments().Length; i++) { var argument = cmdProperty._propertyInfo.PropertyType.GetGenericArguments()[i]; yield return(cmdProperty._propertyInfo.PropertyType.Name.Contains("Func") && i != 0 ? $"return[{i}]({argument.Name}): { desc?.ElementAtOrDefault(i) ?? "No return description" }" : $"argument[{i}]({argument.Name}): { desc?.ElementAtOrDefault(i) ?? "No argument description" }"); } }
public static void WriteCommandHelp(string arg) { var commandProps = Handler.GetAllCommandsProperties().Where(c => c.IsCommand(arg)); if (!commandProps.Any()) { ConsoleX.WriteError(new Exception($"command does not exist"), ConsoleX.LogLevel.basic); return; } foreach (var commandProp in commandProps) { ConsoleX.WriteLine($"short name : { commandProp.Command.ShortName }"); ConsoleX.WriteLine($"long name : { commandProp.Command.LongName }"); ConsoleX.WriteLine($"description : { commandProp.Command.Description } "); if (!commandProp.HasArguments()) { ConsoleX.WriteLine($"no arguments"); continue; } ConsoleX.WriteLines(commandProp.GetArgDescriptions()); } }
public static object Invoke(this CommandProperty cmdProperty, IEnumerable <object> arguments) { try { var cmdMethod = cmdProperty._propertyInfo.PropertyType.GetMethod("Invoke"); var cmdObj = cmdProperty._propertyInfo.GetValue(cmdProperty._commandContainer); return(cmdMethod.Invoke(cmdObj, cmdProperty.CheckArguments(cmdMethod, arguments ?? new object[] { }).ToArray())); } catch (TargetParameterCountException) { ConsoleX.WriteError(new TargetParameterCountException($"The argument count for the command '{ cmdProperty.LongName ?? cmdProperty.ShortName }' does not match. " + $"Single arguments should be passed in as a single string and multiple should be passed in as an array. " + $"eg [ arg1, arg2 , arg3 ]"), ConsoleX.LogLevel.basic); return(null); } catch (Exception e) { ConsoleX.WriteError(new MethodAccessException($"There has been an unexpected error with the command '{ cmdProperty.LongName ?? cmdProperty.ShortName }'", e), ConsoleX.LogLevel.basic); return(null); } }