public static Options CreateFromType(Type type) { if (type == null) { throw new ArgumentNullException("type"); } //if (!Attribute.IsDefined(type, typeof(OptionsAttribute))) // throw new ArgumentException("The type '" + type + "' is not marked as options"); MemberInfo[] members = type.FindMembers(MemberTypes.Field | MemberTypes.Property, BindingFlags.Public | BindingFlags.Instance, FilterMember, null); var groups = new Dictionary <string, OptionGroup>(); var requiredGroups = new List <string>(); Attribute[] groupsAttrs = Attribute.GetCustomAttributes(type, typeof(OptionGroupAttribute)); foreach (OptionGroupAttribute groupAttr in groupsAttrs) { OptionGroup group; if (!groups.TryGetValue(groupAttr.Name, out @group)) { @group = new OptionGroup { IsRequired = groupAttr.IsRequired }; groups[groupAttr.Name] = @group; if (groupAttr.IsRequired) { requiredGroups.Add(groupAttr.Name); } } } var options = new Options(); foreach (MemberInfo member in members) { Option option = CreateOptionFromMember(member, groups); if (option != null) { options.AddOption(option); } } foreach (var entry in groups) { var group = entry.Value; options.AddOptionGroup(group); } return(options); }
public static Options CreateFromType(Type type) { if (type == null) throw new ArgumentNullException("type"); //if (!Attribute.IsDefined(type, typeof(OptionsAttribute))) // throw new ArgumentException("The type '" + type + "' is not marked as options"); MemberInfo[] members = type.FindMembers(MemberTypes.Field | MemberTypes.Property, BindingFlags.Public | BindingFlags.Instance, FilterMember, null); var groups = new Dictionary<string, OptionGroup>(); var requiredGroups = new List<string>(); Attribute[] groupsAttrs = Attribute.GetCustomAttributes(type, typeof(OptionGroupAttribute)); foreach (OptionGroupAttribute groupAttr in groupsAttrs) { OptionGroup group; if (!groups.TryGetValue(groupAttr.Name, out @group)) { @group = new OptionGroup {IsRequired = groupAttr.IsRequired}; groups[groupAttr.Name] = @group; if (groupAttr.IsRequired) requiredGroups.Add(groupAttr.Name); } } var options = new Options(); foreach (MemberInfo member in members) { Option option = CreateOptionFromMember(member, groups); if (option != null) options.AddOption(option); } foreach(var entry in groups) { var group = entry.Value; options.AddOptionGroup(group); } return options; }
public void PrintOptionGroupUsage() { OptionGroup group = new OptionGroup(); group.AddOption(OptionBuilder.New().Create("a")); group.AddOption(OptionBuilder.New().Create("b")); group.AddOption(OptionBuilder.New().Create("c")); Options options = new Options(); options.AddOptionGroup(group); StringWriter output = new StringWriter(); HelpFormatter formatter = new HelpFormatter(); formatter.PrintUsage(options, new HelpSettings { Width = 80, CommandLineSyntax = "app" }, output); Assert.AreEqual("usage: app [-a | -b | -c]" + Eol, output.ToString()); }