C# Cocona
last modified January 18, 2024
In this article we create simple console applications in C# using the Cocona library.
Cocona is a library which makes it easy and fast to build console applications on .NET. The library mimics ASP.NET Core Minimal API. It has similar semantics like standard Unix tools.
There are two versions of the library: Cocona and Cocona.Light. The light version has less overheads and features. It has no Logging, DI, and configuration.
C# Cocona simple example
We have a very simple console application.
using Cocona; CoconaLiteApp.Run((string name) => { Console.WriteLine($"Hello {name}"); });
If our application has only one command, we can directly use the
CoconaLiteApp.Run
method. Cocona creates a command-line option
from the method parameter (name
).
$ dotnet run --name Peter Hello Peter
We pass a value to the --name
option. The program responds with
a hello message.
$ dotnet run -- --help Usage: FirstEx [--name <String>] [--help] [--version] FirstEx Options: --name <String> (Required) -h, --help Show help message --version Show version
Cocona automatically creates the application help. Since dotnet
command also has the --help
option, we must precede ours with
--
.
The previous code was a shortened version of the following one:
using Cocona; var builder = CoconaLiteApp.CreateBuilder(); var app = builder.Build(); app.AddCommand((string name) => { Console.WriteLine($"Hello {name}"); }); app.Run();
In this code example, we build the Cocona application with a builder and add
a new command with AddCommand
.
app.Run();
The Run
method starts the Cocona enabled application.
Class-based example
In class-based applications, public methods are automatically transformed into commands.
using Cocona; class Program { static void Main(string[] args) { CoconaLiteApp.Run<Program>(args); } public void Message(string name, int age) { Console.WriteLine($"{name} is {age} years old"); } }
The program accepts two options: --name
and --age
.
$ dotnet run --name John --age 34 John is 34 years old
Multiple named commands
We can add multiple named commands.
using Cocona; var app = CoconaLiteApp.Create(); app.AddCommand("add", () => { Console.WriteLine("add command"); }); app.AddCommand("remove", () => { Console.WriteLine("remove command");}); app.AddCommand("show", () => { Console.WriteLine("show command"); }); app.Run();
The example adds three named commands.
app.AddCommand("add", () => { Console.WriteLine("add command"); });
The first parameter is the name of the command. The second parameter is the delegate to the command body.
Flags
Flags can be added with the Option
attribute.
using Cocona; var app = CoconaLiteApp.Create(); app.AddCommand(([Option('f')] bool force, [Option('r')] bool recursive) => { Console.WriteLine(force); Console.WriteLine(recursive); }); app.Run();
The example adds two flags: -f
and -r
.
$ dotnet run -- -f -r True True $ dotnet run -- -fr True True
We can use both -f -r
and -fr
variations.
An array of values
An array of values can be inserted into an array. This option is not supported in the light Cocona application.
using Cocona; var app = CoconaApp.CreateBuilder().Build(); app.AddCommand(([Option('N')]string[] names) => { foreach (var name in names) { Console.WriteLine(name); } }); app.Run();
The program accepts one or more values for the -N
option.
app.AddCommand(([Option('N')]string[] names) =>
An array of strings is decorated with the [Option('N')]
attribute.
$ dotnet run -NPaul -NJane -NJohn -NLucy Paul Jane John Lucy
Source
In this article we have created simple console applications in C# using Cocona.
Author
List all C# tutorials.