CommandAppInterface 1.4.0

dotnet add package CommandAppInterface --version 1.4.0
                    
NuGet\Install-Package CommandAppInterface -Version 1.4.0
                    
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="CommandAppInterface" Version="1.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CommandAppInterface" Version="1.4.0" />
                    
Directory.Packages.props
<PackageReference Include="CommandAppInterface" />
                    
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 CommandAppInterface --version 1.4.0
                    
#r "nuget: CommandAppInterface, 1.4.0"
                    
#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.
#:package CommandAppInterface@1.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CommandAppInterface&version=1.4.0
                    
Install as a Cake Addin
#tool nuget:?package=CommandAppInterface&version=1.4.0
                    
Install as a Cake Tool

NuGet: https://www.nuget.org/packages/CommandAppInterface/

Command App Interface

This is a library for easy command interface making (actualy I used something like this in my other projects, but I tired of always typing if/else):<br> изображение (you can see this example in CAIExamples folder)

Usage:

First you need to use CAI namespace and create CAI instance:

AppInterface interface = new(caiName: "example", isCatchExceptions: true);

If you want to handle exceptions yourself, set isCatchExceptions false. Next to create a command, use AppInterface.AddCommand method:

interface.AddCommand(new Command("command_name", "this is a command without parameters", DoSomeDealMethod, "this is text about how to use this command);

interface.AddCommand<int>(new Command("command_with_int_parameter", "this command expects an int parameter after command name", MethodWithIntArgument, "usage: \"command_with_int_parameter 42\""));

Now commands support up to 6 various parameters. Each parameters implements IParsable.<br> In the command constructor, the command handler method is assigned. It must have the same number of parameters and the same types as those specified in the command generics<br>

CAI supports commands with variable parameters count:

interface.AddVariableArgsCommand<double>("command_with_many_double_parameters". "this command expects any count of doubles", DoSomeDealWithDoubleArray, "usage: \"command_with_many_double_parameters num1 num2 ... numN\"")

To start interface, run AppInterface.Start() method:

interface.Start();

The whole code looks like this:

...
AppInterface interface = new(caiName: "example", isCatchExceptions: true);

interface.AddCommand(new Command("command_name", "this is a command without parameters", DoSomeDealMethod, "this is text about how to use this command);
interface.AddCommand<int>(new Command("command_with_int_parameter", "this command expects an int parameter after command name", MethodWithIntArgument, "usage: \"command_with_int_parameter 42\""));
interface.AddVariableArgsCommand<double>("command_with_many_double_parameters". "this command expects any count of doubles", DoSomeDealWithDoubleArray, "usage: \"command_with_many_double_parameters num1 num2 ... numN\"")

interface.Start();
...

Also CAI contains built-in commands: изображение

You can run one CAI inside other. In this case when you type quit, you will exit from internal CAI and continue working in external CAI: изображение

Logging

CAI can log your string message, or any Spectre.Console.Rendering.IRenderable:<br> изображение

Styles

CAI supports several styles:

  • None -- no welcome message and no command prompt
  • WriteCommandPrompt -- only command prompt
  • WelcomeMessage -- only welcome message
  • WelcomeAndCommandPrompt -- both command prompt and welcome message
  • CopyrightMessage -- Spectre.Console copyright
  • CAINAme -- show CAI name at start
  • Full -- standard CAI style. Contains everything this way you can make clear interface without welcome message or blue command prompt:
            AppInterface clearInterface = new("clear interface", isCatchExceptions: true, style: InterfaceStyles.None);

"style" parameter isn`t required, it is set to Styles.Full by default. Styles are presented enum with Flags attribute, so you can actually use WelcomeMessage | WriteCommandPromt instead of WelcomeAndCommandPrompt1.

Native AOT:

CAI namespace itself supports Native AOT, also CAI uses Spectre.Console (Not Spectre.Console.Cli!!!), as stated in https://spectreconsole.net/best-practices, Spectre.Console supports Native AOT. But child namespaces doesn't have to support it.

Reflection:

WARNING: THIS NAMESPACE DOESN'T GUARANTEE NATIVE AOT SUPPORT!<br> using namespace CAI.Reflection, you can add commands using attributes (see CAI.Examples.ReflectionExample):<br>

    [Command("reflection", "echo", "Echoes the input text", "echo [text]")]
    public static void Echo(string value)
    {
        AnsiConsole.WriteLine($"{value}");
    }

    [Command("reflection", "bar", "say bar", "bar")]
    public static void Bar()
    {
        AnsiConsole.WriteLine("bar");
    }

By default, app interface doesn't load that commands, you have to call extension method AppInterface.LoadAttributedCommands()

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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
1.4.0 561 7/23/2025
1.3.1 131 4/19/2025
1.3.0 138 4/12/2025
1.2.0 119 3/14/2025
1.1.0 248 3/6/2025
1.0.1 235 3/3/2025
1.0.0 131 3/2/2025