Rephidock.ConsolePrompts
2.0.0
dotnet add package Rephidock.ConsolePrompts --version 2.0.0
NuGet\Install-Package Rephidock.ConsolePrompts -Version 2.0.0
<PackageReference Include="Rephidock.ConsolePrompts" Version="2.0.0" />
paket add Rephidock.ConsolePrompts --version 2.0.0
#r "nuget: Rephidock.ConsolePrompts, 2.0.0"
// Install Rephidock.ConsolePrompts as a Cake Addin #addin nuget:?package=Rephidock.ConsolePrompts&version=2.0.0 // Install Rephidock.ConsolePrompts as a Cake Tool #tool nuget:?package=Rephidock.ConsolePrompts&version=2.0.0
Console Prompts
A .NET library to take user input in a console with exception handling and fluent syntax.
Features
- User input queries with fluent syntax
- Support for strings,
IParsable
, including numbers, and booleans - Input restrictions (e.g. numeric range, string length, path to an existing file)
- Invalid input handling
- Hints and styling
Usage
Use the Prompt
class to create a query with one of the For
methods, add restrictions 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.");
}
Advanced Use
Use the Prompter
class to customize how prompts and hints are displayed.
Prompter prompter = new Prompter(autoSetupHints: false);
// Set up all hints to be displayed
prompter.SetHintHandlers(PromptHintHandlers.GetAllHandlers());
// Change format of prompts
prompter.PromptFormat = "[{1}] {0} := ";
prompter.HintSeparator = " & ";
prompter.InvalidInputFormat = "Not accepted: {0}";
// Example prompt
float x = prompter
.PromptFor<float>("x")
.AddTypeHint()
.ForceFinite()
.OfRange(-1, 1)
.NotEqualTo(0)
.Display();
Console.WriteLine($"f(x) = 60 + 10 * {x} = {60 + 10 * x}");
See Demo Project on the source repository for a more full tutorial.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. 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. |
-
net7.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.
Major update 2.0:
- (Massively breaking!) Completely reworked prompt hints and styling. See Demo project.
- New class: `Prompter`. Handles `Prompt` creation, hint display and style.
- Simplified `Prompt.For<T> where T : INumber<T>`.
- Removed `Prompt<T>.SetParserFormat` as it is now resposibility of `Prompter`.
- Prompts can now work with input and output streams other than the Console.
- Prompts without text can now display hints.
- Type hints are now added directly to Prompts.
- Added a way to remove prompt's hints matching a predicate or remove all of the hints.
- Removed built-in invalid input exception message changes.
- Renamed the class containing validators to be in line with new classes.