Trivial.Console
1.0.0
Please update to the latest version.
See the version list below for details.
dotnet add package Trivial.Console --version 1.0.0
NuGet\Install-Package Trivial.Console -Version 1.0.0
<PackageReference Include="Trivial.Console" Version="1.0.0" />
paket add Trivial.Console --version 1.0.0
#r "nuget: Trivial.Console, 1.0.0"
// Install Trivial.Console as a Cake Addin #addin nuget:?package=Trivial.Console&version=1.0.0 // Install Trivial.Console as a Cake Tool #tool nuget:?package=Trivial.Console&version=1.0.0
Trivial.Console
You can use the library for your console application to parse the arguments, write the selection, dispatch verbs, etc.
Import
Just add following namespace to your code file to use.
using Trivial.Console;
Parse
You can parse the arguments which is in string array or just in string to a readable object by programming.
// Suppose the arguments is:
// a.exe --name Kingcean Tuan --say Hello
void Main(string[] args)
{
var arguments = new Arguments(args);
Console.WriteLine("{0} {1}", arguments["say"], arguments["name"]);
// Console -> Hello Kingcean Tuan
}
It still work well when the arguments is from a string.
void Main(string[] args)
{
var str = Console.ReadLine();
// Suppose the str is:
// hijklmn abcdefg
var arguments = new Arguments(str);
Console.WriteLine("{0} {1}", arguments[1], arguments[0]);
// Console -> abcdefg hijklmn
}
Verb
If your application contains a set of functionalities, you may need verbs to help you to build a more flexible console application.
Each verb can has its own business logic. The arguments will be filled into the property with attribute ArgumentAttribute
.
class FirstVerb: Verb
{
[Argument("name")]
public string Name { get; set; }
public override string Description => "Test 1";
public override void Process()
{
Console.WriteLine("This is the verb handler 1.");
Console.WriteLine("Name is {0}.", Name);
Console.WriteLine("Name is {0}.", Arguments["name"]);
}
}
And you can also define a verb handler with async process method.
class SecondVerb: AsyncVerb
{
public override string Description => "Test 2";
public override async Task Process()
{
Console.WriteLine("This is the verb handler 2. Step 1.");
await Task.Run(() => {
Console.WriteLine("This is the verb handler 2. Step 2.");
});
}
}
Then you can use dispatcher to dispatch the correct verb handler to process.
void Main(string[] args)
{
var dispatcher = new Dispatcher();
dispatcher.Register<FirstVerb>("one");
dispatcher.Register<SecondVerb>("two");
dispatcher.Process(args);
// a.exe one --name Test
// Console -> This is the verb handler 1.
// Console -> Name is Test.
// Console -> Name is Test.
// a.ext two
// Console -> This is the verb handler 2. Step 1.
// Console -> This is the verb handler 2. Step 2.
}
Line
You can write a specified string value to the standard output stream and then change it. Following is a sample.
var Main(string[] args)
{
var line = new Line();
// Write something.
line.Write("Loading {0}......", "something");
// Remove some charactors and add other string with color.
line.Backspace(3);
line.Write(ConsoleColor.Green, " Done!");
// And you can append other string following above in the same line and in the default color.
line.Write(" This is only for test.");
// Add terminator.
line.End();
// You cannot add further terminator if there is no any string output.
line.End();
// So following will be in a new line.
line.Write("Next line. ");
// We can turn off the auto flush so that all strings write later will be in an output queue.
line.AutoFlush = false;
line.Write(ConsoleColor.Red, ConsoleColor.Yellow, "Red foreground and yellow background");
line.End();
line.Write("This will not be output immediately, neither.");
// Now let's write them.
line.Flush();
// Following will not output until we call Flush member method or set AutoFlush property as true.
line.Write(" Hello?");
line.AutoFlush = true;
line.End();
}
Select
You can have a way to list a lot of items so that user can select one of them just by arrow key.
var Main(string[] args)
{
// Create an instance for adding items and setting options.
var col = new Selection<string>();
// Add some items. For each item, you can set a string value,
// an additional data, an additional displayed title and an additional hot key.
col.Add("char a", "a", "char [a]", 'a');
col.Add("char b", "b", "char [b]", 'b');
for (var i = 0; i < 120; i++)
{
col.Add("num " + i, i.ToString());
}
// You can define a question string after the list.
col.Question = "Please select one: ";
// We can define the colors of the item selected.
col.SelectedForegroundColor = ConsoleColor.White;
col.SelectedBackgroundColor = ConsoleColor.Blue;
// The selected item will also be displayed after the question string.
// So you can define its color.
col.DefaultValueForegroundColor = ConsoleColor.Cyan;
// At the end of the list, the tips will be displayed before user press any key.
// There is a default value and you can customize it.
// And you can disable it by set it as null.
col.Tips = "Tips: You can use arrow key to select and press ENTER key to continue.";
// Then you can define its color.
col.TipsForegroundColor = ConsoleColor.Yellow;
// You can define the prefix for the item and the one selected.
col.SelectedPrefix = "√ ";
col.Prefix = " ";
// You can define the column count for the list.
col.Column = 5;
// You can define the maximum rows to displayed.
// A paging will be displayed if the count of the list is greater than it.
col.MaxRow = 10;
// Press ESC can cancel this selection.
// But you can enable the manual way by set a manual question
// so that user can type the words directly.
col.ManualQuestion = "Type: ";
// Write it to the standard output stream and wait for user selection.
var result = LineUtilities.Select(col);
// You can get the result.
Console.WriteLine("The result is {0}.", result.Value);
}
Product | Versions 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. |
-
.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 | |
---|---|---|---|
9.0.0-preview3 | 28 | 11/16/2024 | |
9.0.0-preview2 | 67 | 11/1/2024 | |
9.0.0-preview1 | 92 | 10/27/2024 | |
8.0.0 | 177 | 9/8/2024 | |
8.0.0-preview1 | 110 | 2/21/2024 | |
7.2.0 | 1,089 | 11/16/2023 | |
7.1.0 | 228 | 5/8/2023 | |
7.0.0 | 424 | 1/20/2023 | |
6.6.0 | 547 | 11/9/2022 | |
6.5.1 | 439 | 6/28/2022 | |
6.5.0 | 423 | 6/28/2022 | |
6.4.0 | 467 | 4/14/2022 | |
6.3.0 | 456 | 3/8/2022 | |
6.2.0 | 2,196 | 1/30/2022 | |
6.1.0 | 453 | 1/23/2022 | |
6.0.0 | 302 | 1/1/2022 | |
5.2.0 | 322 | 12/15/2021 | |
5.1.0 | 285 | 12/2/2021 | |
5.0.0 | 285 | 11/27/2021 | |
4.0.0 | 322 | 11/9/2021 | |
3.9.0 | 317 | 12/15/2021 | |
3.8.0 | 397 | 11/9/2021 | |
3.7.2 | 584 | 4/22/2021 | |
3.7.1 | 436 | 4/20/2021 | |
3.7.0 | 500 | 3/17/2021 | |
3.6.0 | 718 | 11/12/2020 | |
3.5.2 | 816 | 4/14/2020 | |
3.5.1 | 888 | 4/12/2020 | |
3.5.0 | 969 | 4/1/2020 | |
3.4.0 | 1,065 | 2/20/2020 | |
3.3.0 | 1,229 | 2/14/2020 | |
3.2.0 | 1,596 | 1/22/2020 | |
3.1.0 | 1,612 | 12/30/2019 | |
3.0.1 | 1,514 | 12/26/2019 | |
2.2.0 | 1,240 | 2/19/2020 | |
2.1.0 | 1,660 | 12/24/2019 | |
2.0.0 | 1,569 | 8/31/2019 | |
1.2.0 | 1,642 | 5/20/2019 | |
1.1.0 | 1,552 | 5/8/2019 | |
1.0.0 | 1,571 | 5/1/2019 |