|
1 | | -# command-bridge |
| 1 | +# CommandBridge |
| 2 | + |
| 3 | +[](https://github.com/g4-api/command-bridge/actions/workflows/GithubActions.yml) |
| 4 | + |
| 5 | +CommandBridge is a versatile and powerful command-line utility framework for .NET, designed to simplify the creation, management, and execution of command-line commands and their associated parameters. This framework provides a structured and extensible way to handle complex command-line interfaces with ease. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Simple and intuitive API for defining commands and parameters. |
| 10 | +- Automatic handling of global options like `help`. |
| 11 | +- Extensible base class for creating custom commands. |
| 12 | +- Comprehensive error handling and validation. |
| 13 | +- Easy integration with existing .NET applications. |
| 14 | +- Supports short and long forms of command parameters. |
| 15 | +- Seamless `--help` or `-h` switches to display command help. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +CommandBridge is available as a NuGet package. You can install it using the NuGet Package Manager or the .NET CLI. |
| 20 | + |
| 21 | +### NuGet Package Manager |
| 22 | + |
| 23 | +```sh |
| 24 | +Install-Package CommandBridge |
| 25 | +``` |
| 26 | + |
| 27 | +### .NET CLI |
| 28 | + |
| 29 | +```sh |
| 30 | +dotnet add package CommandBridge |
| 31 | +``` |
| 32 | + |
| 33 | +## Quick Start |
| 34 | + |
| 35 | +Here's a quick example to get you started with CommandBridge. |
| 36 | + |
| 37 | +1. Create a new console application: |
| 38 | + |
| 39 | +```sh |
| 40 | +dotnet new console -n CommandBridgeExample |
| 41 | +cd CommandBridgeExample |
| 42 | +``` |
| 43 | + |
| 44 | +2. Install the CommandBridge NuGet package: |
| 45 | + |
| 46 | +```sh |
| 47 | +dotnet add package CommandBridge |
| 48 | +``` |
| 49 | + |
| 50 | +3. Define a custom command by inheriting from `CommandBase`: |
| 51 | + |
| 52 | +```csharp |
| 53 | +using System; |
| 54 | +using System.Collections.Generic; |
| 55 | +using CommandBridge; |
| 56 | + |
| 57 | +namespace CommandBridgeExample |
| 58 | +{ |
| 59 | + [Command(name: "greet", description: "Greets the user with a message.")] |
| 60 | + public class GreetCommand : CommandBase |
| 61 | + { |
| 62 | + private static readonly Dictionary<string, IDictionary<string, CommandData>> s_commands = new() |
| 63 | + { |
| 64 | + ["greet"] = new Dictionary<string, CommandData>(StringComparer.Ordinal) |
| 65 | + { |
| 66 | + { "n", new() { Name = "name", Description = "The name of the user.", Mandatory = true } } |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + public GreetCommand() : base(s_commands) { } |
| 71 | + |
| 72 | + protected override void OnInvoke(Dictionary<string, string> parameters) |
| 73 | + { |
| 74 | + var name = parameters["name"]; |
| 75 | + Console.WriteLine($"Hello, {name}!"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + class Program |
| 80 | + { |
| 81 | + static void Main(string[] args) |
| 82 | + { |
| 83 | + var command = CommandBase.FindCommand(args); |
| 84 | + command?.Invoke(args); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +4. Run your application: |
| 91 | + |
| 92 | +```sh |
| 93 | +dotnet run greet -n John |
| 94 | +``` |
| 95 | + |
| 96 | +You should see the output: |
| 97 | + |
| 98 | +``` |
| 99 | +Hello, John! |
| 100 | +``` |
| 101 | + |
| 102 | +## Tutorial |
| 103 | + |
| 104 | +### Step 1: Define Your Commands |
| 105 | + |
| 106 | +To create a new command, you need to create a class that inherits from `CommandBase` and use the `Command` attribute to specify the command name and description. The commands dictionary supports both short and long forms of the command parameters. |
| 107 | + |
| 108 | +```csharp |
| 109 | +using CommandBridge; |
| 110 | + |
| 111 | +[Command(name: "mycommand", description: "This is my custom command.")] |
| 112 | +public class MyCommand : CommandBase |
| 113 | +{ |
| 114 | + private static readonly Dictionary<string, IDictionary<string, CommandData>> s_commands = new() |
| 115 | + { |
| 116 | + ["mycommand"] = new Dictionary<string, CommandData>(StringComparer.Ordinal) |
| 117 | + { |
| 118 | + { "p1", new() { Name = "Parameter1", Description = "Description for parameter 1.", Mandatory = true } }, |
| 119 | + { "p2", new() { Name = "Parameter2", Description = "Description for parameter 2.", Mandatory = false } } |
| 120 | + } |
| 121 | + }; |
| 122 | + |
| 123 | + public MyCommand() : base(s_commands) { } |
| 124 | + |
| 125 | + protected override void OnInvoke(Dictionary<string, string> parameters) |
| 126 | + { |
| 127 | + // Implement your command logic here |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +### Step 2: Implement Command Logic |
| 133 | + |
| 134 | +Override the `OnInvoke` method to define the behavior of your command. |
| 135 | + |
| 136 | +```csharp |
| 137 | +protected override void OnInvoke(Dictionary<string, string> parameters) |
| 138 | +{ |
| 139 | + var param1 = parameters["Parameter1"]; |
| 140 | + var param2 = parameters.ContainsKey("Parameter2") ? parameters["Parameter2"] : "default value"; |
| 141 | + |
| 142 | + Console.WriteLine($"Parameter 1: {param1}"); |
| 143 | + Console.WriteLine($"Parameter 2: {param2}"); |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +### Step 3: Execute Commands |
| 148 | + |
| 149 | +In your `Main` method, use the `FindCommand` method to locate and execute the appropriate command based on the provided arguments. |
| 150 | + |
| 151 | +```csharp |
| 152 | +class Program |
| 153 | +{ |
| 154 | + static void Main(string[] args) |
| 155 | + { |
| 156 | + var command = CommandBase.FindCommand(args); |
| 157 | + command?.Invoke(args); |
| 158 | + } |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +### Step 4: Display Help Information |
| 163 | + |
| 164 | +CommandBridge automatically handles global options like `help`. You can trigger it by passing `--help` or `-h` as an argument. |
| 165 | + |
| 166 | +```sh |
| 167 | +dotnet run mycommand --help |
| 168 | +``` |
| 169 | + |
| 170 | +## Basic Implementation |
| 171 | + |
| 172 | +Here's a complete example demonstrating the creation and usage of a simple command with both short and long forms of parameters. |
| 173 | + |
| 174 | +```csharp |
| 175 | +using System; |
| 176 | +using System.Collections.Generic; |
| 177 | +using CommandBridge; |
| 178 | + |
| 179 | +namespace CommandBridgeExample |
| 180 | +{ |
| 181 | + [Command(name: "deploy", description: "Deploys an application to the specified environment.")] |
| 182 | + public class DeployCommand : CommandBase |
| 183 | + { |
| 184 | + private static readonly Dictionary<string, IDictionary<string, CommandData>> s_commands = new() |
| 185 | + { |
| 186 | + ["deploy"] = new Dictionary<string, CommandData>(StringComparer.Ordinal) |
| 187 | + { |
| 188 | + { "e", new() { Name = "env", Description = "Specifies the target environment (e.g., production, staging).", Mandatory = true } }, |
| 189 | + { "v", new() { Name = "version", Description = "Specifies the version of the application to deploy.", Mandatory = true } }, |
| 190 | + { "c", new() { Name = "config", Description = "Path to the configuration file." } }, |
| 191 | + { "f", new() { Name = "force", Description = "Force deploy without confirmation.", Type = "Switch" } } |
| 192 | + } |
| 193 | + }; |
| 194 | + |
| 195 | + public DeployCommand() : base(s_commands) { } |
| 196 | + |
| 197 | + protected override void OnInvoke(Dictionary<string, string> parameters) |
| 198 | + { |
| 199 | + var env = parameters["env"]; |
| 200 | + var version = parameters["version"]; |
| 201 | + var config = parameters.ContainsKey("config") ? parameters["config"] : "default-config.yml"; |
| 202 | + var force = parameters.ContainsKey("force"); |
| 203 | + |
| 204 | + Console.WriteLine($"Deploying version {version} to {env} environment."); |
| 205 | + Console.WriteLine($"Using configuration file: {config}"); |
| 206 | + if (force) |
| 207 | + { |
| 208 | + Console.WriteLine("Force deploy enabled."); |
| 209 | + } |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + class Program |
| 214 | + { |
| 215 | + static void Main(string[] args) |
| 216 | + { |
| 217 | + var command = CommandBase.FindCommand(args); |
| 218 | + command?.Invoke(args); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | +``` |
| 223 | + |
| 224 | +To run the example: |
| 225 | + |
| 226 | +```sh |
| 227 | +dotnet run deploy -e production -v 1.0.0 -c config.yml -f |
| 228 | +``` |
| 229 | + |
| 230 | +You should see the output: |
| 231 | + |
| 232 | +``` |
| 233 | +Deploying version 1.0.0 to production environment. |
| 234 | +Using configuration file: config.yml |
| 235 | +Force deploy enabled. |
| 236 | +``` |
| 237 | + |
| 238 | +## Contributing |
| 239 | + |
| 240 | +We welcome contributions to CommandBridge! If you find a bug or have a feature request, please open an issue on our GitHub repository. If you'd like to contribute code, feel free to fork the repository and submit a pull request. |
| 241 | + |
| 242 | +## License |
| 243 | + |
| 244 | +CommandBridge is licensed under the MIT License. See the [LICENSE](LICENSE) file for more information. |
| 245 | + |
| 246 | +--- |
| 247 | + |
| 248 | +CommandBridge is designed to make building command-line interfaces in .NET simple and efficient. We hope you find it useful and look forward to your feedback and contributions. |
0 commit comments