Commands.NET 2.0.0-alpha.3

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.3                
NuGet\Install-Package Commands.NET -Version 2.0.0-alpha.3                
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.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Commands.NET --version 2.0.0-alpha.3                
#r "nuget: Commands.NET, 2.0.0-alpha.3"                
#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.
// Install Commands.NET as a Cake Addin
#addin nuget:?package=Commands.NET&version=2.0.0-alpha.3&prerelease

// Install Commands.NET as a Cake Tool
#tool nuget:?package=Commands.NET&version=2.0.0-alpha.3&prerelease                

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.Create(() => "Hello world!", "greet");

var manager = ComponentManager.Create(command);

manager.TryExecute(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.Create("math");

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

var manager = ComponentManager.Create(mathCommands);

manager.TryExecute(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 helpCommands = CommandGroup.Create<HelpModule>();

var manager = ComponentManager.Create(mathCommands, helpCommands);

manager.TryExecute(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<ComponentManager>(ComponentManager.Create(mathCommands, helpCommands);
    .BuildServiceProvider();

var manager = services.GetRequiredService<ComponentManager>();

manager.TryExecute(new ConsoleContext(), args, new CommandOptions() { Services = services });

Modules can be injected directly from the provider. They themselves are considered transient services;

public class ServicedModule(MyService service) : CommandModule 
{
}

Samples

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. 
.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 (1)

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

Package Downloads
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.5 27 1/11/2025
2.0.0-alpha.4 7 1/9/2025
2.0.0-alpha.3 8 1/9/2025
2.0.0-alpha.2 32 1/7/2025
2.0.0-alpha.1 23 1/7/2025
1.0.0 136 12/2/2024
1.0.0-rc6 109 11/21/2024
1.0.0-rc5 98 11/19/2024
1.0.0-rc4 103 11/16/2024
1.0.0-rc3 97 11/16/2024
1.0.0-rc2 102 11/15/2024
1.0.0-rc1 99 11/15/2024
1.0.0-alpha9 101 11/2/2024
1.0.0-alpha8 97 10/21/2024
1.0.0-alpha7 199 10/16/2024
1.0.0-alpha6 123 5/22/2024
1.0.0-alpha5 126 4/14/2024
1.0.0-alpha4 105 2/15/2024
1.0.0-alpha3 100 2/14/2024
1.0.0-alpha2 105 2/10/2024
1.0.0-alpha1 104 2/8/2024
1.0.0-alpha 113 2/6/2024