/// <summary>
 /// Hides choices.
 /// </summary>
 /// <typeparam name="T">The prompt result type.</typeparam>
 /// <param name="obj">The prompt.</param>
 /// <returns>The same instance so that multiple calls can be chained.</returns>
 public static TextPrompt <T> HideChoices <T>(this TextPrompt <T> obj)
 {
     return(ShowChoices(obj, false));
 }
 /// <summary>
 /// Shows choices.
 /// </summary>
 /// <typeparam name="T">The prompt result type.</typeparam>
 /// <param name="obj">The prompt.</param>
 /// <returns>The same instance so that multiple calls can be chained.</returns>
 public static TextPrompt <T> ShowChoices <T>(this TextPrompt <T> obj)
 {
     return(ShowChoices(obj, true));
 }
 /// <summary>
 /// Shows the default value.
 /// </summary>
 /// <typeparam name="T">The prompt result type.</typeparam>
 /// <param name="obj">The prompt.</param>
 /// <returns>The same instance so that multiple calls can be chained.</returns>
 public static TextPrompt <T> ShowDefaultValue <T>(this TextPrompt <T> obj)
 {
     return(ShowDefaultValue(obj, true));
 }
 /// <summary>
 /// Hides the default value.
 /// </summary>
 /// <typeparam name="T">The prompt result type.</typeparam>
 /// <param name="obj">The prompt.</param>
 /// <returns>The same instance so that multiple calls can be chained.</returns>
 public static TextPrompt <T> HideDefaultValue <T>(this TextPrompt <T> obj)
 {
     return(ShowDefaultValue(obj, false));
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Shows the prompt and requests input from the user.
        /// </summary>
        /// <param name="console">The console to show the prompt in.</param>
        /// <returns>The user input converted to the expected type.</returns>
        /// <inheritdoc/>
        public T Show(IAnsiConsole console)
        {
            if (console is null)
            {
                throw new ArgumentNullException(nameof(console));
            }

            var promptStyle = PromptStyle ?? Style.Plain;

            WritePrompt(console);

            while (true)
            {
                var input = console.ReadLine(promptStyle, IsSecret);

                // Nothing entered?
                if (string.IsNullOrWhiteSpace(input))
                {
                    if (DefaultValue != null)
                    {
                        console.Write(TextPrompt <T> .GetTypeConverter().ConvertToInvariantString(DefaultValue.Value), promptStyle);
                        console.WriteLine();
                        return(DefaultValue.Value);
                    }

                    if (!AllowEmpty)
                    {
                        continue;
                    }
                }

                console.WriteLine();

                // Try convert the value to the expected type.
                if (!TextPrompt <T> .TryConvert(input, out var result) || result == null)
                {
                    console.MarkupLine(ValidationErrorMessage);
                    WritePrompt(console);
                    continue;
                }

                if (Choices.Count > 0)
                {
                    if (Choices.Contains(result))
                    {
                        return(result);
                    }
                    else
                    {
                        console.MarkupLine(InvalidChoiceMessage);
                        WritePrompt(console);
                        continue;
                    }
                }

                // Run all validators
                if (!ValidateResult(result, out var validationMessage))
                {
                    console.MarkupLine(validationMessage);
                    WritePrompt(console);
                    continue;
                }

                return(result);
            }
        }