/// <summary> /// Constructs a mapping between argument keys & other related data /// </summary> /// <typeparam name="T">Custom argument class</typeparam> /// <returns>Mapping between arguments & corresponding data</returns> internal static IDictionary <string, ArgumentData> GetArgumentData <T>() { ArgumentData helpData = new ArgumentData { Details = "Prints this help text", Key = "h", LongKey = "help", Optional = true, Type = ArgumentType.Flag, }; IDictionary <string, ArgumentData> mapping = new Dictionary <string, ArgumentData>(StringComparer.OrdinalIgnoreCase) { { "h", helpData }, { "help", helpData }, }; foreach (PropertyInfo propertyInfo in typeof(T).GetProperties()) { ParamAttribute paramAttr = propertyInfo.GetCustomAttribute <ParamAttribute>(); if (paramAttr == null) { continue; } ArgumentData data = ArgumentData.Parse(propertyInfo); mapping.SafeAdd(paramAttr.Key, data); mapping.SafeAdd(paramAttr.LongKey, data); } return(mapping); }
internal static ArgumentData Parse(PropertyInfo propertyInfo) { DetailsAttribute detailsAttr = propertyInfo.GetCustomAttribute <DetailsAttribute>(); OptionalAttribute optionalAttr = propertyInfo.GetCustomAttribute <OptionalAttribute>(); ParamAttribute paramAttr = propertyInfo.GetCustomAttribute <ParamAttribute>(); if (paramAttr == null) { return(null); } if (!IsTypeSupported(propertyInfo.PropertyType, paramAttr.Type)) { throw new InvalidOperationException( string.Format("Incompatible type specified for '{0}'", propertyInfo.Name)); } if (string.IsNullOrWhiteSpace(paramAttr.Key) && string.IsNullOrWhiteSpace(paramAttr.LongKey)) { throw new InvalidOperationException( string.Format("Both Key & LongKey cannot be null for property '{0}'.", propertyInfo.Name)); } string details = detailsAttr != null ? detailsAttr.Details : null; string defaultValue = optionalAttr != null ? optionalAttr.DefaultValue : null; ArgumentData data = new ArgumentData { DefaultValue = defaultValue, Details = details, Key = paramAttr.Key, LongKey = paramAttr.LongKey, Optional = optionalAttr != null, Property = propertyInfo, Type = paramAttr.Type, }; return(data); }