92 lines
1.9 KiB
C#
92 lines
1.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Runtime.CompilerServices;
|
|
using TermApp.Commands;
|
|
|
|
|
|
|
|
namespace TermApp.Core
|
|
{
|
|
|
|
public class CommandInterpreter
|
|
{
|
|
public readonly Dictionary<string, ICommand> _commands = new();
|
|
|
|
public CommandInterpreter()
|
|
{
|
|
|
|
RegisterCommand(new GreetCommand());
|
|
RegisterCommand(new HelpCommand(this));
|
|
RegisterCommand(new AddCommand());
|
|
RegisterCommand(new EchoCommand());
|
|
RegisterCommand(new ClearCommand());
|
|
|
|
}
|
|
public void RegisterCommand(ICommand command)
|
|
{
|
|
|
|
_commands[command.Name.ToLower()] = command;
|
|
|
|
}
|
|
|
|
public void Execute(string input)
|
|
{
|
|
|
|
string[] pipedCommands = input.Split("|", StringSplitOptions.RemoveEmptyEntries);
|
|
string pipedOutput = null;
|
|
|
|
foreach (var cmdInput in pipedCommands)
|
|
{
|
|
|
|
string trimmed = cmdInput.Trim();
|
|
|
|
string[] parts = input.Split(" ", StringSplitOptions.RemoveEmptyEntries);
|
|
if (parts.Length == 0) continue;
|
|
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");
|
|
return;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(pipedOutput))
|
|
{
|
|
|
|
Console.WriteLine(pipedOutput);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public IEnumerable<ICommand> GetAllCommands()
|
|
{
|
|
|
|
return _commands.Values;
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|