feat: add console command framework
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using Game.Features.Console.Contracts;
|
||||
|
||||
namespace Game.Features.Console.Application.Commands;
|
||||
|
||||
public sealed class ClearCommand : ICommand
|
||||
{
|
||||
public string Name => "clear";
|
||||
public string Description => "Clears the console output.";
|
||||
public IReadOnlyList<CommandArgSpec> Args { get; } = new CommandArgsBuilder().Build();
|
||||
|
||||
public CommandResult Execute(CommandArgValues args) => CommandResult.Ok(clearOutput: true);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://cmmw8kn2fc3bb
|
||||
@@ -0,0 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using Game.Features.Console.Contracts;
|
||||
|
||||
namespace Game.Features.Console.Application.Commands;
|
||||
|
||||
public sealed class EchoCommand : ICommand
|
||||
{
|
||||
public string Name => "echo";
|
||||
public string Description => "Prints the given text back to the console.";
|
||||
public IReadOnlyList<CommandArgSpec> Args { get; } = new CommandArgsBuilder().AddStringArg("text").Build();
|
||||
|
||||
public CommandResult Execute(CommandArgValues args) => CommandResult.Ok(args.GetString(0));
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://cy6dkjf4fxkvj
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Game.Features.Console.Contracts;
|
||||
|
||||
namespace Game.Features.Console.Application.Commands;
|
||||
|
||||
public sealed class HelpCommand(ICommandRegistry registry) : ICommand
|
||||
{
|
||||
public string Name => "help";
|
||||
public string Description => "Lists all available commands.";
|
||||
public IReadOnlyList<CommandArgSpec> Args { get; } = new CommandArgsBuilder().Build();
|
||||
|
||||
private static string FormatUsage(ICommand command)
|
||||
{
|
||||
string args = string.Join(" ", command.Args.Select(spec => $"<{spec.Name}:{spec}>"));
|
||||
return $"{command.Name} {args}";
|
||||
}
|
||||
|
||||
public CommandResult Execute(CommandArgValues args)
|
||||
{
|
||||
IEnumerable<string> lines = registry.All
|
||||
.OrderBy(command => command.Name, StringComparer.Ordinal)
|
||||
.Select(command => command.Args.Count == 0
|
||||
? $"{command.Name} - {command.Description}"
|
||||
: $"{command.Name} - {command.Description}\n usage: {FormatUsage(command)}");
|
||||
|
||||
return CommandResult.Ok($"List of available commands:\n{string.Join("\n", lines)}");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
uid://bo7o0t08bb5u5
|
||||
Reference in New Issue
Block a user