InputHandler 1.0.3
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package InputHandler --version 1.0.3
NuGet\Install-Package InputHandler -Version 1.0.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="InputHandler" Version="1.0.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add InputHandler --version 1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: InputHandler, 1.0.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install InputHandler as a Cake Addin #addin nuget:?package=InputHandler&version=1.0.3 // Install InputHandler as a Cake Tool #tool nuget:?package=InputHandler&version=1.0.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
InputHandler
A small library for handling user input
Methods Available:
// Prompts for input using getString until action does not throw an exception. Can optionally use parameter getMessage to specify an error message.
void Get(Func<string> getString, Action<string> action, Func<string, Exception, string> getMessage = null) {}
// Prompts for input using getString until check returns true. Can optionally use parameter getMessage to specify an error message.
string Get(Func<string> getString, Predicate<string> check, Func<string, string> getMessage = null) {}
// Prompts for input using getString until it receives y, n, yes, or no. Can optionally use parameter message to specify an error message.
bool GetYN(Func<string> getString, string message = "Invalid format... use y or n.") {}
// Prompts for input using getString until it receives a string contained in yesOptions or noOptions. Can optionally use parameter message to specify an error message.
bool GetYN(Func<string> getString, IEnumerable<string> yesOptions, IEnumerable<string> noOptions, string message = "Specified input is not a valid yes or no option. Please try a different input.") {}
// Prompts for input using getString until it receives y or n. Can optionally use parameter message to specify an error message.
bool GetYNStrict(Func<string> getString, string message = "Invalid format... use y or n.") {}
// Prompts for input using getString until convert does not throw an exception. Can optionally use parameter message to specify an error message.
bool GetBool(Func<string> getString, Predicate<string> convert, Func<string, Exception, string> getMessage = null) {}
// Takes input using getString until a null, empty, or whitespace string is sent.
string GetUntilEmpty(Func<string> getString) {}
// Takes input using getString until shouldStop returns true.
string GetUntil(Func<string> getString, Predicate<string> shouldStop) {}
// Takes input using getString until a null, empty, or whitespace string is sent.
List<string> ListUntilEmpty(Func<string> getString) {}
// Takes input using getString until shouldStop returns true.
List<string> ListUntil(Func<string> getString, Predicate<string> shouldStop) {}
// Prompts for input using <paramref name="getString"/> until it receives a string contained in <paramref name="options"/>. Can optionally use parameter <paramref name="message"/> to specify an error message.
string GetOption(Func<string> getString, IEnumerable<string> options, string message = "Value inputted is not a valid option.")
Example Program:
using System;
using System.Collections.Generic;
using System.Linq;
using InputHandler;
public class Program
{
// sample project, gets numbers and either adds or multiples them depending on user input
public static void Main()
{
string[] addOptions = new string[] { "add", "addition", "+", "plus", "sum", "a" };
string[] multOptions = new string[] { "multiply", "multiplication", "*", "x", "mult", "product", "m" };
Console.WriteLine("Would you like to add or multiply?");
string optionChosen = Input.Get(Console.ReadLine, s => addOptions.Contains(s) || multOptions.Contains(s));
bool isAddition = addOptions.Contains(optionChosen);
List<double> values = new();
while (true)
{
Console.WriteLine("Take another value? (y/n)");
if (Input.GetYN(Console.ReadLine))
{
Console.WriteLine("Enter value:");
Input.Get(Console.ReadLine, s => values.Add(double.Parse(s)), (_, _) => "Must enter a value that can be parsed to double.");
}
else break;
}
if (values.Count == 0)
{
Console.WriteLine("Must have values to complete operation!");
return;
}
if (isAddition)
{
double sum = 0;
foreach (var x in values) sum += x;
Console.WriteLine($"Sum is: {sum}");
}
else
{
double product = 1;
foreach (var x in values) product *= x;
Console.WriteLine($"Product is: {product}");
}
/* Note: if you'd like to take trimmed lowercase input for the operation, for example, you could do something like this:
*
* string optionChosen = Input.Get(() => Console.ReadLine().Trim().ToLower(), s => addOptions.Contains(s) || multOptions.Contains(s));
*
*/
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net5.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
v.1.0.3:
- added GetOption method
- added README.md
- updated some documentation