GG EZ, ima go piss
This commit is contained in:
54
TermEngine/CommandInterpreter.cs
Normal file
54
TermEngine/CommandInterpreter.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user