private OptionDefinition BuildOptDef(OptDefAttribute defAttr, MemberInfo info) { ArrayList nameDefinitions; Attribute[] attrs; CategoryAttribute catAttr; DefaultValueAttribute defValAttr; DescriptionAttribute descAttr; OptionDefinition def; UseNameAsLongOptionAttribute useNameAttr; Type type; bool useName = false; char[] shortNames = null; string category; string description; string[] longNames = null; // Get the description descAttr = (DescriptionAttribute)GetAttribute(typeof(DescriptionAttribute), info); description = (descAttr == null) ? info.Name : descAttr.Description; // Get the category catAttr = (CategoryAttribute)GetAttribute(typeof(CategoryAttribute), info); category = (catAttr == null) ? null : catAttr.Category; // use the name as a value? useNameAttr = (UseNameAsLongOptionAttribute)GetAttribute( typeof(UseNameAsLongOptionAttribute), info); useName = (useNameAttr == null) ? true : useNameAttr.UseName; nameDefinitions = new ArrayList(); attrs = (Attribute[])info.GetCustomAttributes(typeof(ShortOptionNameAttribute), true); if (attrs != null) { int counter = 0; shortNames = new char[attrs.Length]; foreach (ShortOptionNameAttribute attr in attrs) { shortNames[counter] = attr.Name; counter++; } } attrs = (Attribute[])info.GetCustomAttributes(typeof(LongOptionNameAttribute), true); if (attrs != null) { int count = (useName) ? attrs.Length + 1 : attrs.Length; int counter = 0; longNames = new string[count]; foreach (LongOptionNameAttribute attr in attrs) { longNames[counter] = attr.Name; counter++; } if (useName) longNames[longNames.Length - 1] = info.Name; } if (defAttr.ValueType == null) { if (defAttr.OptionValueType == OptValType.MultValue) throw new ParseException("Multiple value options must have the " + "ValueType property set in the OptDefAttribute declaration"); defAttr.ValueType = (info is FieldInfo) ? ((FieldInfo)info).FieldType : ((PropertyInfo)info).PropertyType; } if (defAttr.ValueType == null) { if (info is PropertyInfo) type = ((PropertyInfo)info).PropertyType; else type = ((FieldInfo)info).FieldType; } else type = defAttr.ValueType; def = new OptionDefinition(info.Name, defAttr.OptionValueType, type, category, description, shortNames, longNames); // Get the default value defValAttr = (DefaultValueAttribute)GetAttribute(typeof(DefaultValueAttribute), info); if (defValAttr != null) def.DefaultValue = defValAttr.Value; return def; }
private OptionDefinition BuildOptDef(OptDefAttribute defAttr, MemberInfo info) { ArrayList nameDefinitions; Attribute[] attrs; CategoryAttribute catAttr; DefaultValueAttribute defValAttr; DescriptionAttribute descAttr; OptionDefinition def; UseNameAsLongOptionAttribute useNameAttr; Type type; bool useName = false; char[] shortNames = null; string category; string description; string[] longNames = null; // Get the description descAttr = (DescriptionAttribute)GetAttribute(typeof(DescriptionAttribute), info); description = (descAttr == null) ? info.Name : descAttr.Description; // Get the category catAttr = (CategoryAttribute)GetAttribute(typeof(CategoryAttribute), info); category = (catAttr == null) ? null : catAttr.Category; // use the name as a value? useNameAttr = (UseNameAsLongOptionAttribute)GetAttribute( typeof(UseNameAsLongOptionAttribute), info); useName = (useNameAttr == null) ? true : useNameAttr.UseName; nameDefinitions = new ArrayList(); attrs = (Attribute[])info.GetCustomAttributes(typeof(ShortOptionNameAttribute), true); if (attrs != null) { int counter = 0; shortNames = new char[attrs.Length]; foreach (ShortOptionNameAttribute attr in attrs) { shortNames[counter] = attr.Name; counter++; } } attrs = (Attribute[])info.GetCustomAttributes(typeof(LongOptionNameAttribute), true); if (attrs != null) { int count = (useName) ? attrs.Length + 1 : attrs.Length; int counter = 0; longNames = new string[count]; foreach (LongOptionNameAttribute attr in attrs) { longNames[counter] = attr.Name; counter++; } if (useName) { longNames[longNames.Length - 1] = info.Name; } } if (defAttr.ValueType == null) { if (defAttr.OptionValueType == OptValType.MultValue) { throw new ParseException("Multiple value options must have the " + "ValueType property set in the OptDefAttribute declaration"); } defAttr.ValueType = (info is FieldInfo) ? ((FieldInfo)info).FieldType : ((PropertyInfo)info).PropertyType; } if (defAttr.ValueType == null) { if (info is PropertyInfo) { type = ((PropertyInfo)info).PropertyType; } else { type = ((FieldInfo)info).FieldType; } } else { type = defAttr.ValueType; } def = new OptionDefinition(info.Name, defAttr.OptionValueType, type, category, description, shortNames, longNames); // Get the default value defValAttr = (DefaultValueAttribute)GetAttribute(typeof(DefaultValueAttribute), info); if (defValAttr != null) { def.DefaultValue = defValAttr.Value; } return(def); }