public static OptionsDefinition GetDefinition(Type type)
        {
            var arguments = new OptionsDefinition(type);

            // Ensure that the type has a default constructor.
            if (type.GetConstructor(Array.Empty <Type>()) == null)
            {
                throw new Exception("Parse types must have a default constructor.");
            }

            // Get commands
            foreach (var field in type.GetFields(BindingFlags.Public | BindingFlags.Instance))
            {
                if (TryGetCommand(field, out var command))
                {
                    var options = GetDefinition(field.FieldType);
                    arguments.Commands.Add(new CommandDefinition(command.Name, options, field));
                }

                if (TryGetParameter(field, out var parameter))
                {
                    arguments.Parameters.Add(new Tuple <ParameterAttribute, MemberInfo>(parameter, field));
                }
            }


            foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
            {
                if (TryGetCommand(property, out var command))
                {
                    var options = GetDefinition(property.PropertyType);
                    arguments.Commands.Add(new CommandDefinition(command.Name, options, property));
                }

                if (TryGetParameter(property, out var parameter))
                {
                    arguments.Parameters.Add(new Tuple <ParameterAttribute, MemberInfo>(parameter, property));
                }
            }

            return(arguments);
        }
Ejemplo n.º 2
0
 public CommandDefinition(string name, OptionsDefinition options, MemberInfo member)
 {
     Name    = name;
     Options = options;
     Member  = member;
 }
Ejemplo n.º 3
0
 public ArgumentParser()
 {
     Definition = OptionsDefinition.GetDefinition <T>();
 }