public CommandLineOptionDescriptor(PropertyInfo pinfo)
: this()
{
var nameAttribute = pinfo.GetCustomAttribute<NameAttribute>();
if(nameAttribute != null)
{
ShortName = nameAttribute.ShortName;
LongName = nameAttribute.LongName;
}
else
{
ShortName = char.ToLower(pinfo.Name.ElementAt(0));
LongName = ShortName + pinfo.Name.Substring(1);
}
OptionType = pinfo.PropertyType;
UnderlyingProperty = pinfo;
IsRequired = (pinfo.GetCustomAttribute<RequiredAttribute>() != null);
AcceptsArgument = (pinfo.PropertyType != typeof(bool));
AllowMultipleOccurences = pinfo.PropertyType.IsArray;
var defaultValueAttribute = pinfo.GetCustomAttribute<DefaultValueAttribute>();
if(defaultValueAttribute != null)
{
if(OptionType != defaultValueAttribute.DefaultValue.GetType())
{
throw new ArgumentException(string.Format("Default value for option '{0}' is of unexpected type.", LongName ?? ShortName.ToString()));
}
DefaultValue = defaultValueAttribute.DefaultValue;
}
var descriptionAttribute = pinfo.GetCustomAttribute<DescriptionAttribute>();
if(descriptionAttribute != null)
{
Description = descriptionAttribute.Value;
}
if(OptionType.IsArray)
{
var numberOfElementsAttribute = pinfo.GetCustomAttribute<NumberOfElementsAttribute>();
if(numberOfElementsAttribute != null)
{
MaxElements = numberOfElementsAttribute.Max;
}
var delimiterAttribute = pinfo.GetCustomAttribute<DelimiterAttribute>();
if(delimiterAttribute != null)
{
Delimiter = delimiterAttribute.Delimiter;
}
}
}