private static void SetValue(ArgumentMap map, NameValueArgument argument, IArgumentDefinition arguments) { var property = map.Property; var type = property.PropertyType; if (property.Name == "Help") { property.SetValue(arguments, true, null); } else { property.SetValue(arguments, ArgumentValue.Parse(type, argument.Value.ToString(CultureInfo.CurrentCulture)), null); } }
public void ParseArguments(string[] args) { var map = this.mapManager.GetArgumentMap(); var argumentDefinitionClassName = this.mapManager.ArgumentDefinition.GetType().Name; if (map.Count == 0) { throw new InvalidArgumentException(string.Format( CultureInfo.CurrentCulture, "No arguments were defined in {0}", argumentDefinitionClassName)); } var requiredList = map.Where(m => m.Attributes.Required) .Select(s => s.Attributes.LongName).ToList(); foreach (var arg in args) { var argument = NameValueArgument.Parse(arg); var mapItem = map.FirstOrDefault(a => 0 == string.Compare(a.Attributes.LongName, argument.Name, true, CultureInfo.CurrentCulture)) ?? map.FirstOrDefault(a => 0 == string.Compare(a.Attributes.ShortName, argument.Name, true, CultureInfo.CurrentCulture)); if (mapItem == null) { throw new InvalidArgumentException(string.Format( CultureInfo.CurrentCulture, "Argument '{0}' not defined in definition class {1}", argument.Name, argumentDefinitionClassName)); } requiredList.Remove(mapItem.Attributes.LongName); SetValue(mapItem, argument, this.mapManager.ArgumentDefinition); } if (requiredList.Count > 0) { throw new InvalidArgumentException(string.Format( CultureInfo.CurrentCulture, "Required arguments not specified: {0}", string.Join(", ", requiredList.ToArray()))); } }
public static NameValueArgument Parse(string value) { string argument; if (value.StartsWith("/", true, CultureInfo.CurrentCulture) || value.StartsWith("-", true, CultureInfo.CurrentCulture)) { argument = value.Substring(1, value.Length - 1); } else { throw new InvalidArgumentException(value); } var result = new NameValueArgument(); var vals = argument.Split(':'); for (var i = 0; i < vals.Length; i++) { if (i == 0) { result.Name = vals[i]; } else { result.Value += vals[i]; if (i < vals.Length - 1) { result.Value += ":"; } } } if (!string.IsNullOrWhiteSpace(result.Value)) { result.Value = result.Value.Trim('"'); } return(result); }