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>().
csharp
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().
csharp
if (AnsiConsole.Confirm("Continue with installation?"))
{
AnsiConsole.MarkupLine("[green]Installing...[/]");
}
Present Choices
To let users pick from options, use SelectionPrompt.
csharp
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.
csharp
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
- TextPrompt - Full text prompt API
- SelectionPrompt - Selection prompt options
- MultiSelectionPrompt - Multi-selection options