CommandPatternLib 1.0.2
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package CommandPatternLib --version 1.0.2
NuGet\Install-Package CommandPatternLib -Version 1.0.2
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="CommandPatternLib" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CommandPatternLib --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: CommandPatternLib, 1.0.2"
#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 CommandPatternLib as a Cake Addin #addin nuget:?package=CommandPatternLib&version=1.0.2 // Install CommandPatternLib as a Cake Tool #tool nuget:?package=CommandPatternLib&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
CommandApp Library
CommandApp
is a powerful framework for building interactive console applications with command-based architecture. It simplifies command registration, parameter handling, and execution while maintaining flexibility and extensibility.
📦 Installation
Install the package via NuGet:
dotnet add package CommandApp
Getting Started
Step 1: Implement ICommandRepository
CommandApp relies on an ICommandRepository implementation for managing commands. Implement this interface as needed for your application:
csharp
Copy code
public class CustomCommandRepository : ICommandRepository
{
// Implement methods to manage commands, categories, and execution.
}
Step 2: Initialize CommandApp
Create an instance of CommandApp and provide your command repository:
csharp
Copy code
var commandRepository = new CustomCommandRepository();
var commandApp = new CommandApp(commandRepository);
Step 3: Register Commands
Synchronous Commands
csharp
Copy code
commandApp.RegisterCommand("greet", args =>
{
Console.WriteLine($"Hello, {args[0]}!");
}, "Greets the user with their name.", "default");
Asynchronous Commands
csharp
Copy code
commandApp.RegisterCommand("delayed-greet", async args =>
{
await Task.Delay(2000);
Console.WriteLine($"Hello after delay, {args[0]}!");
}, "Greets the user with a delay.", "default");
Commands with Parameters
csharp
Copy code
commandApp.RegisterCommand("sum", args =>
{
int a = int.Parse(args[0]);
int b = int.Parse(args[1]);
Console.WriteLine($"The sum is: {a + b}");
}, "Calculates the sum of two numbers.", "default", "a:int,b:int");
Step 4: Run the Application
Start the application loop to process user input:
csharp
Copy code
await commandApp.RunAsync();
📋 Features
Built-In Commands
Command Description
help or ? Displays available commands and their usage.
clear or cls Clears the console screen.
exit or quit Exits the application.
changecategory Switches to a different command category.
Parameter Definitions
Parameters are defined using a structured format:
Syntax: name:type[options]
type: int, double, bool, string, or regex.
Options:
[optional] – Makes the parameter optional.
... – Indicates a continuous parameter.
Examples:
text
Copy code
username:string
age:int[optional]
data:regex'\d+'
🌟 Advanced Usage
Command Categories
Organize commands into categories for better management:
csharp Copy code
commandApp.CurrentCategory = "inventory";
commandApp.RegisterCommand("list", args =>
{
Console.WriteLine("Listing items...");
}, "Lists items in the inventory.", "inventory");
Dynamic Command Registration
Register commands dynamically based on user input or external data:
csharp
Copy code
commandApp.RegisterCommand("dynamic", args =>
{
Console.WriteLine("This is a dynamically registered command.");
}, "A dynamically registered command.");
🛠 Sample Application
csharp
Copy code
var commandRepository = new CustomCommandRepository();
var commandApp = new CommandApp(commandRepository);
// Register commands
commandApp.RegisterCommand("greet", args =>
{
Console.WriteLine($"Hello, {args[0]}!");
}, "Greets the user with their name.");
commandApp.RegisterCommand("sum", args =>
{
int a = int.Parse(args[0]);
int b = int.Parse(args[1]);
Console.WriteLine($"Sum: {a + b}");
}, "Calculates the sum of two numbers.", "default", "a:int,b:int");
// Run the app
await commandApp.RunAsync();
❓ FAQ
1. How do I register a command without parameters?
csharp
Copy code
commandApp.RegisterCommand("simple", args =>
{
Console.WriteLine("This command requires no parameters.");
}, "A command with no parameters.");
2. Can I override built-in commands?
Yes, register a new command with the same name:
csharp
Copy code
commandApp.RegisterCommand("clear", args =>
{
Console.WriteLine("Custom clear command logic.");
}, "Overrides the built-in clear command.");
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETFramework 4.7.2
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
First Release, implements basic of CommandAPP and Redo Undo pattern .