β Back to Home β’ Next: Switch β Methods β
Toggle a single boolean between its on and off states. The user flips it with the arrow keys or Space and confirms with Enter.
The Switch control is the fastest way to collect one yes/no, enable/disable, or active/inactive
value. It shows two labeled states and lets the user flip between them; the labels can be plain text
or emoji, and the confirmed value can be persisted as history. Everything is configured through a
single fluent chain.
π Just need to display an on/off indicator without asking for input? Use the read-only Switch widget instead β it renders the same toggle but does not wait for the user.
| Sub-page | What you will find |
|---|---|
| Index (this page) | What it is, when to use it, a first working example, the method map |
| Methods | Every fluent method β signature, parameters, defaults, and a snippet |
| Operations | Keyboard, labels, history, and edge cases |
| Styles | The SwitchStyles regions and how to recolor them |
Use Switch when⦠|
Consider instead⦠|
|---|---|
| You need a single on/off boolean | β |
| You want a yes/no key-press confirmation | Confirm |
| The user should pick one of several options | Select |
| You only need to display a state, not collect one | Switch widget |
using PromptPlusLibrary;
var result = PromptPlus.Controls
.Switch("Enable feature?")
.Run();
if (!result.IsAborted)
PromptPlus.Console.WriteLine($"Enabled: {result.Content}");
Switch("Enable feature?") creates the control. The first argument is the prompt; an optional
second argument is a description line shown under it.false) and shows the localized Yes/No labels..Run() renders the toggle and blocks until the user presses Enter (confirm) or Esc (abort).ResultPrompt<bool?>: read .Content
for the state and .IsAborted to detect Esc.π‘ The value is nullable. On abort
.Contentisnullβ always checkIsAbortedfirst.
using PromptPlusLibrary;
var env = PromptPlus.Controls
.Switch("Environment")
.OnValue("Production") // label for the on state
.OffValue("Development") // label for the off state
.Default(false) // start on Development
.Run();
if (!env.IsAborted)
PromptPlus.Console.WriteLine($"Environment: {(env.Content == true ? "Production" : "Development")}");
This shows the two things you most often customize: the state labels (OnValue / OffValue)
and the starting value (Default). See Methods for the emoji label overloads and
Operations for how flipping behaves at runtime.
Grouped by purpose. Full signatures and examples are on the Methods page.
| Purpose | Methods |
|---|---|
| Starting value | Default |
| State labels | OnValue, OffValue |
| Dynamic description | ChangeDescription, ChangeDescriptionAsync |
| History | EnableHistory |
| Appearance & behavior | Styles, Options |
| Run | Run |
Switch returns ResultPrompt<bool?>.
| Member | Meaning |
|---|---|
.Content |
The confirmed state (true = on, false = off; null if aborted) |
.IsAborted |
true when the user pressed Esc / the abort key |
var (state, aborted) = PromptPlus.Controls.Switch("Enable?").Run();
if (!aborted) PromptPlus.Console.WriteLine($"{state}");