https://github.com/rephidock/rephidock.consoleprompts
A library to take user input in a console with exception handling and fluent syntax.
https://github.com/rephidock/rephidock.consoleprompts
console console-io csharp csharp-library
Last synced: 3 months ago
JSON representation
A library to take user input in a console with exception handling and fluent syntax.
- Host: GitHub
- URL: https://github.com/rephidock/rephidock.consoleprompts
- Owner: Rephidock
- License: mit
- Created: 2023-10-04T18:54:52.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-11-01T14:26:11.000Z (over 1 year ago)
- Last Synced: 2024-04-27T06:06:53.600Z (about 1 year ago)
- Topics: console, console-io, csharp, csharp-library
- Language: C#
- Homepage:
- Size: 96.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Console Prompts
[](https://github.com/Rephidock/Rephidock.ConsolePrompts/blob/main/LICENSE)
[](https://www.nuget.org/packages/Rephidock.ConsolePrompts)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.
```csharp
int userAge = Prompt.For("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.
```csharp
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("x")
.AddTypeHint()
.ForceFinite()
.OfRange(-1, 1)
.NotEqualTo(0)
.Display();Console.WriteLine($"f(x) = 60 + 10 * {x} = {60 + 10 * x}");
```
See [Demo Project](./src/Rephidock.ConsolePrompts.Demo) for a more full tutorial.
## Installation
### NuGet
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.