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)); }
public MethodEventFormatter(IParameterFormatter parameterFormatter) { if (parameterFormatter == null) { throw new ArgumentNullException("parameterFormatter"); } this.parameterFormatter = parameterFormatter; }
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); }
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); } }
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)); }
public RemoteProcess(IExecutionBroker executionBroker, IParameterFormatter patternFormatter) { _executionBroker = executionBroker; _patternFormatter = patternFormatter; }
public HelpPresenter(Configuration configuration, IParameterFormatter parameterFormatter) { Configuration = configuration; _parameterFormatter = parameterFormatter; }