Text prompt

Sometimes you want to get some input from the user, and for this you can use the Prompt<TResult>.

The use of prompts insides status or progress displays is not supported.

Confirmation

if (!AnsiConsole.Confirm("Run example?"))
{
    return;
}
Run example? [y/n] (y): _

Simple

// Ask for the user's name
string name = AnsiConsole.Ask<string>("What's your [green]name[/]?");

// Ask for the user's age
int age = AnsiConsole.Ask<int>("What's your [green]age[/]?");
What's your name? Patrik
What's your age? 37

Choices

var fruit = AnsiConsole.Prompt(
    new TextPrompt<string>("What's your [green]favorite fruit[/]?")
        .InvalidChoiceMessage("[red]That's not a valid fruit[/]")
        .DefaultValue("Orange")
        .AddChoice("Apple")
        .AddChoice("Banana")
        .AddChoice("Orange"));
What's your favorite fruit? [Apple/Banana/Orange] (Orange): _

Validation

var age = AnsiConsole.Prompt(
    new TextPrompt<int>("What's the secret number?")
        .Validate(age =>
        {
            return age switch
            {
                < 99 => ValidationResult.Error("[red]Too low[/]"),
                > 99 => ValidationResult.Error("[red]Too high[/]"),
                _ => ValidationResult.Success(),
            };
        }));
What's the secret number? 32
Too low
What's the secret number? 102
Too high
What's the secret number? _

Secrets

var password = AnsiConsole.Prompt(
    new TextPrompt<string>("Enter [green]password[/]")
        .PromptStyle("red")
        .Secret());
Enter password: ************_

Optional

var color = AnsiConsole.Prompt(
    new TextPrompt<string>("[grey][[Optional]][/] [green]Favorite color[/]?")
        .AllowEmpty());
[Optional] Favorite color? _