Micro-framework for .NET console application. Cocona makes it easy and fast to build console applications on .NET.
Micro-framework for .NET console application. Cocona makes it easy and fast to build console applications on .NET.
Micro-framework for .NET Core console application. Cocona makes it easy and fast to build console applications on .NET.
CoconaApp.Run((string? name, bool hey) =>
Console.WriteLine($"{(hey ? "Hey" :"Hello")} {(name ?? "Guest")}!"));public method as a commandgetopt/getopt_long like options)-rf / and -r -f / :-)myapp --foo --bar -n arg0 "arg1" (e.g. dir, cp, ls ...)myapp server -m "Hello world!" (e.g. dotnet, git, kubectl ...)-h or --help.bash and zshMicrosoft.Extensions.* framework. Cocona natively supports Logging, DI, Configuration and ConsoleLifetime.Microsoft.Extensions.*? then you can use a lightweight version of Cocona (named Cocona.Lite).You can find sample code for various features.
Install NuGet package from NuGet.org
$ dotnet add package Cocona
# A lightweight version is also available if you prefer less dependency.
$ dotnet add package Cocona.Liteusing Cocona;
CoconaApp.Run((string name) =>
{
Console.WriteLine($"Hello {name}");
})using Cocona;
class Program
{
static void Main(string[] args)
{
// Cocona parses command-line and executes a command.
CoconaApp.Run<Program>(args);
}
// public method as a command ™
public void Hello(string name)
{
Console.WriteLine($"Hello {name}");
}
}$ dotnet run
Usage: ConsoleAppSample [--name <String>]
Options:
--name <String> (Required)
-h, --help Show help message
--version Show version
$ dotnet run -- --name Cocona
Hello CoconaIf your application runs on .NET Core 3.0 or later, you can publish the app as a single-file executable. (see. What's new in .NET Core 3.0)
PS> dotnet publish -r win-x64 -p:PublishSingleFile=true
PS> app.exe --name Cocona
$ dotnet publish -r linux-x64 -p:PublishSingleFile=true
$ ./app --name CoconaIf your application has a single command, you can easily define and run it with CoconaApp.Run.
CoconaApp.Run((string name, int age) => { ... });This is equivalent to the following code using the Minimal API Builder.
var builder = CoconaApp.CreateBuilder();
var app = builder.Build();
app.AddCommand((string name, int age) => { ... });
app.Run();If you want your application to have more than one command, you can add named commands. See Sub commands for details.
var app = CoconaApp.Create(); // is a shorthand for `CoconaApp.CreateBuilder().Build()`
app.AddCommand("list", () => { ... });
app.AddCommand("add", () => { ... });
app.AddCommand("delete", () => { ... });
app.Run();You can add (classic) Class-based style commands with the AddCommands<T> method.
app.AddCommands<MyCommand>();By default, Cocona treats public methods as commands.
If an application has one public method, Cocona calls it on startup. If there are more than one, they are treated as sub-commands. (see also Sub commands)
// Treats a method name as a command name. (Below method is named `command`)
public void Command() { ... }
// Specify a command name using CommandAttribute.
[Command("commandname")]
public void Command() { ... }
// Cocona will ignore this method.
[Ignore]
public void Ignored() { ... }If you want to specify a method as a command manually, set false to TreatPublicMethodsAsCommands option at startup. All command methods require CommandAttribute.
CoconaApp.Run<Program>(args, options =>
{
// If the option value is `false`, All command methods require `CommandAttribute`.
options.TreatPublicMethodsAsCommands = false;
});Cocona exposes method parameters as command-line options (also known as flags).
// This command accepts `--name <string>` and `--hey` options.
app.AddCommand((string name, bool hey) => { ... });// This command accepts `--name <string>` and `--hey` options.
public void Hello(string name, bool hey) { ... }If the parameter of a method is defined as nullable, Cocona will treat them as non-mandatory option for a command. (That is, the parameters are treated as required option by default excepts boolean).
If a parameter is boolean, it's assumed that false default value is specified.
// `--name` is non-mandatory option.
// If the user runs the application without this option, the parameter will be `null`.
app.AddCommand((string? name) => { ... });If method parameters are optional argument, Cocona treats those as optional command options. (That is, the parameters are treated as required option by default excepts boolean).
If a parameter is boolean, it's assumed that false default value is specified.
// `--name "default user"` is specified implicity.
public void Hello(string name = "default user") { ... }Do you want to use short-name option -f instead of --force?
You can specify short-name to an option using OptionAttribute.
// The command accepts `-f` or `--force` option.
// Cocona's command-line parser accepts getopt-like styles. See below.
// $ remove --force --recursive
// $ remove -r -f
// $ remove -rf
app.AddCommand(([Option('f')]bool force, [Option('r')]bool recursive) => { ... });// The command accepts `-f` or `--force` option.
// Cocona's command-line parser accepts getopt-like styles. See below.
// $ remove --force --recursive
// $ remove -r -f
// $ remove -rf
public void Remove([Option('f')]bool force, [Option('r')]bool recursive) { ... }If a parameter is T[] or IEnumerable<T>, a command accepts one or more options by the same name.
// $ compile -I../path/to/foo.h -I/usr/include/bar.h -I/usr/include/baz.h nantoka.c
// include = new [] { "../path/to/foo.h", "/usr/include/bar.h", "/usr/include/baz.h" };
app.AddCommand(([Option('I')]string[] include, [Argument]string file) => { ... });// $ compile -I../path/to/foo.h -I/usr/include/bar.h -I/usr/include/baz.h nantoka.c
// include = new [] { "../path/to/foo.h", "/usr/include/bar.h", "/usr/include/baz.h" };
public void Compile([Option('I')]string[] include, [Argument]string file) { ... }You can also specify a description for options that appear in the help.
app.AddCommand((
[Option(Description = "Description of the option")] int value,
[Argument(Description = "Description of the argument")]string arg
) => { ... });public void HasDescription([Option(Description = "Description of the option")] int value, [Argument(Description = "Description of the argument")]string arg) { ... }Usage: CoconaSample.InAction.CommandOptions has-description [--value <Int32>] [--help] arg
Arguments:
0: arg Description of the argument (Required)
Options:
--value <Int32> Description of the option (Required)
-h, --help Show help messageCommand-line arguments are defined as method parameters as same as options.
// ./app alice karen
app.AddCommand(([Argument]string from, [Argument]string to) => { ... });// ./app alice karen
public void Hello([Argument]string from, [Argument]string to) { ... }You can define a parameter as T[]. It allows defining cp-like command which accepts many file paths and one destination path (cp file1 file2 file3 dest).
// ./copy file1 file2 file3 dest
app.AddCommand(([Argument]string[] src, [Argument]string dest) => { ... });// ./copy file1 file2 file3 dest
public void Copy([Argument]string[] src, [Argument]string dest) { ... }You can add multiple commands with names and expose them as sub-commands. You can implement an application that has sub-commands similar to dotnet, git, kubectl etc...
var app = CoconaApp.Create();
app.AddCommand("hello", ([Argument]string name) => Console.WriteLine($"Hello {name}!"))
.WithDescription("Say hello");
app.AddCommand("bye", ([Argument]string name) => Console.WriteLine($"Goodbye {name}!"))
.WithDescription("Say goodbye");
app.Run();If a command type has more than one public method or [Command], those commands are exposed as sub-commands. You can implement an application that has sub-commands similar to dotnet, git, `kubect
No open issues yet, or sync has not completed.