A small .NET library to take user input in a console with some exception handling and fluent syntax.
- User input queries with fluent syntax
- Input restrictions (e.g. numeric range, string length, path to an existing file)
- Invalid input handling
- Prompt styling
- Support for
IParsable
Use the Prompt
class to create a query with one of the For
methods. Add limits to the query with fluent syntax and Display
the query to the user.
int userAge = Prompt.For<int>("Your age").NoLessThan(1).Display();
const int drinkingAge = 21;
if (userAge >= drinkingAge)
{
Console.WriteLine("You are of drinking age!");
}
else
{
Console.WriteLine("Sorry, you can't have a drink.");
}
The way prompts are displayed can be changed with PromptStyler
class.
PromptStyler.PromptFormat = "[{1}] {0} = ";
PromptStyler.InvalidInputFormat = "I can't accept that: {0}";
PromptStyler.HintLevel = PromptHintLevel.Verbose;
Console.WriteLine("f(x) = 60 + 10x");
float x = Prompt
.For<float>("x")
.ForceFinite()
.OfRange(0, 1)
.AddHint("real", PromptHintLevel.Verbose)
.Display();
Console.WriteLine($"f(x) = 60 + 10 * {x} = {60 + 10 * x}");
See Demo Project for some other examples.
The best way to add the library to your project is via NuGet package manager. Use the .NET CLI command:
dotnet add package Rephidock.ConsolePrompts
or the package browser in the IDE of your choice.
Alternatively you can download the library files manually.
-
Open the Releases page.
-
Download the
.dll
. -
(Optional, Recommended) Also download the
.xml
file of the same release.The
.xml
file contains documentation and should be placed in the same directory as the.dll
for hints to appear in an IDE. -
Add the dependency in your
.csproj
file:<ItemGroup> <Reference Include="Rephidock.ConsolePrompts"> <HintPath>Relative\Or\Absolute\Path\To\Rephidock.ConsolePrompts.dll</HintPath> </Reference> </ItemGroup>