Commands.NET
2.0.0-alpha.14
See the version list below for details.
dotnet add package Commands.NET --version 2.0.0-alpha.14
NuGet\Install-Package Commands.NET -Version 2.0.0-alpha.14
<PackageReference Include="Commands.NET" Version="2.0.0-alpha.14" />
<PackageVersion Include="Commands.NET" Version="2.0.0-alpha.14" />
<PackageReference Include="Commands.NET" />
paket add Commands.NET --version 2.0.0-alpha.14
#r "nuget: Commands.NET, 2.0.0-alpha.14"
#addin nuget:?package=Commands.NET&version=2.0.0-alpha.14&prerelease
#tool nuget:?package=Commands.NET&version=2.0.0-alpha.14&prerelease
Commands.NET
Consider leaving a ⭐
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 command = Command.From(() => "Hello world!", "greet");
var collection = ComponentCollection.From(command).Create();
await collection.Execute(new ConsoleCallerContext(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 ConsoleCallerContext(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 ConsoleCallerContext(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<ComponentCollection>(ComponentCollection.From(mathCommands).AddType<HelpModule>().Create());
.BuildServiceProvider();
var collection = services.GetRequiredService<ComponentCollection>();
await collection.Execute(new ConsoleCallerContext(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
{
}
.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)
.ConfigureServices(configure => configure.AddHostedService<CommandListener>())
.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.
using Commands.Hosting;
using Microsoft.Extensions.Hosting;
public class CommandListener(IExecutionFactory factory) : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
var context = new ConsoleCallerContext(Console.ReadLine());
await factory.StartExecution(context);
}
}
}
Samples
- Commands.Samples.Core
- Manage, create and execute commands in a basic console application.
- Commands.Samples.Console
- Fluent API's, complex execution flow and workflow expansion.
- Commands.Samples.Hosting
- Integrating Commands.NET into the .NET Generic Host infrastructure.
- Commands.Samples.FSharp
- Use Commands.NET in F# projects.
Benchmarks
Benchmark results are found here.
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 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. |
-
.NETStandard 2.0
- System.Memory (>= 4.6.0)
- System.Threading.Tasks.Extensions (>= 4.6.0)
-
net8.0
- No dependencies.
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.15 | 64 | 4/25/2025 |
2.0.0-alpha.14 | 91 | 4/25/2025 |
2.0.0-alpha.13 | 126 | 4/22/2025 |
2.0.0-alpha.12 | 122 | 4/22/2025 |
2.0.0-alpha.11 | 70 | 2/5/2025 |
2.0.0-alpha.10 | 62 | 1/30/2025 |
2.0.0-alpha.9 | 52 | 1/27/2025 |
2.0.0-alpha.8 | 58 | 1/27/2025 |
2.0.0-alpha.7 | 50 | 1/22/2025 |
2.0.0-alpha.6 | 51 | 1/16/2025 |
2.0.0-alpha.5 | 52 | 1/11/2025 |
2.0.0-alpha.4 | 52 | 1/9/2025 |
2.0.0-alpha.3 | 36 | 1/9/2025 |
2.0.0-alpha.2 | 65 | 1/7/2025 |
2.0.0-alpha.1 | 52 | 1/7/2025 |
1.0.0 | 153 | 12/2/2024 |
1.0.0-rc6 | 116 | 11/21/2024 |
1.0.0-rc5 | 106 | 11/19/2024 |
1.0.0-rc4 | 109 | 11/16/2024 |
1.0.0-rc3 | 104 | 11/16/2024 |
1.0.0-rc2 | 107 | 11/15/2024 |
1.0.0-rc1 | 106 | 11/15/2024 |
1.0.0-alpha9 | 107 | 11/2/2024 |
1.0.0-alpha8 | 102 | 10/21/2024 |
1.0.0-alpha7 | 279 | 10/16/2024 |
1.0.0-alpha6 | 127 | 5/22/2024 |
1.0.0-alpha5 | 127 | 4/14/2024 |
1.0.0-alpha4 | 106 | 2/15/2024 |
1.0.0-alpha3 | 101 | 2/14/2024 |
1.0.0-alpha2 | 106 | 2/10/2024 |
1.0.0-alpha1 | 104 | 2/8/2024 |
1.0.0-alpha | 113 | 2/6/2024 |