Пример #1
0
        private static void SetPropertyValue <T>(CommandLineParsedArgs args, T instance, PropertyInfo prop, CommandOptionsValueAttribute attribute) where T : class, new()
        {
            var arg = args
                      .CommandOptionsValues
                      .FirstOrDefault(x =>
                                      x.Key == attribute.Name ||
                                      x.Key == attribute.Alias);

            if (arg.IsDefault() && attribute.Required)
            {
                throw new RequiredOptionNotFoundException(attribute.Name);
            }

            if (arg.IsDefault() == false)
            {
                prop.SetValue(instance, Convert.ChangeType(arg.Value, prop.PropertyType));
            }
            else if (attribute.DefaultValue != null)
            {
                prop.SetValue(instance, Convert.ChangeType(attribute.DefaultValue, prop.PropertyType));
            }
            else
            {
                prop.SetValue(instance, Convert.ChangeType(prop.PropertyType.GetDefaultValue(), prop.PropertyType));
            }
        }
Пример #2
0
 private static Type[] FindCommand(CommandLineParsedArgs args)
 {
     return(AssemblyFinder
            .GetCurrentAssemblyWithDependencies()
            .SelectMany(x => x.GetTypes()
                        .Where(z => z.GetTypeInfo().GetCustomAttribute <CommandAttribute>() != null &&
                               (z.GetTypeInfo().GetCustomAttribute <CommandAttribute>().Name == args.CommandName ||
                                z.GetTypeInfo().GetCustomAttribute <CommandAttribute>().Alias == args.CommandName
                               ))).ToArray());
 }
Пример #3
0
        public static T Create <T>(CommandLineParsedArgs args) where T : class, new()
        {
            Type[] commands = FindCommand(args);

            if (commands.Count() > 1)
            {
                throw new TooManyCommandsWithSameNameException(args.CommandName);
            }

            var commandType = commands.First();

            var instance = Activator.CreateInstance <T>();

            foreach (var prop in instance.GetType().GetProperties())
            {
                var attribute = prop.GetCustomAttribute <CommandOptionsValueAttribute>();
                if (attribute != null)
                {
                    SetPropertyValue(args, instance, prop, attribute);
                }
            }

            return(instance);
        }
        public void ExecuteFromParsedArgs(CommandLineParsedArgs args, TextWriter @out)
        {
            T command = CommandFactory.Create <T>(args);

            Execute(command, @out);
        }