initial commit
This commit is contained in:
31
Commands/CommandDispatcher.cs
Normal file
31
Commands/CommandDispatcher.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Spectre.Console;
|
||||
namespace AnchorCli.Commands;
|
||||
|
||||
public class CommandDispatcher
|
||||
{
|
||||
private readonly CommandRegistry _registry;
|
||||
|
||||
public CommandDispatcher(CommandRegistry registry)
|
||||
{
|
||||
_registry = registry;
|
||||
}
|
||||
|
||||
public async Task<bool> TryExecuteAsync(string input, CancellationToken ct)
|
||||
{
|
||||
if (!input.StartsWith('/')) return false;
|
||||
|
||||
var parts = input[1..].Split(' ', 2);
|
||||
var commandName = parts[0];
|
||||
var args = parts.Length > 1 ? new[] { parts[1] } : Array.Empty<string>();
|
||||
|
||||
var command = _registry.GetCommand(commandName);
|
||||
if (command == null)
|
||||
{
|
||||
AnsiConsole.MarkupLine($"[red]Unknown command: /{commandName}[/]");
|
||||
return true;
|
||||
}
|
||||
|
||||
await command.ExecuteAsync(args, ct);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user