Conzole 0.4.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package Conzole --version 0.4.4                
NuGet\Install-Package Conzole -Version 0.4.4                
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="Conzole" Version="0.4.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Conzole --version 0.4.4                
#r "nuget: Conzole, 0.4.4"                
#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 Conzole as a Cake Addin
#addin nuget:?package=Conzole&version=0.4.4

// Install Conzole as a Cake Tool
#tool nuget:?package=Conzole&version=0.4.4                

Conzole

Conzole is a collection of static helper functions to simplify data display and input from the command line in DotNet Core.

Usage

The following contains examples of library usage. These functions can be accessed through the ConzoleUtils static class.

Count

The Count function takes a collection of items and displays the size of the collection. The simplest usage will take a collection of objects and display the count in the collection using the default format:

var items = new int[] { 3, 4, 2 };
ConzoleUtils.Count(items);

Output:

3 result(s)

Alternative formatting can be applied for a line by supplying a custom formatter through the CountOptions class:

var items = new int[] { 3, 4, 2 };
var countOptions = new CountOptions();
countOptions.ResultFormatter = count => $"{count}!!!";

ConzoleUtils.Count(items, countOptions);

Output:

3!!!

List

The List function takes a collection of items and displays the as a list. The simplest usage will take a collection of objects and convert them to their string representation. For example, the following will list three integers and display with the default format:

var items = new int[] { 3, 4, 2 };
ConzoleUtils.List(items);

Output:

1) 3
2) 4
3) 2

Alternative formatting can be applied for a line by supplying a custom formatter through the LineOptions class:

var items = new int[] { 3, 4, 2 };
var listOptions = new ListOptions<int>();
listOptions.LineFormatter = (index, item) => $"{item}::{index}";

ConzoleUtils.List(items, listOptions);

Output:

1::3
2::4
3::2

Prompt

The Prompt functions request the user to input a value on console. The simplest case for a prompt takes an optional text prompt to display to the user then reads from the console and returns the result:

var result = ConzoleUtils.Prompt("Enter your name here:");

The other prompt functions are built on top of this function and also convert the resulting string into another data type:

var success1 = ConzoleUtils.PromptInt("Enter your int here:", out var result);
var success2 = ConzoleUtils.PromptDouble("Enter your double here:", out var result);

Since these fuctions are built on top of the TryParse paradigm they follow the pattern of returning a boolean indicating whether a valid value was provided.

Repeat Until Success

The Repeat Until Success function attempts an action in a loop until either the action returns true or the user provides the negative response.

var success = await ConzoleUtils.RepeatUntilSuccessAsync(() =>
{
    return Task.FromResult(true);
});

The menu function allows the user to define a navigable menu structure and attach hooks for actions to be performed when entering those menus. For example, the following defines a navigable root menu and nested menu:

var nestedMenu = new Menu("nested menu");
nestedMenu.AddMenuItem("a", new MenuItem("a1", () => Task.CompletedTask));
nestedMenu.AddMenuItem("b", new MenuItem("b1", () => Task.FromResult(true)));
nestedMenu.AddMenuItem("c", new MenuItem("c1", () => Task.FromResult(false)));

var rootMenu = new Menu("root menu");
rootMenu.SetExitMenuItem("Exit", "X");
rootMenu.AddMenuItem("I", nestedMenu);
rootMenu.AddMenuItem("II", new MenuItem("root action", () => Task.CompletedTask));

Initially this menu will display:

root menu
I) nested menu
II) root action
X) Exit

Enter selection:

Entering "II" will cause the menu to repeat since the Action provided simply returns a completed Task, but entering "I" will navigate to the nested menu and display:

nested menu
a) a1
b) b1
c) c1
0) Back

Enter selection:

Entering either "a" or "b" will cause the menu to repeat since returning a completed Task or true wrapped in a Task have the same effect. However entering "c" returns a false wrapped in a Task which has the result of exiting the current menu and returning to the parent menu. The default exit action "0" always returns false and exits the current menu, returning to the parent.

Functions can be substituted for the lambdas in the example above to provide hooks into the menu items.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.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
0.4.5 614 6/3/2020
0.4.4 433 5/27/2020
0.4.2 500 5/23/2020
0.4.1 510 5/17/2020
0.4.0 499 5/17/2020
0.3.0 514 5/17/2020
0.2.0 466 5/16/2020
0.1.4 529 5/15/2020
0.1.3 428 5/15/2020
0.1.2 440 5/15/2020
0.1.1 443 5/14/2020
0.1.0 454 5/14/2020

Added initial support for parsing command line arguments, as well as an addition Menu function for selecting items from a flat collection.