private static ConsoleMethod GetConsoleMethod(ParsedArguments arguments, Type type, string key, object instance = null) { string commandLineSwitch = key; string switchValue = arguments[key]; MethodInfo[] methods = type.GetMethods(); List <ConsoleMethod> toExecute = new List <ConsoleMethod>(); foreach (MethodInfo method in methods) { if (method.HasCustomAttributeOfType(out ConsoleActionAttribute consoleAction)) { if (consoleAction.CommandLineSwitch.Or("").Equals(commandLineSwitch) || consoleAction.CommandLineSwitch.CaseAcronym().ToLowerInvariant().Or("").Equals(commandLineSwitch)) { toExecute.Add(new ConsoleMethod(method, consoleAction, instance, switchValue)); } } } Expect.IsFalse(toExecute.Count > 1, "Multiple ConsoleActions found with the specified command line switch: {0}"._Format(commandLineSwitch)); if (toExecute.Count == 0) { return(null); } return(toExecute[0]); }
/// <summary> /// Execute the methods on the specified instance that are addorned with ConsoleAction /// attributes that have CommandLineSwitch(es) defined that match keys in the /// specified ParsedArguments using the specified ILogger to report any switches not /// found. An ExpectFailedException will be thrown if more than one method is found /// with a matching CommandLineSwitch defined in ConsoleAction attributes /// </summary> /// <param name="arguments"></param> /// <param name="type"></param> /// <param name="warnForNotFoundSwitches"></param> /// <param name="instance"></param> /// <param name="logger"></param> /// <returns>true if command line switches were executed otherwise false</returns> public static bool ExecuteSwitches(ParsedArguments arguments, Type type, bool warnForNotFoundSwitches = true, object instance = null, ILogger logger = null) { bool executed = false; foreach (string key in arguments.Keys) { ConsoleMethod methodToInvoke = GetConsoleMethod(arguments, type, key, instance); if (methodToInvoke != null) { if (IsolateMethodCalls) { methodToInvoke.InvokeInSeparateAppDomain(); } else { methodToInvoke.InvokeInCurrentAppDomain(); } executed = true; logger?.AddEntry("Executed {0}: {1}", key, methodToInvoke.Information); } else { if (logger != null && warnForNotFoundSwitches) { logger.AddEntry("Specified command line switch was not found {0}", LogEventType.Warning, key); } } } return(executed); }
/// <summary> /// Execute the methods on the specified instance that are addorned with ConsoleAction /// attributes that have CommandLineSwitch(es) defined that match keys in the /// specified ParsedArguments using the specified ILogger to report any switches not /// found. An ExpectFailedException will be thrown if more than one method is found /// with a matching CommandLineSwitch defined in ConsoleAction attributes /// </summary> /// <param name="arguments"></param> /// <param name="type"></param> /// <param name="isolateMethodCalls"></param> /// <param name="logger"></param> /// <returns></returns> public static bool ExecuteSwitches(ParsedArguments arguments, Type type, bool isolateMethodCalls, ILogger logger = null) { bool originalValue = IsolateMethodCalls; IsolateMethodCalls = isolateMethodCalls; bool result = ExecuteSwitches(arguments, type, false, null, logger); IsolateMethodCalls = originalValue; return(result); }
protected static void ParseArgs(string[] args) { Arguments = new ParsedArguments(args, ValidArgumentInfo.ToArray()); if (Arguments.Status == ArgumentParseStatus.Error || Arguments.Status == ArgumentParseStatus.Invalid) { ArgsParsedError?.Invoke(Arguments); } else if (Arguments.Status == ArgumentParseStatus.Success) { ArgsParsed?.Invoke(Arguments); } }
public CommandLineArgumentConfigurer(ParsedArguments arguments) { this.Arguments = arguments; }
/// <summary> /// Execute the methods on the specified instance that are addorned with ConsoleAction /// attributes that have CommandLineSwitch(es) defined that match keys in the /// specified ParsedArguments using the specified ILogger to report any switches not /// found. An ExpectFailedException will be thrown if more than one method is found /// with a matching CommandLineSwitch defined in ConsoleAction attributes /// </summary> /// <param name="arguments"></param> /// <param name="type"></param> /// <param name="instance"></param> /// <param name="logger"></param> /// <returns></returns> public static bool ExecuteSwitches(ParsedArguments arguments, Type type, object instance = null, ILogger logger = null) { return(ExecuteSwitches(arguments, type, true, instance, logger)); }
/// <summary> /// Execute the methods on the specified instance that are addorned with ConsoleAction /// attributes that have CommandLineSwitch(es) defined that match keys in the /// specified ParsedArguments using the specified ILogger to report any switches not /// found. An ExpectFailedException will be thrown if more than one method is found /// with a matching CommandLineSwitch defined in ConsoleAction attributes /// </summary> /// <param name="arguments"></param> /// <param name="instance"></param> /// <param name="logger"></param> public static bool ExecuteSwitches(ParsedArguments arguments, object instance, ILogger logger = null) { Expect.IsNotNull(instance, "instance can't be null, use a Type if executing static method"); return(ExecuteSwitches(arguments, instance.GetType(), instance, logger)); }