InputHandler 1.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package InputHandler --version 1.2.1                
NuGet\Install-Package InputHandler -Version 1.2.1                
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.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add InputHandler --version 1.2.1                
#r "nuget: InputHandler, 1.2.1"                
#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.2.1

// Install InputHandler as a Cake Tool
#tool nuget:?package=InputHandler&version=1.2.1                

InputHandler

A small library for handling user input in console apps

All methods are contained in a static class Input within the InputHandler namespace.

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) {}
List<T> ListUntilEmpty(Func<string> getString, Func<string, T> convert) {}

// Takes input using getString until shouldStop returns true.
List<string> ListUntil(Func<string> getString, Predicate<string> shouldStop, bool includeFinal = false) {}
List<T> ListUntil(Func<string> getString, Predicate<string> shouldStop, Func<string, T> convert, bool includeFinal = false) {}

// Yield-returns consecutive inputs grabbed by getString until a null, empty, or whitespace string is sent.
IEnumerable<string> YieldUntilEmpty(Func<string> getString)

// Yield-returns consecutive inputs grabbed by getString until shouldStop returns true.
IEnumerable<string> YieldUntil(Func<string> getString, Predicate<string> shouldStop, bool includeFinal = false)

// Prompts for input using getString until it receives a string contained in options. Can optionally use parameter 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 Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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.
  • net6.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.

Version Downloads Last updated
2.0.0 372 1/5/2022
1.2.1 302 12/17/2021
1.2.0 2,013 11/26/2021
1.1.0 327 10/21/2021
1.0.3 317 10/20/2021
1.0.2 343 10/20/2021
1.0.0 350 10/20/2021

v.1.2.1:
- added generic ListUntil and ListUntilEmpty