Commands.NET 2.0.0-alpha.13

This is a prerelease version of Commands.NET.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package Commands.NET --version 2.0.0-alpha.13
                    
NuGet\Install-Package Commands.NET -Version 2.0.0-alpha.13
                    
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="Commands.NET" Version="2.0.0-alpha.13" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Commands.NET" Version="2.0.0-alpha.13" />
                    
Directory.Packages.props
<PackageReference Include="Commands.NET" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Commands.NET --version 2.0.0-alpha.13
                    
#r "nuget: Commands.NET, 2.0.0-alpha.13"
                    
#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.
#addin nuget:?package=Commands.NET&version=2.0.0-alpha.13&prerelease
                    
Install Commands.NET as a Cake Addin
#tool nuget:?package=Commands.NET&version=2.0.0-alpha.13&prerelease
                    
Install Commands.NET as a Cake Tool

Here goes a banner.

Commands.NET

Consider leaving a ⭐

Build Status Download Discord

Do more, with less. With speed, compatibility and fluent integration in mind.

Commands.NET aims to improve your experience integrating input from different sources* into the same, concurrent pool and treating them as triggered actions, called commands. It provides a modular and intuitive API for registering and executing commands.

Browse the wiki for a full overview of the library.

*Sources can range from command-line, console, chatboxes, to social platforms like Discord, Slack, Messenger & much, much more.

Usage

Running a Command

A command is a method executed when a specific syntax is provided. By creating a manager to contain said command, you can run it with the provided arguments.

using Commands;

var command = Command.From(() => "Hello world!", "greet");

var collection = ComponentCollection.From(command).Create();

await collection.Execute(new ConsoleContext(args));

// dotnet run greet -> Hello world!

Creating Command Groups

Command groups are named collections of commands or other command groups. Groups allow for subcommand creation, where the group name is a category for its children.

using Commands;

var mathCommands = CommandGroup.From("math")
    .AddComponents(
        Command.From((double number, int sumBy)      => number + sumBy, 
            "sum", "add"), 
        Command.From((double number, int subtractBy) => number - subtractBy, 
            "subtract", "sub"), 
        Command.From((double number, int multiplyBy) => number * multiplyBy, 
            "multiply", "mul"), 
        Command.From((double number, int divideBy)   => number / divideBy, 
            "divide", "div")
    );

var collection = ComponentCollection.From(mathCommands).Create();

await collection.Execute(new ConsoleContext(args));

// dotnet run math sum 5 3 -> 8

Creating Command Modules

Command modules are classes that can contain commands or nested command modules, which themselves can also contain (sub)commands.

using Commands;

public class HelpModule : CommandModule 
{
    [Name("help")]
    public void Help()
    {
        var builder = new StringBuilder()
            .AppendLine("Commands:");

        foreach (var command in Manager!.GetCommands())
            builder.AppendLine(command.GetFullName());

        Respond(builder.ToString());
    }
}

...

var collection = ComponentCollection.From(mathCommands).AddType<HelpModule>().Create();

await collection.Execute(new ConsoleContext(args));

// dotnet run help -> Commands: math sum <...> math subtract <...> math ...

Using Dependency Injection

Commands.NET is designed to be compatible with dependency injection out of the box, propagating IServiceProvider throughout the execution flow.

using Commands;
using Microsoft.Extensions.DependencyInjection;

...

var services = new ServiceCollection()
    .AddSingleton<MyService>()
    .AddSingleton<ComponentCollection>(ComponentCollection.From(mathCommands).AddType<HelpModule>().Create());
    .BuildServiceProvider();

var collection = services.GetRequiredService<ComponentCollection>();

await collection.Execute(new ConsoleContext(args), new CommandOptions() { Services = services });

Modules can be injected directly from the provider. They themselves are considered transient, being created and disposed of per command execution.

public class ServicedModule(MyService service) : CommandModule 
{

}

Samples

Benchmarks

Benchmark results are found here.

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 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on Commands.NET:

Package Downloads
Commands.NET.Hosting

A package containing tooling for integrating Commands.NET with Microsoft.Extensions.*.

Commands.NET.Console

An extension for Commands.NET that specifically aims to support the console and CLI, implementing Spectre.Console for beautification.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.0-alpha.19 107 11 days ago
2.0.0-alpha.18 107 12 days ago
2.0.0-alpha.17 42 14 days ago
2.0.0-alpha.16 104 16 days ago
2.0.0-alpha.15 67 a month ago
2.0.0-alpha.14 94 a month ago
2.0.0-alpha.13 129 2 months ago
2.0.0-alpha.12 125 2 months ago
2.0.0-alpha.11 72 4 months ago
2.0.0-alpha.10 65 4 months ago
2.0.0-alpha.9 55 4 months ago
2.0.0-alpha.8 60 4 months ago
2.0.0-alpha.7 52 5 months ago
2.0.0-alpha.6 53 5 months ago
2.0.0-alpha.5 54 5 months ago
2.0.0-alpha.4 54 5 months ago
2.0.0-alpha.3 42 5 months ago
2.0.0-alpha.2 67 5 months ago
2.0.0-alpha.1 54 5 months ago
1.0.0 157 6 months ago
1.0.0-rc6 118 7 months ago
1.0.0-rc5 108 7 months ago
1.0.0-rc4 111 7 months ago
1.0.0-rc3 106 7 months ago
1.0.0-rc2 111 7 months ago
1.0.0-rc1 110 7 months ago
1.0.0-alpha9 109 7 months ago
1.0.0-alpha8 105 8 months ago
1.0.0-alpha7 291 8 months ago
1.0.0-alpha6 129 5/22/2024
1.0.0-alpha5 129 4/14/2024
1.0.0-alpha4 108 2/15/2024
1.0.0-alpha3 103 2/14/2024
1.0.0-alpha2 108 2/10/2024
1.0.0-alpha1 106 2/8/2024
1.0.0-alpha 115 2/6/2024