Exemple #1
0
        public bool Start()
        {
            using (var scope = new ConsoleScope())
            {
                bool result;

                while (true)
                {
                    scope.Render(Template, new TemplateModel {
                        Message = _message, DefaultValue = _defaultValue
                    });

                    var input = scope.ReadLine();

                    if (string.IsNullOrEmpty(input))
                    {
                        if (_defaultValue != null)
                        {
                            result = _defaultValue.Value;
                            break;
                        }

                        scope.SetError(new ValidationError("Value is required"));
                    }
                    else
                    {
                        var lowerInput = input.ToLower();

                        if (lowerInput == "y" || lowerInput == "yes")
                        {
                            result = true;
                            break;
                        }

                        if (lowerInput == "n" || lowerInput == "no")
                        {
                            result = false;
                            break;
                        }

                        scope.SetError(new ValidationError("Value is invalid"));
                    }
                }

                scope.Render(FinishTemplate, new FinishTemplateModel {
                    Message = _message, Result = result
                });

                return(result);
            }
        }
Exemple #2
0
        public T Start()
        {
            using (var scope = new ConsoleScope())
            {
                T result;

                while (true)
                {
                    scope.Render(Template, new TemplateModel {
                        Message = _message, DefaultValue = _defaultValue
                    });

                    var input = scope.ReadLine();

                    if (!scope.Validate(input, _validators))
                    {
                        continue;
                    }

                    if (string.IsNullOrEmpty(input))
                    {
                        if (_targetType.IsValueType && _underlyingType == null && _defaultValue == null)
                        {
                            scope.SetError(new ValidationError("Value is required"));

                            continue;
                        }

                        result = (T)_defaultValue;

                        break;
                    }

                    try
                    {
                        result = (T)Convert.ChangeType(input, _underlyingType ?? _targetType);
                        break;
                    }
                    catch (Exception ex)
                    {
                        scope.SetException(ex);
                    }
                }

                scope.Render(FinishTemplate, new FinishTemplateModel {
                    Message = _message, Result = result
                });

                return(result);
            }
        }