Make default answer for confirmation prompt configurable

This commit is contained in:
Thomas Freudenberg 2021-02-15 11:08:47 +01:00 committed by Patrik Svensson
parent 04d0e663d5
commit ed0fb29be4
2 changed files with 13 additions and 3 deletions

View File

@ -38,10 +38,15 @@ namespace Spectre.Console
/// Displays a prompt with two choices, yes or no.
/// </summary>
/// <param name="prompt">The prompt markup text.</param>
/// <param name="defaultValue">Specifies the default answer.</param>
/// <returns><c>true</c> if the user selected "yes", otherwise <c>false</c>.</returns>
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);
}
}
}

View File

@ -17,6 +17,11 @@ namespace Spectre.Console
/// </summary>
public char No { get; set; } = 'n';
/// <summary>
/// Gets or sets a value indicating whether "yes" is the default answer.
/// </summary>
public bool DefaultValue { get; set; } = true;
/// <summary>
/// Gets or sets the message for invalid choices.
/// </summary>
@ -51,7 +56,7 @@ namespace Spectre.Console
.ValidationErrorMessage(InvalidChoiceMessage)
.ShowChoices(ShowChoices)
.ShowDefaultValue(ShowDefaultValue)
.DefaultValue(Yes)
.DefaultValue(DefaultValue ? Yes : No)
.AddChoice(Yes)
.AddChoice(No);