Rephidock.ConsolePrompts 2.0.0

dotnet add package Rephidock.ConsolePrompts --version 2.0.0                
NuGet\Install-Package Rephidock.ConsolePrompts -Version 2.0.0                
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="Rephidock.ConsolePrompts" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Rephidock.ConsolePrompts --version 2.0.0                
#r "nuget: Rephidock.ConsolePrompts, 2.0.0"                
#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 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

GitHub License Badge

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.");
}

image: example_prompt_age

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}");

image: example_styled_float

See Demo Project on the source repository for a more full tutorial.

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • 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.

Version Downloads Last updated
2.0.0 105 10/19/2024
1.1.0 280 11/1/2023
1.0.1 127 10/20/2023
1.0.0 153 10/20/2023

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.