In this tutorial, we'll build a pizza ordering system that collects user input. By the end, you'll know how to ask for text, let users choose from lists, and confirm their selections.
What We're Building
Here's what our pizza order flow will look like:
Prerequisites
- .NET 6.0 or later
- Basic C# knowledge
- Completion of the Getting Started tutorial
- 1
Ask for the Customer's Name
Let's start by asking for the customer's name. The
Ask<string>()method displays a prompt and waits for input:var name = AnsiConsole.Ask<string>("What's your [green]name[/]?"); AnsiConsole.MarkupLine($"Welcome, [blue]{name}[/]!");Run the code:
dotnet runYou should see "What's your name?" with a cursor waiting for input. Type your name and press Enter. The program then greets you by name.
Notice how we used
[green]markup in the prompt? You can style your prompts just like any other Spectre.Console output.You've captured your first user input.
- 2
Choose a Pizza Size
Now let's let the user pick from a list of options. A
SelectionPromptshows an interactive menu where users navigate with arrow keys:var size = AnsiConsole.Prompt( new SelectionPrompt<string>() .Title("What [green]size pizza[/] would you like?") .AddChoices("Small", "Medium", "Large", "Extra Large")); AnsiConsole.MarkupLine($"You selected: [yellow]{size}[/]");Run it:
dotnet runYou should see a list of pizza sizes with one highlighted. Use the up/down arrow keys to move between options, then press Enter to select.
Notice how the prompt handles all the keyboard interaction for you? No need to parse input or validate choices - Spectre.Console takes care of it.
That's an interactive menu with just a few lines of code.
- 3
Select Your Toppings
What if the user wants to pick multiple items? That's where
MultiSelectionPromptcomes in. Users can toggle items with the spacebar and confirm with Enter:var toppings = AnsiConsole.Prompt( new MultiSelectionPrompt<string>() .Title("What [green]toppings[/] would you like?") .NotRequired() .InstructionsText("[grey](Press [blue]<space>[/] to toggle, [green]<enter>[/] to confirm)[/]") .AddChoices("Pepperoni", "Mushrooms", "Sausage", "Onions", "Green Peppers", "Black Olives", "Extra Cheese", "Bacon", "Pineapple")); if (toppings.Count == 0) { AnsiConsole.MarkupLine("A plain cheese pizza - classic choice!"); } else { AnsiConsole.MarkupLine($"Toppings: [yellow]{string.Join(", ", toppings)}[/]"); }Run it:
dotnet runYou should see a list of toppings with checkboxes. Press Space to select or deselect items, use arrow keys to navigate, and press Enter when you're done.
Notice the
NotRequired()call? That allows the user to select zero items (for a plain cheese pizza). Without it, at least one selection would be required.Your users can now make multiple selections.
- 4
Confirm the Order
Before placing the order, let's ask for confirmation. The
Confirm()method presents a simple yes/no question:var confirmed = AnsiConsole.Confirm("Place this order?"); if (confirmed) { AnsiConsole.MarkupLine("[green]Order placed![/]"); } else { AnsiConsole.MarkupLine("[yellow]Order cancelled.[/]"); }Run it:
dotnet runYou should see "Place this order? [y/n]" with options to type
yorn. The method returnstruefor yes andfalsefor no.Notice how the
[y/n]hint is automatically added? Spectre.Console handles the common UX patterns for you.Now you can get confirmation before important actions.
- 5
Complete Pizza Order
Let's put it all together into a complete ordering flow with a styled order summary:
AnsiConsole.MarkupLine("[bold yellow]Welcome to Spectre Pizza![/]"); AnsiConsole.WriteLine(); // Ask for name var name = AnsiConsole.Ask<string>("What's your [green]name[/]?"); // Choose size var size = AnsiConsole.Prompt( new SelectionPrompt<string>() .Title("What [green]size pizza[/] would you like?") .AddChoices("Small", "Medium", "Large", "Extra Large")); // Select toppings var toppings = AnsiConsole.Prompt( new MultiSelectionPrompt<string>() .Title("What [green]toppings[/] would you like?") .NotRequired() .InstructionsText("[grey](Press [blue]<space>[/] to toggle, [green]<enter>[/] to confirm)[/]") .AddChoices("Pepperoni", "Mushrooms", "Sausage", "Onions", "Green Peppers", "Black Olives", "Extra Cheese", "Bacon", "Pineapple")); // Show order summary AnsiConsole.WriteLine(); var panel = new Panel( new Rows( new Markup($"[bold]Customer:[/] {name}"), new Markup($"[bold]Size:[/] {size}"), new Markup($"[bold]Toppings:[/] {(toppings.Count > 0 ? string.Join(", ", toppings) : "Plain cheese")}"))) .Header("[yellow]Order Summary[/]") .Border(BoxBorder.Rounded); AnsiConsole.Write(panel); AnsiConsole.WriteLine(); // Confirm order if (AnsiConsole.Confirm("Place this order?")) { AnsiConsole.MarkupLine($"[green]Order placed! Thanks, {name}![/]"); } else { AnsiConsole.MarkupLine("[yellow]Order cancelled.[/]"); }Run the complete application:
dotnet runYou should see the full ordering experience: enter your name, pick a size, select toppings, review the summary in a styled panel, and confirm your order.
Notice how we used a
Panelto display the order summary? Combining prompts with other Spectre.Console widgets creates polished, professional interfaces.That's a complete interactive ordering flow.
Congratulations!
We've built a pizza ordering system that demonstrates all the core prompting features. Our application asks for text input, presents single-choice and multiple-choice menus, displays a styled summary, and confirms the order before processing.
These same techniques work for any interactive console application: configuration wizards, CLI tools, installation scripts, and more.
Next Steps
- Showing Status and Spinners - Display animated feedback while operations run
- Text Prompts Reference - Explore validation, secrets, and default values
- Selection Prompts Reference - Learn about grouping, search, and styling options