/// <summary> /// => Console.LogLine() /// </summary> /// <param name="c">Color</param> /// <param name="line">Line</param> protected static int Log(Color c, string line) { CLI.LogLine(c, line); return(0); }
/// <summary> /// => Console.LogLine() /// </summary> /// <param name="line">Line</param> protected static int Log(string line) { CLI.LogLine(Color.LightGray, line); return(0); }
/// <summary> /// Parses the specified command line arguments into the specified class. /// </summary> /// <typeparam name="T">Class where the command line arguments are stored.</typeparam> /// <param name="configuration">Class which stores the command line arguments.</param> /// <param name="args">Command line arguments.</param> /// <returns>Array of unresolved arguments.</returns> public static string[] ParseArguments<T>(T configuration, params string[] args) { const BindingFlags flags = BindingFlags.Public | BindingFlags.Instance; List<KeyValuePair<PropertyInfo, ArgumentAttribute>> properties = WithAttribute<PropertyInfo, ArgumentAttribute>(typeof (T).GetProperties(flags)).ToList(); var unresolvedArguments = new List<string>(); foreach (var arg in args) { // Parse the argument. Match match = ArgumentRegex.Match(arg); if (!match.Success) // This is not a typed argument. { unresolvedArguments.Add(arg); continue; } // Extract the argument details. bool isShortname = !arg.StartsWith("--"); string name = match.Groups[1].ToString(); string value = match.Groups[2].Length > 0 ? match.Groups[2].ToString().Substring(1) : null; // Find the argument. const StringComparison ignoreCase = StringComparison.InvariantCultureIgnoreCase; PropertyInfo property = (from kv in properties where name.Equals(isShortname ? kv.Value.ShortName : kv.Value.Name, ignoreCase) select kv.Key).SingleOrDefault(); // Check if this is a special argument we should handle. if (name == "help") { foreach (var line in GenerateCommandLineHelp(configuration)) { CLI.LogLine(Color.White, line); } } else if (property == null) { CLI.LogLine(Color.Red, "Unknown argument: " + (isShortname ? "-" : "--") + name); continue; } // Change the property. object convertedValue = null; if (value == null) { if (property == null || property.PropertyType == typeof (bool)) { convertedValue = true; } } else { Type type = property.PropertyType; if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof (Nullable<>)) type = type.GetGenericArguments().First(); convertedValue = Convert.ChangeType(value, type); } if (convertedValue == null) { CLI.LogLine(Color.Red, string.Format( "Argument '{0}' requires a value of the type '{1}'.", name, property.PropertyType.Name)); continue; } if (property != null) property.SetValue(configuration, convertedValue, null); } return unresolvedArguments.ToArray(); }