Skip to content

Pjotrtje/ConsoleAsksFor

Repository files navigation

ConsoleAsksFor

Icon

ConsoleAsksFor is library for asking input in a console app easily.

Azure DevOps builds (branch)

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 on NuGet ConsoleAsksFor downloads on NuGet
ConsoleAsksFor.Microsoft.DependencyInjection ConsoleAsksFor.Microsoft.DependencyInjection on NuGet ConsoleAsksFor.Microsoft.DependencyInjection downloads on NuGet
ConsoleAsksFor.NodaTime.ISO ConsoleAsksFor.NodaTime.ISO on NuGet ConsoleAsksFor.NodaTime.ISO downloads on NuGet

Setup

Register Console with ConsoleAsksFor.Microsoft.DependencyInjection:

services.AddConsoleAsksFor();

Resolve a service which has dependency on IConsole of resolve service directly:

var console = serviceProvider.GetRequiredService<IConsole>();

Or create with factory:

ConsoleFactory.Create();

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?", new[] { "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 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?", new[] { "Whale", "Yesterday", "Some", "Stereo", "Random" });

Demo

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

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 :).