54 lines
1.1 KiB
C#
54 lines
1.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
public class CommandInterpreter
|
|
{
|
|
public readonly Dictionary<string, ICommand> _commands = new();
|
|
|
|
public CommandInterpreter()
|
|
{
|
|
|
|
RegisterCommand(new GreetCommand());
|
|
RegisterCommand(new HelpCommand(this));
|
|
|
|
}
|
|
public void RegisterCommand(ICommand command)
|
|
{
|
|
|
|
_commands[command.Name.ToLower()] = command;
|
|
|
|
}
|
|
|
|
public void Execute(string input)
|
|
{
|
|
string[] parts = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);
|
|
if (parts.Length == 0) return;
|
|
|
|
string commandName = parts[0].ToLower();
|
|
string[] args = parts.Length > 1 ? parts[1..] : new string[0];
|
|
|
|
if (_commands.TryGetValue(commandName, out var command))
|
|
{
|
|
|
|
command.Execute(args);
|
|
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("Err - uknown Command : '{commandName}'. Type help For a list of commands");
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public IEnumerable<ICommand> GetAllCommands()
|
|
{
|
|
|
|
return _commands.Values;
|
|
|
|
}
|
|
|
|
|
|
} |