protected virtual object GetSubOptions(OptionInfo option, HelpParts parts) { return(new Grid { Stroke = LineThickness.None, Columns = { new Column { Width = GridLength.Auto, MinWidth = OptionNameMinLength + SubOptionMargin.Left + 1, MaxWidth = OptionNameMaxLength + SubOptionMargin.Left + 1, }, new Column { Width = GridLength.Star(1) }, }, Children = { option.SubOptions.Where(IsOptionVisible(parts)).Select(o => GetOption(o, parts, isSubOption: true)) } }); }
public static IEnumerable <OptionInfo> GetOptionsFromOptionsRoots(List <Type> optionsRoots) { var roots = GetTypeInfos(optionsRoots); #if CLP_22 if (roots.Count > 1 || roots.SelectMany(GetAttributes).Any(a => a.GetType().FullName == VerbAttributeTypeName22)) { return(roots.Select(CreateOptionVerb).Where(o => o != null)); } #endif if (roots.Count == 1) { var props = roots.Single().GetProperties(); #if CLP_19 if (props.SelectMany(GetAttributes).Any(a => a.GetType().FullName == VerbOptionAttributeTypeName19)) { return(props .Where(p => p.GetCustomAttributes <Attribute>().Any(a => a.GetType().FullName == VerbOptionAttributeTypeName19)) .Select(CreateOptionVerb).Where(o => o != null)); } #endif #if CLP_19 || CLP_22 return(props.Select(CreateOption).Where(o => o != null)); #endif } throw UnsupportedVersion(); OptionInfo CreateOptionVerb(MemberInfo root) => OptionInfo.FromMember(root, GetAttributes(root), isVerb: true) .WithSubOptions(GetVerbOptions(root).Select(CreateOption).Where(o => o != null)); OptionInfo CreateOption(PropertyInfo prop) => OptionInfo.FromMember(prop, GetAttributes(prop)); #if NET_STANDARD_15 IEnumerable <PropertyInfo> GetVerbOptions(MemberInfo source) => (source as TypeInfo ?? ((PropertyInfo)source).PropertyType.GetTypeInfo()).GetProperties(BindingFlags.Public | BindingFlags.Instance); #else IEnumerable <PropertyInfo> GetVerbOptions(MemberInfo source) => (source as Type ?? ((PropertyInfo)source).PropertyType).GetProperties(BindingFlags.Public | BindingFlags.Instance); #endif }
protected virtual object GetOption(OptionInfo option, HelpParts parts, bool isSubOption) { var m = isSubOption ? SubOptionMargin : OptionMargin; var marginName = new Thickness(m.Left, m.Top, 1, m.Bottom); var marginText = new Thickness(0, m.Top, m.Right, m.Bottom); string values = parts.Has(HelpParts.OptionsValues) && option.HasHelpValues ? string.Join(", ", option.HelpValues) : null; string defaultValue = parts.Has(HelpParts.OptionsDefaultValue) && option.DefaultValue != null ? GetOptionDefault(option) : null; object subOptions = parts.Has(HelpParts.SubOptions) && option.SubOptions.Any(IsOptionVisible(parts)) ? GetSubOptions(option, parts) : null; return(new[] { new Div { Margin = marginName, Color = isSubOption ? SubOptionNameColor : OptionNameColor, TextWrap = TextWrap.WordWrapSpace, Children = { option.HelpNameWithValue } }, new Div { Margin = marginText, Color = OptionTextColor, Children = { option.HelpText, option.IsRequired ? " (Required)" : null, values != null ? $"\nValid values: {values}" : null, defaultValue != null ? $"\nDefault: {defaultValue}" : null } }, subOptions != null ? new Div { [Grid.ColumnSpanProperty] = 2, Children = { subOptions } } : null }); }
protected virtual string GetOptionDefault(OptionInfo option) { switch (option.DefaultValue) { case null: return(null); case bool b: return(ToString(b).ToLower()); case string s: return(s); case IEnumerable e: return(string.Join(" ", e)); default: return(null); } string ToString(object value) => Convert.ToString(value, EffectiveCulture); }