Commands.NET.Hosting 2.0.0-alpha.17

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

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

Documentation

Browse the wiki for a full overview of the library.

Installation

Commands.NET is available on NuGet. You can install it using the package manager, or the following command:

dotnet add package Commands.NET
dotnet add package Commands.NET.Hosting

Alternatively, adding it to your .csproj file:

<PackageReference Include="Commands.NET" Version="x.x.x" />
<PackageReference Include="Commands.NET.Hosting" Version="x.x.x" />

The hosting package is optional.

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 components = new ComponentTree() 
{
    new Command(() => "Hello world!", "greet");
};

var provider = new ComponentProvider(components);

await provider.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 mathGroup = new ComponentGroup("math")
{
    new Command((double number, int sumBy)      => number + sumBy, 
        "sum", "add"), 
    new Command((double number, int subtractBy) => number - subtractBy, 
        "subtract", "sub"), 
    new Command((double number, int multiplyBy) => number * multiplyBy, 
        "multiply", "mul"), 
    new Command((double number, int divideBy)   => number / divideBy, 
        "divide", "div")
};

var provider = new ComponentProvider();

provider.Components.Add(mathGroup);

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 provider = new ComponentProvider();

provider.Components.Add<HelpModule>();
provider.Components.Add(mathGroup);

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

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

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<ComponentProvider>(new ComponentProvider());
    .BuildServiceProvider();

var provider = services.GetRequiredService<ComponentProvider>();

provider.Components.Add<HelpModule>();
provider.Components.Add(mathGroup);

await provider.Execute(new ConsoleContext(args), new ExecutionOptions() { 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 
{

}

.NET Generic Host

Alongside dependency injection support in the base package, Commands.NET provides an extension package for the .NET Generic Host, allowing you to integrate Commands.NET into your application with ease.

using Commands.Hosting;
using Microsoft.Extensions.Hosting;

var host = Host.CreateDefaultBuilder(args)
    .ConfigureComponents(configure => ...)
    .Build();

The extension package supports factory-based command execution alongside scope management, allowing you to manage the lifetime of your commands and modules.

Samples

Benchmarks

Benchmark results are found here.

Product Compatible and additional computed target framework versions.
.NET 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
2.0.0-alpha.19 107 5/27/2025
2.0.0-alpha.18 110 5/26/2025
2.0.0-alpha.17 43 5/23/2025
2.0.0-alpha.16 109 5/22/2025
2.0.0-alpha.15 66 4/25/2025
2.0.0-alpha.14 97 4/25/2025
2.0.0-alpha.13 123 4/24/2025
1.0.0 222 12/2/2024