From ed0fb29be454fd27334c48276efa929cf0bc6f62 Mon Sep 17 00:00:00 2001 From: Thomas Freudenberg Date: Mon, 15 Feb 2021 11:08:47 +0100 Subject: [PATCH] Make default answer for confirmation prompt configurable --- src/Spectre.Console/AnsiConsole.Prompt.cs | 9 +++++++-- src/Spectre.Console/ConfirmationPrompt.cs | 7 ++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Spectre.Console/AnsiConsole.Prompt.cs b/src/Spectre.Console/AnsiConsole.Prompt.cs index f1a162b..c1bb92f 100644 --- a/src/Spectre.Console/AnsiConsole.Prompt.cs +++ b/src/Spectre.Console/AnsiConsole.Prompt.cs @@ -38,10 +38,15 @@ namespace Spectre.Console /// Displays a prompt with two choices, yes or no. /// /// The prompt markup text. + /// Specifies the default answer. /// true if the user selected "yes", otherwise false. - public static bool Confirm(string prompt) + public static bool Confirm(string prompt, bool defaultValue = true) { - return new ConfirmationPrompt(prompt).Show(Console); + return new ConfirmationPrompt(prompt) + { + DefaultValue = defaultValue, + } + .Show(Console); } } } diff --git a/src/Spectre.Console/ConfirmationPrompt.cs b/src/Spectre.Console/ConfirmationPrompt.cs index 5ee9983..148f28e 100644 --- a/src/Spectre.Console/ConfirmationPrompt.cs +++ b/src/Spectre.Console/ConfirmationPrompt.cs @@ -17,6 +17,11 @@ namespace Spectre.Console /// public char No { get; set; } = 'n'; + /// + /// Gets or sets a value indicating whether "yes" is the default answer. + /// + public bool DefaultValue { get; set; } = true; + /// /// Gets or sets the message for invalid choices. /// @@ -51,7 +56,7 @@ namespace Spectre.Console .ValidationErrorMessage(InvalidChoiceMessage) .ShowChoices(ShowChoices) .ShowDefaultValue(ShowDefaultValue) - .DefaultValue(Yes) + .DefaultValue(DefaultValue ? Yes : No) .AddChoice(Yes) .AddChoice(No);