Example #1
0
        public static void Bind(CommandTree tree, ref CommandSettings settings, ITypeResolver resolver)
        {
            ValidateRequiredParameters(tree);

            TypeConverter GetConverter(CommandParameter parameter)
            {
                if (parameter.Converter == null)
                {
                    if (parameter.ParameterType.IsArray)
                    {
                        // Return a converter for each array item (not the whole array)
                        return(TypeDescriptor.GetConverter(parameter.ParameterType.GetElementType()));
                    }

                    return(TypeDescriptor.GetConverter(parameter.ParameterType));
                }
                var type = Type.GetType(parameter.Converter.ConverterTypeName);

                return(resolver.Resolve(type) as TypeConverter);
            }

            while (tree != null)
            {
                // Process mapped parameters.
                foreach (var mapped in tree.Mapped)
                {
                    var converter = GetConverter(mapped.Parameter);
                    mapped.Parameter.Assign(settings, converter.ConvertFromInvariantString(mapped.Value));
                    ValidateParameter(mapped.Parameter, settings);
                }

                // Process unmapped parameters.
                foreach (var parameter in tree.Unmapped)
                {
                    // Is this an option with a default value?
                    if (parameter is CommandOption option && option.DefaultValue != null)
                    {
                        parameter.Assign(settings, option.DefaultValue.Value);
                        ValidateParameter(parameter, settings);
                    }
                }

                tree = tree.Next;
            }

            // Validate the settings.
            var validationResult = settings.Validate();

            if (!validationResult.Successful)
            {
                throw RuntimeException.ValidationFailed(validationResult);
            }
        }
        public static CommandSettings CreateSettings(CommandValueLookup lookup, Type settingsType, ITypeResolver resolver)
        {
            var settings = CreateSettings(resolver, settingsType);

            foreach (var(parameter, value) in lookup)
            {
                parameter.Property.SetValue(settings, value);
            }

            // Validate the settings.
            var validationResult = settings.Validate();

            if (!validationResult.Successful)
            {
                throw RuntimeException.ValidationFailed(validationResult);
            }

            return(settings);
        }
Example #3
0
        private static void ValidateParameter(CommandParameter parameter, CommandSettings settings)
        {
            var assignedValue = parameter.Get(settings);

            foreach (var validator in parameter.Validators)
            {
                var validationResult = validator.Validate(assignedValue);
                if (!validationResult.Successful)
                {
                    // If there is a error message specified in the parameter validator attribute,
                    // then use that one, otherwise use the validation result.
                    var result = validator.ErrorMessage != null
                        ? ValidationResult.Error(validator.ErrorMessage)
                        : validationResult;

                    throw RuntimeException.ValidationFailed(result);
                }
            }
        }
Example #4
0
        private static Task <int> Execute(
            CommandTree leaf,
            CommandTree tree,
            CommandContext context,
            ITypeResolver resolver,
            IConfiguration configuration)
        {
            // Bind the command tree against the settings.
            var settings = CommandBinder.Bind(tree, leaf.Command.SettingsType, resolver);

            configuration.Settings.Interceptor?.Intercept(context, settings);

            // Create and validate the command.
            var command          = leaf.CreateCommand(resolver);
            var validationResult = command.Validate(context, settings);

            if (!validationResult.Successful)
            {
                throw RuntimeException.ValidationFailed(validationResult);
            }

            // Execute the command.
            return(command.Execute(context, settings));
        }