Example #1
0
        public ConOut AskYN(string question, out bool value)
        {
            var _value = false;
            var ask    = new ConAsk(this, question);

            ask.Resolve(answer =>
            {
                if (new[] { "y", "yes", "Y", "YES", "Yes" }.Contains(answer.Value))
                {
                    answer.Action = AskAction.Accept;
                    answer.Value  = "Yes";
                    _value        = true;
                }
                else if (new[] { "n", "no", "N", "NO", "No" }.Contains(answer.Value))
                {
                    answer.Action = AskAction.Accept;
                    answer.Value  = "No";
                    _value        = false;
                }
                else
                {
                    answer.Action = AskAction.Retry;
                }
            });
            value = _value;
            return(this);
        }
Example #2
0
        protected ConOut AskResolve <T>(string question, out T value, Action <AskAnswer> followResolve, bool whitespaceRetry, T defaultValue)
        {
            T   _value = default;
            var ask    = new ConAsk(this, question);

            ask.Resolve(answer =>
            {
                if (answer.Value.IsWhiteSpace())
                {
                    if (whitespaceRetry)
                    {
                        answer.Action = AskAction.Retry; return;
                    }
                    else
                    {
                        answer.Action = AskAction.Accept;
                        answer.Value  = defaultValue?.ToString();
                        _value        = defaultValue; return;
                    }
                }

                try
                {
                    followResolve(answer);
                    if (answer.Action == AskAction.Accept)
                    {
                        _value = (T)Convert.ChangeType(answer.Value, typeof(T));
                    }
                }
                catch { answer.Action = AskAction.Retry; }
            });
            value = _value;
            return(this);
        }