CommandAppInterface 1.4.0
dotnet add package CommandAppInterface --version 1.4.0
NuGet\Install-Package CommandAppInterface -Version 1.4.0
<PackageReference Include="CommandAppInterface" Version="1.4.0" />
<PackageVersion Include="CommandAppInterface" Version="1.4.0" />
<PackageReference Include="CommandAppInterface" />
paket add CommandAppInterface --version 1.4.0
#r "nuget: CommandAppInterface, 1.4.0"
#:package CommandAppInterface@1.4.0
#addin nuget:?package=CommandAppInterface&version=1.4.0
#tool nuget:?package=CommandAppInterface&version=1.4.0
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 | Versions 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. |
-
net9.0
- Spectre.Console (>= 0.49.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.