public bool Parse(string[] args) { PropertyInfo[] Infos = typeof(Arguments).GetProperties(); foreach (var Info in Infos) { object[] Attributes = Info.GetCustomAttributes(false); string Name = null; string DefaultValue = null; foreach (var Att in Attributes) { if (Att.GetType() == typeof(ArgumentName)) { Name = (Att as ArgumentName).Name; } if (Att.GetType() == typeof(DefaultArgument)) { DefaultValue = (Att as DefaultArgument).Value; } } bool found = false; foreach (var arg in args) { if (arg.StartsWith(Name)) { string val = arg.Replace(Name, ""); Info.SetValue(this, val); found = true; break; } } if (!found && DefaultValue != null) { Info.SetValue(this, DefaultValue); } else if (!found && DefaultValue == null) { return(false); } } return(true); }
public void ShowParsedValues() { PropertyInfo[] Infos = typeof(Arguments).GetProperties(); foreach (var Info in Infos) { object[] Attributes = Info.GetCustomAttributes(false); string Name = null; foreach (var Att in Attributes) { if (Att.GetType() == typeof(ArgumentName)) { Name = (Att as ArgumentName).Name; } } if (Info.GetValue(this) != null) { System.Console.WriteLine("Name: " + Name + " " + Info.GetValue(this).ToString()); } } }
public void ShowAllOptions() { PropertyInfo[] Infos = typeof(Arguments).GetProperties(); foreach (var Info in Infos) { object[] Attributes = Info.GetCustomAttributes(false); string Name = null; string DefaultValue = null; foreach (var Att in Attributes) { if (Att.GetType() == typeof(ArgumentName)) { Name = (Att as ArgumentName).Name; } if (Att.GetType() == typeof(DefaultArgument)) { DefaultValue = (Att as DefaultArgument).Value; } } System.Console.WriteLine("Option: {0}, Default Value {1}", Name, DefaultValue == null ? " None, this option is required " : DefaultValue); } }