ConsoleAsksFor 4.1.0
dotnet add package ConsoleAsksFor --version 4.1.0
NuGet\Install-Package ConsoleAsksFor -Version 4.1.0
<PackageReference Include="ConsoleAsksFor" Version="4.1.0" />
paket add ConsoleAsksFor --version 4.1.0
#r "nuget: ConsoleAsksFor, 4.1.0"
// Install ConsoleAsksFor as a Cake Addin #addin nuget:?package=ConsoleAsksFor&version=4.1.0 // Install ConsoleAsksFor as a Cake Tool #tool nuget:?package=ConsoleAsksFor&version=4.1.0
ConsoleAsksFor
ConsoleAsksFor is library for asking input in a console app easily.
Goal
The aim of ConsoleAsksFor is removing the hassle of retrieving/parsing/retrying input in a console app.
Installation
You can download the latest release / pre-release NuGet packages from nuget:
Package: | ||
---|---|---|
ConsoleAsksFor | ||
ConsoleAsksFor.NodaTime.ISO |
Setup
Create with console factory (make sure it is first line in console so it can highjack Console.Out and Console.Error):
ConsoleFactory.Create();
And register in DI
services.AddSingleton(console)
Start using
Some examples:
var isSure = await console.AskForBool("Are you sure?", cancellationToken: cancellationToken);
var age = await console.AskForInt("What is your age?", Between(0, 125));
var length = await console.AskForDecimal("What is your length (in meters)?", Scale.Two, Between(0m, 2.5m));
var appointmentStart = await console.AskForDateTimeOffset("How late should we meet?", TimeZoneInfo.Local, AtLeast(DateTimeOffset.Now), defaultValue: DateTimeOffset.Now.AddHours(1));
var favoriteColor = await console.AskForEnum<ConsoleColor>("What is your favorite color?");
var preferredName = await console.AskForItem("Which name do you prefer?", ["Jantje", "Pietje"]);
var directory = await console.AskForExistingDirectory("Where to store file?", defaultValue: new DirectoryInfo(@"C:\Temp"));
var name = await console.AskForString("What is your name?");
var zipcode = await console.AskForString("What is your Dutch zipcode?", new Regex("^[1-9][0-9]{3}[A-Z]{2}$"), "Format: '5555AA' where first digit is not a 0");
var agb = await console.AskForStringBasedValueObject<Agb>("What is your agb?", Agb.TryParse, x => x.ToString(), "8 numbers");
var uzovi = await console.AskForStringBasedValueObject<Uzovi>("What is your uzovi?", "4 numbers"); // Uzovi implements IParsable<Uzovi> and IFormattable
var pw = await console.AskForPassword("What is the secret code?");
Demo
var console = ConsoleFactory.Create();
console.WriteInfoLine("Tip: Use arrows to go through history.");
var wordOfTheDay = await console.AskForString("What is your word of the day?");
console.WriteInfoLine("Tip: Use tab for intellisense.");
var likableWords = await console.AskForItems("Which of these words do you like?", ["Whale", "Yesterday", "Some", "Stereo", "Random"]);
Features
- Default values
- History
- ByQuestionTextAndType (default)
- ByQuestionType
- NotFiltered
- Basic Intellisense
- Colors
- Visual feedback whether current answer is correct
- Different colors for different line types
- Customizable
- Cancelable
- Support for cancellationTokens
- Push F12 to throw TaskCanceledByF12Exception
- Basic multithreading support:
- During asking questions other output to Console is suspended
- When multiple threads are asking questions, questions are queued
Supported types
ConsoleAsksFor
- AskForBool
- AskForByte
- AskForSignedByte
- AskForShort
- AskForUnsignedShort
- AskForInt
- AskForUnsignedInt
- AskForLong
- AskForUnsignedLong
- AskForDecimal
- AskForDateOnly
- AskForTimeOnly
- AskForDateTime
- AskForDateTimeOffset
- AskForTimeSpan
- AskForDirectory
- AskForExistingDirectory
- AskForNewDirectory
- AskForFileName
- AskForExistingFileName
- AskForNewFileName
- AskForEnum<T>
- AskForFlaggedEnum<T>
- AskForItem<T>
- AskForItems<T>
- AskForString
- AskForStringBasedValueObject<T>
- AskForPassword
ConsoleAsksFor.NodaTime.ISO
- AskForInstant
- AskForLocalDate
- AskForLocalDateTime
- AskForLocalTime
- AskForZonedDateTime
- AskForAnnualDate
- AskForYearMonth
Release notes
See the Releases page.
Versioning
ConsoleAsksFor follows Semantic Versioning 2.0.0 for the releases published to nuget.org.
Credits
- Sergey Shumov for showing his AsyncAutoResetEvent.
- Jerrie Pelser for showing how to enable ansi colors in windows.
- JSQuareD for showing how to cancel Console.ReadKey.
- Sergey for showing how to lock resource in combination with async/await.
- Asbjørn Ulsberg for showing how to get amount of digits after decimal point.
Other
Another reason to make ConsoleAsksFor is because I have never worked with github and never created public nuget package. So this is a little brain exercise I guess 😃.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. net9.0 is compatible. |
-
net8.0
- No dependencies.
-
net9.0
- No dependencies.
NuGet packages (2)
Showing the top 2 NuGet packages that depend on ConsoleAsksFor:
Package | Downloads |
---|---|
ConsoleAsksFor.NodaTime.ISO
NodaTime (ISO) integration for ConsoleAsksFor. |
|
ConsoleAsksFor.Microsoft.DependencyInjection
Microsoft DependencyInjection integration for ConsoleAsksFor. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
4.1.0 | 93 | 11/15/2024 |
4.1.0-prerelease-2024111511... | 70 | 11/15/2024 |
4.0.0 | 102 | 11/3/2024 |
3.1.0 | 542 | 12/27/2022 |
3.0.0 | 707 | 5/10/2022 |
2.0.0 | 437 | 12/31/2021 |
2.0.0-prerelease-2021123111... | 217 | 12/31/2021 |
1.0.0 | 569 | 10/4/2021 |
1.0.0-prerelease-2021100310... | 379 | 10/3/2021 |