Prompt for User Input

Collect input from users with text prompts, confirmations, and selection menus

When your application needs user input, use the interactive prompts.

Caution

Prompts are not thread safe. Using them together with other interactive components such as progress displays, status displays, or other prompts is not supported.

Ask for Text

To get text input, use AnsiConsole.Ask<T>().

var name = AnsiConsole.Ask<string>("What's your [green]name[/]?");
AnsiConsole.MarkupLine($"Hello, [blue]{name}[/]!");

Ask for Confirmation

To get a yes/no answer, use AnsiConsole.Confirm().

if (AnsiConsole.Confirm("Continue with installation?"))
{
    AnsiConsole.MarkupLine("[green]Installing...[/]");
}

Present Choices

To let users pick from options, use SelectionPrompt.

var choice = AnsiConsole.Prompt(
    new SelectionPrompt<string>()
        .Title("Select an [green]environment[/]:")
        .AddChoices("Development", "Staging", "Production"));
  
AnsiConsole.MarkupLine($"Deploying to [blue]{choice}[/]");

Allow Multiple Selections

To let users select multiple items, use MultiSelectionPrompt.

var features = AnsiConsole.Prompt(
    new MultiSelectionPrompt<string>()
        .Title("Select [green]features[/] to enable:")
        .AddChoices("Logging", "Caching", "Authentication", "Analytics"));
  
AnsiConsole.MarkupLine($"Enabled: [blue]{string.Join(", ", features)}[/]");

See Also