Added simple, modular command parsing
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using YourProject.Commands;
|
||||
|
||||
namespace YourProject.Commands
|
||||
{
|
||||
public class ClearCommand : Command
|
||||
{
|
||||
public override string Name => "clear";
|
||||
public override string Description => "Clears all text from the console output";
|
||||
public override string Usage => "clear";
|
||||
|
||||
public override void Execute(string[] args, Action<string> outputCallback)
|
||||
{
|
||||
// Send a special marker that the console UI will recognize
|
||||
outputCallback("CLEAR_CONSOLE");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://clober5sjosj3
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace YourProject.Commands // Add namespace
|
||||
{
|
||||
public abstract class Command
|
||||
{
|
||||
public abstract string Name { get; }
|
||||
public abstract string Description { get; }
|
||||
public abstract string Usage { get; }
|
||||
public abstract void Execute(string[] args, Action<string> outputCallback);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://d20rwntnb4hf7
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using YourProject.Commands;
|
||||
|
||||
namespace YourProject.Commands
|
||||
{
|
||||
public static class ConsoleManager
|
||||
{
|
||||
private static Dictionary<string, Command> commands = new Dictionary<string, Command>(StringComparer.OrdinalIgnoreCase);
|
||||
|
||||
static ConsoleManager()
|
||||
{
|
||||
// Register all commands
|
||||
RegisterCommand(new HelpCommand());
|
||||
RegisterCommand(new ClearCommand());
|
||||
}
|
||||
|
||||
public static void RegisterCommand(Command command)
|
||||
{
|
||||
commands[command.Name] = command;
|
||||
}
|
||||
|
||||
public static Command GetCommand(string name)
|
||||
{
|
||||
return commands.ContainsKey(name) ? commands[name] : null;
|
||||
}
|
||||
|
||||
public static IEnumerable<Command> GetAllCommands()
|
||||
{
|
||||
return commands.Values;
|
||||
}
|
||||
|
||||
public static bool Execute(string input, Action<string> outputCallback)
|
||||
{
|
||||
string[] parts = input.Trim().Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts.Length == 0) return false;
|
||||
|
||||
string commandName = parts[0];
|
||||
string[] args = parts.Skip(1).ToArray();
|
||||
|
||||
if (commands.ContainsKey(commandName))
|
||||
{
|
||||
try
|
||||
{
|
||||
commands[commandName].Execute(args, outputCallback);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
outputCallback($"Error: {e.Message}");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://bwl1i4sf5eysn
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using YourProject.Commands; // Reference the namespace
|
||||
|
||||
namespace YourProject.Commands
|
||||
{
|
||||
public class HelpCommand : Command
|
||||
{
|
||||
public override string Name => "help";
|
||||
public override string Description => "Shows available commands";
|
||||
public override string Usage => "help [command]";
|
||||
|
||||
public override void Execute(string[] args, Action<string> outputCallback)
|
||||
{
|
||||
if (args.Length > 0)
|
||||
{
|
||||
var command = ConsoleManager.GetCommand(args[0]);
|
||||
if (command != null)
|
||||
{
|
||||
outputCallback($"{command.Name} - {command.Description}");
|
||||
outputCallback($"Usage: {command.Usage}");
|
||||
}
|
||||
else
|
||||
{
|
||||
outputCallback($"Unknown command: {args[0]}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
outputCallback("Available commands:");
|
||||
foreach (var cmd in ConsoleManager.GetAllCommands())
|
||||
{
|
||||
outputCallback($" {cmd.Name,-10} - {cmd.Description}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://hungsjp7fpvo
|
||||
@@ -0,0 +1 @@
|
||||
uid://t8o6quogxufd
|
||||
Reference in New Issue
Block a user