Fix help text indentation

This commit is contained in:
Stone_Red
2023-12-14 00:03:09 +01:00
parent 83e7ea7451
commit 751bbbb1ee
3 changed files with 14 additions and 14 deletions
+11 -12
View File
@@ -4,19 +4,11 @@ namespace Commander_Net;
public class Command public class Command
{ {
internal readonly List<Command> subCommands = new();
public string[] Identifiers { get; init; } public string[] Identifiers { get; init; }
public Func<string, CommandResult> Method { get; init; } public Func<string, CommandResult> Method { get; init; }
public HelpText HelpText { get; init; } public HelpText HelpText { get; init; }
internal readonly List<Command> subCommands = new();
internal Command(Func<string, CommandResult> method, HelpText description, string[] identifiers)
{
Method = method;
HelpText = description;
Identifiers = identifiers;
}
public Command() public Command()
{ {
Identifiers = Array.Empty<string>(); Identifiers = Array.Empty<string>();
@@ -24,6 +16,13 @@ public class Command
Method = new Func<string, CommandResult>((_) => CommandResult.Success()); Method = new Func<string, CommandResult>((_) => CommandResult.Success());
} }
internal Command(Func<string, CommandResult> method, HelpText description, string[] identifiers)
{
Method = method;
HelpText = description;
Identifiers = identifiers;
}
public Command Register(Action<string> method, params string[] identifiers) public Command Register(Action<string> method, params string[] identifiers)
{ {
return Register(method, new HelpText(), identifiers); return Register(method, new HelpText(), identifiers);
@@ -93,7 +92,7 @@ public class Command
{ {
string[] inputParts = input.Split(' '); string[] inputParts = input.Split(' ');
Command? command = subCommands.FirstOrDefault(command => command.Identifiers.Contains(inputParts[0])); Command? command = subCommands.Find(command => command.Identifiers.Contains(inputParts[0]));
if (command is not null) if (command is not null)
{ {
@@ -110,7 +109,7 @@ public class Command
} }
} }
internal string GetHelp(string input) internal string GetHelp(string input, int indentation = 2)
{ {
string[] inputParts = input.Split(' '); string[] inputParts = input.Split(' ');
@@ -118,7 +117,7 @@ public class Command
foreach (Command command in subCommands.Where(command => command.Identifiers.Contains(inputParts[0]) || string.IsNullOrWhiteSpace(input))) foreach (Command command in subCommands.Where(command => command.Identifiers.Contains(inputParts[0]) || string.IsNullOrWhiteSpace(input)))
{ {
_ = helpText.Append($"{Environment.NewLine} {command.GetHelp(string.Join(' ', inputParts.Skip(1)))}"); _ = helpText.Append($"{Environment.NewLine}{new string(' ', indentation)}{command.GetHelp(string.Join(' ', inputParts.Skip(1)), indentation + 2)}");
} }
return helpText.ToString(); return helpText.ToString();
+1 -1
View File
@@ -60,7 +60,7 @@ public class Commander
{ {
string[] inputParts = input.Split(' '); string[] inputParts = input.Split(' ');
Command? command = commands.FirstOrDefault(command => command.Identifiers.Contains(inputParts[0])); Command? command = commands.Find(command => command.Identifiers.Contains(inputParts[0]));
if (command is null) if (command is null)
{ {
+2 -1
View File
@@ -4,7 +4,8 @@ Console.WriteLine("Hello, World!");
Commander commander = new Commander(); Commander commander = new Commander();
Command sayCommand = commander.Register(HahaYes, "say", "yell"); Command sayCommand = commander.Register(HahaYes, "say", "yell");
sayCommand.Register(HahaYesSub, "kek"); Command kekCommand = sayCommand.Register(HahaYesSub, "kek");
kekCommand.Register(HahaYesSub, "lol");
commander.Register(HahaNo, "scream", "yell"); commander.Register(HahaNo, "scream", "yell");
commander.Register(Add, "add"); commander.Register(Add, "add");