Пример #1
0
        private void ProcessMultiQuestion(MultiQuestion question, DefaultConfigModel model)
        {
            WriteContent(question.Content);
            WriteDefaultAnswer(question.DefaultAnswer);
            WriteDescription(question.Descriptions);
            var multiAnswer = question as MultiAnswerQuestion;

            if (multiAnswer != null)
            {
                var           line    = Console.ReadLine();
                List <string> answers = new List <string>();
                while (!string.IsNullOrEmpty(line))
                {
                    answers.Add(line);
                    line = Console.ReadLine();
                }

                if (answers.Count > 0)
                {
                    question.Setter(answers.ToArray(), model);
                }
                else
                {
                    question.SetDefault(model);
                }
            }
        }
Пример #2
0
        private void ProcessSingleQuestion(SingleQuestion question, DefaultConfigModel model)
        {
            WriteContent(question.Content);
            WriteDefaultAnswer(question.DefaultAnswer);
            WriteDescription(question.Descriptions);
            var singleChoice = question as SingleChoiceQuestion;

            if (singleChoice != null)
            {
                var options = singleChoice.Options;
                Console.Write("Choose Answer ({0}): ", string.Join("/", options));
                Console.Write(singleChoice.DefaultAnswer[0]);
                Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
                string matched;
                do
                {
                    var input = Console.ReadLine();
                    if (string.IsNullOrEmpty(input))
                    {
                        question.SetDefault(model);
                        break;
                    }
                    else
                    {
                        matched = GetMatchedOption(options, input);
                        if (matched == null)
                        {
                            Console.Write("Invalid Answer, please reenter: ");
                        }
                        else
                        {
                            question.Setter(matched, model);
                        }
                    }
                }while (matched == null);
            }
            else
            {
                var line = Console.ReadLine();
                if (!string.IsNullOrEmpty(line))
                {
                    question.Setter(line, model);
                }
                else
                {
                    question.SetDefault(model);
                }
            }
        }
Пример #3
0
        public ParseResult Exec(RunningContext context)
        {
            string name = null;
            string path = null;

            try
            {
                DefaultConfigModel config = new DefaultConfigModel();
                if (_options.Quiet)
                {
                    // Generate a default Config
                    foreach (var question in _questions)
                    {
                        question.SetDefault(config);
                    }
                }
                else
                {
                    foreach (var question in _questions)
                    {
                        if (question is SingleQuestion)
                        {
                            ProcessSingleQuestion((SingleQuestion)question, config);
                        }
                        else
                        {
                            ProcessMultiQuestion((MultiQuestion)question, config);
                        }
                    }
                }

                name = string.IsNullOrEmpty(_options.Name) ? ConfigName : _options.Name;
                path = string.IsNullOrEmpty(_options.OutputFolder) ? name : Path.Combine(_options.OutputFolder, name);

                JsonUtility.Serialize(path, config, Formatting.Indented);
                return(new ParseResult(ResultLevel.Success, $"Generated {name} to {path}"));
            }
            catch (Exception e)
            {
                return(new ParseResult(ResultLevel.Error, $"Error init { name ?? ConfigName}: {e.Message}"));
            }
        }
Пример #4
0
 public void SetDefault(DefaultConfigModel model)
 {
     Setter(DefaultAnswer, model);
 }