示例#1
0
        /// <summary>
        /// Prompts the user to input a binary response (true/false, yes/no, etc..).
        /// </summary>
        /// <param name="prompt">The prompt to provide to the user for input.</param>
        /// <param name="options">Optional parameters to modify binary question.</param>
        public static bool BinaryQuestion(string prompt, BinaryQuestionOptions options = null)
        {
            var binaryOptions  = options ?? new BinaryQuestionOptions();
            var answer         = string.Empty;
            var isPositive     = false;
            var isNegative     = false;
            var comparisonType = binaryOptions.IgnoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;

            do
            {
                answer     = Prompt(binaryOptions.Formatter(prompt));
                isPositive = string.Equals(answer, binaryOptions.PositiveResponse, comparisonType);
                isNegative = string.Equals(answer, binaryOptions.NegativeResponse, comparisonType);
            }while (binaryOptions.RepeatOnInvalid && !isPositive && !isNegative);

            return(isPositive);
        }
示例#2
0
        /// <summary>
        /// Repeats an action until it returns true or the user declines to continue.
        /// </summary>
        /// <param name="action">The action to repeat.</param>
        /// <param name="options">Optional parameters to modify repeat.</param>
        public static async Task <bool> RepeatUntilSuccessAsync(Func <Task <bool> > action, RepeatUntilSuccessOptions options = null)
        {
            var success = false;

            var repeatOptions = options ?? new RepeatUntilSuccessOptions();
            var binaryOptions = new BinaryQuestionOptions();

            binaryOptions.PositiveResponse = repeatOptions.PositiveResponse;
            binaryOptions.NegativeResponse = repeatOptions.NegativeResponse;
            binaryOptions.IgnoreCase       = repeatOptions.IgnoreCase;
            binaryOptions.RepeatOnInvalid  = true;

            do
            {
                success = await action();
            }while (!success && BinaryQuestion(repeatOptions.ContinuePrompt, binaryOptions));

            return(success);
        }