From 2f1965d8413641d6ef63e38f1b5a0b5376982e4e Mon Sep 17 00:00:00 2001 From: tacosontitan <65432314+tacosontitan@users.noreply.github.com> Date: Wed, 31 Jan 2024 22:06:30 -0600 Subject: [PATCH] Added prompt function. --- src/Hussy.Net/Hussy.Net.csproj.DotSettings | 1 + src/Hussy.Net/Input/Prompt.cs | 25 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/Hussy.Net/Input/Prompt.cs diff --git a/src/Hussy.Net/Hussy.Net.csproj.DotSettings b/src/Hussy.Net/Hussy.Net.csproj.DotSettings index ebe8bda..b6b54f6 100644 --- a/src/Hussy.Net/Hussy.Net.csproj.DotSettings +++ b/src/Hussy.Net/Hussy.Net.csproj.DotSettings @@ -1,6 +1,7 @@  True True + True True True True diff --git a/src/Hussy.Net/Input/Prompt.cs b/src/Hussy.Net/Input/Prompt.cs new file mode 100644 index 0000000..6feff5e --- /dev/null +++ b/src/Hussy.Net/Input/Prompt.cs @@ -0,0 +1,25 @@ +namespace Hussy.Net; + +/// +/// Defines static methods for common programming tasks. +/// +public static partial class Hussy +{ + /// + /// Prompts the user with a message and reads the user's response, parsing it into the specified type. + /// + /// The type to parse the user's response into. + /// The message to display to the user. + /// The format provider to use for parsing the response. If null, the default format provider is used. + /// The parsed user's response of type T. + public static T P(string message, IFormatProvider? formatProvider = null) + where T : IParsable + { + Console.Write(message); + var response = Console.ReadLine(); + if (!T.TryParse(response, formatProvider, out var parsedResponse)) + throw new FormatException("Unable to parse response."); + + return parsedResponse; + } +} \ No newline at end of file