Ejemplo n.º 1
0
 public AttributeHandler(Configuration configuration, IParameterFormatter parameterFormatter, Func <IOption, string, object> valueConverter, Func <IOption, Type> memberTypeFromOption)
 {
     _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
     //TODO: instead of func, provide an interface for the conversion.
     _valueConverter       = valueConverter ?? throw new ArgumentNullException(nameof(valueConverter));
     _memberTypeFromOption = memberTypeFromOption ?? throw new ArgumentNullException(nameof(memberTypeFromOption));
     _parameterFormatter   = parameterFormatter ?? throw new ArgumentNullException(nameof(parameterFormatter));
 }
Ejemplo n.º 2
0
        public MethodEventFormatter(IParameterFormatter parameterFormatter)
        {
            if (parameterFormatter == null)
            {
                throw new ArgumentNullException("parameterFormatter");
            }

            this.parameterFormatter = parameterFormatter;
        }
Ejemplo n.º 3
0
        static void SetValue <T>(T obj, MemberInfo member, CommanderAttribute param, string name, string value)
        {
            if (param.Regex != null)
            {
                // We need to validate this value with a regex
                if (!value.Matches(param.Regex))
                {
                    throw new ParameterMatchException(param, value);
                }
            }

            if (param.ValidateWith != null)
            {
                // We need to validate this value with a validator
                if (!typeof(IParameterValidator).GetTypeInfo().IsAssignableFrom(param.ValidateWith))
                {
                    throw new ValidatorTypeException(param.ValidateWith);
                }

                IParameterValidator validator = (IParameterValidator)Activator.CreateInstance(param.ValidateWith);

                if (!validator.Validate(name, value))
                {
                    throw new ParameterValidationException(validator);
                }
            }

            object convertedValue;

            if (param.FormatWith != null)
            {
                // We need to format this value
                if (!typeof(IParameterFormatter).GetTypeInfo().IsAssignableFrom(param.FormatWith))
                {
                    throw new FormatterTypeException(param.FormatWith);
                }

                IParameterFormatter formatter = (IParameterFormatter)Activator.CreateInstance(param.FormatWith);

                convertedValue = formatter.Format(name, value);
            }
            else
            {
                // We need to try and convert the value ourselves
                try
                {
                    convertedValue = ValueParse(member.Type(), value);
                }
                catch (FormatException)
                {
                    // The value had to be parsed to a numerical type and failed
                    throw new ParameterFormatException(param, value, member.GetType());
                }
            }

            SetValue(obj, member, convertedValue);
        }
Ejemplo n.º 4
0
        public void RegisterParameterFormatter(Type type, IParameterFormatter formatter)
        {
            //TODO: formatters should be a cache, IParameterFormatter.Support() should be used

            if (formatters.ContainsKey(type))
            {
                formatters[type] = formatter;
            }
            else
            {
                formatters.Add(type, formatter);
            }
        }
Ejemplo n.º 5
0
 public AttributeParser(AttributeConfiguration configuration, IParameterFormatter parameterFormatter, IValueConverter valueConverter, IHelpPresenter helpPresenter)
 {
     Configuration     = configuration ?? throw new ArgumentNullException(nameof(configuration));
     _attributeHandler = new AttributeHandler(configuration, parameterFormatter,
                                              (option, value) =>
     {
         var parameterValueOption = configuration.GetParameterValueOption(option);
         return(valueConverter.ConvertFromString(option, parameterValueOption.ParameterInfo, AttributeHandler.GetValueType(parameterValueOption.Option, parameterValueOption.ParameterInfo.ParameterType), value));
     },
                                              (option) => configuration.GetParameterValueOption(option).ParameterInfo.ParameterType
                                              );
     _helpPresenter = helpPresenter ?? throw new ArgumentNullException(nameof(helpPresenter));
 }
Ejemplo n.º 6
0
 public RemoteProcess(IExecutionBroker executionBroker, IParameterFormatter patternFormatter)
 {
     _executionBroker  = executionBroker;
     _patternFormatter = patternFormatter;
 }
Ejemplo n.º 7
0
 public HelpPresenter(Configuration configuration, IParameterFormatter parameterFormatter)
 {
     Configuration       = configuration;
     _parameterFormatter = parameterFormatter;
 }