Change namespace and improve PrintHelp method

This commit is contained in:
Stone_Red
2025-03-03 18:34:53 +01:00
parent 751bbbb1ee
commit 3511fa9b06
7 changed files with 80 additions and 20 deletions
+41 -7
View File
@@ -1,13 +1,13 @@
using System.Text; using System.Text;
namespace Commander_Net; namespace Commander;
public class Command public class Command
{ {
internal readonly List<Command> subCommands = new(); internal readonly List<Command> subCommands = new();
public string[] Identifiers { get; init; } public string[] Identifiers { get; set; }
public Func<string, CommandResult> Method { get; init; } public Func<string, CommandResult> Method { get; set; }
public HelpText HelpText { get; init; } public HelpText HelpText { get; set; }
public Command() public Command()
{ {
@@ -57,6 +57,24 @@ public class Command
return command; return command;
} }
public Command WithDescription(string description)
{
HelpText = description;
return this;
}
public Command WithIdentifiers(params string[] identifiers)
{
Identifiers = identifiers;
return this;
}
public Command WithMethod(Func<string, CommandResult> method)
{
Method = method;
return this;
}
internal List<CommandResult> ExecuteMultible(string input, List<CommandResult>? commandResults = null) internal List<CommandResult> ExecuteMultible(string input, List<CommandResult>? commandResults = null)
{ {
string[] inputParts = input.Split(' '); string[] inputParts = input.Split(' ');
@@ -109,15 +127,31 @@ public class Command
} }
} }
internal string GetHelp(string input, int indentation = 2) internal string GetHelp(string input, int indentation = 2, int paddingSize = 0, bool includeDescription = true)
{ {
string[] inputParts = input.Split(' '); string[] inputParts = input.Split(' ');
StringBuilder helpText = new StringBuilder($"{string.Join(" | ", Identifiers)} > {HelpText?.Description}"); if (paddingSize - indentation < 0)
{
paddingSize = indentation;
}
StringBuilder helpText = new StringBuilder(string.Join(" | ", Identifiers).PadRight(paddingSize - indentation));
if (!string.IsNullOrWhiteSpace(HelpText.Description) && includeDescription)
{
_ = helpText.Append($" > {HelpText.Description}");
}
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}{new string(' ', indentation)}{command.GetHelp(string.Join(' ', inputParts.Skip(1)), indentation + 2)}"); _ = helpText.Append($"{Environment.NewLine}{new string(' ', indentation)}{command.GetHelp(string.Join(' ', inputParts.Skip(1)), indentation + 2, paddingSize, includeDescription)}");
}
// Indentation is 2 when this is called from the Commander class/not a subcommand
if (subCommands.Count > 0 && indentation == 2)
{
_ = helpText.AppendLine();
} }
return helpText.ToString(); return helpText.ToString();
+1 -1
View File
@@ -1,4 +1,4 @@
namespace Commander_Net; namespace Commander;
public class CommandResult public class CommandResult
{ {
+1 -1
View File
@@ -1,4 +1,4 @@
namespace Commander_Net; namespace Commander;
public enum ResultType public enum ResultType
{ {
+1 -1
View File
@@ -8,7 +8,7 @@
<PackageReadmeFile>README.md</PackageReadmeFile> <PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageId>Console.$(AssemblyName)</PackageId> <PackageId>Console.$(AssemblyName)</PackageId>
<RootNamespace>Commander_Net</RootNamespace> <RootNamespace>Commander</RootNamespace>
<RepositoryUrl>https://github.com/Stone-Red-Code/Commander.NET</RepositoryUrl> <RepositoryUrl>https://github.com/Stone-Red-Code/Commander.NET</RepositoryUrl>
<PackageTags>CLI, Console, Commands</PackageTags> <PackageTags>CLI, Console, Commands</PackageTags>
<Product>$(AssemblyName)</Product> <Product>$(AssemblyName)</Product>
+19 -3
View File
@@ -1,11 +1,16 @@
using System.Text; using System.Text;
namespace Commander_Net; namespace Commander;
public class Commander public class Commander
{ {
private readonly List<Command> commands = new(); private readonly List<Command> commands = new();
public Command Register()
{
return Register(new Command());
}
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);
@@ -40,7 +45,7 @@ public class Commander
return command; return command;
} }
public bool ExecuteMultible(string input, out List<CommandResult> commandResults) public bool ExecuteMultiple(string input, out List<CommandResult> commandResults)
{ {
List<CommandResult> internalCommandResults = new List<CommandResult>(); List<CommandResult> internalCommandResults = new List<CommandResult>();
string[] inputParts = input.Split(' '); string[] inputParts = input.Split(' ');
@@ -80,10 +85,21 @@ public class Commander
foreach (Command command in commands.Where(command => command.Identifiers.Contains(inputParts[0]) || string.IsNullOrWhiteSpace(input))) foreach (Command command in commands.Where(command => command.Identifiers.Contains(inputParts[0]) || string.IsNullOrWhiteSpace(input)))
{ {
_ = internalHelpText.AppendLine(command.GetHelp(string.Join(' ', inputParts.Skip(1)))); _ = internalHelpText.AppendLine(command.GetHelp(string.Join(' ', inputParts.Skip(1)), includeDescription: false));
} }
helpText = internalHelpText.ToString(); helpText = internalHelpText.ToString();
int longestLine = helpText.Split(Environment.NewLine).Max(line => line.Length) + 2;
_ = internalHelpText.Clear();
foreach (Command command in commands.Where(command => command.Identifiers.Contains(inputParts[0]) || string.IsNullOrWhiteSpace(input)))
{
_ = internalHelpText.AppendLine(command.GetHelp(string.Join(' ', inputParts.Skip(1)), paddingSize: longestLine));
}
helpText = internalHelpText.ToString();
return !string.IsNullOrWhiteSpace(helpText); return !string.IsNullOrWhiteSpace(helpText);
} }
+1 -1
View File
@@ -1,4 +1,4 @@
namespace Commander_Net; namespace Commander;
public class HelpText public class HelpText
{ {
+16 -6
View File
@@ -1,18 +1,21 @@
using Commander_Net; using Commander;
Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!");
Commander commander = new Commander(); Commander.Commander commander = new Commander.Commander();
Command sayCommand = commander.Register(HahaYes, "say", "yell"); Command sayCommand = commander.Register(HahaYes, "say", "yell");
Command kekCommand = sayCommand.Register(HahaYesSub, "kek"); Command kekCommand = sayCommand.Register(HahaYesSub, "kek");
kekCommand.Register(HahaYesSub, "lol"); kekCommand.Register(HahaYesSub, "lol");
commander.Register(HahaNo, "scream", "yell"); commander.Register(HahaNo, (HelpText)"AAAAAH", "scream", "yell");
commander.Register(Add, "add"); commander.Register(Add, "add");
commander.Register((args) => Console.Clear(), "clear"); commander.Register((args) => Console.Clear(), "clear");
commander.Register((input) => commander.PrintHelp(input) ? CommandResult.Success() : CommandResult.InvalidInput(), "help"); commander.Register()
.WithMethod((input) => commander.PrintHelp(input) ? CommandResult.Success() : CommandResult.InvalidInput())
.WithDescription("Cool help command")
.WithIdentifiers("help", "?");
Command fancyCommand = commander.Register(_ => CommandResult.InvalidInput("Nonono"), "fancy"); Command fancyCommand = commander.Register(_ => CommandResult.InvalidInput("Nonono"), (HelpText)"Very fancy commands", "fancy");
fancyCommand.Register(new Command() fancyCommand.Register(new Command()
{ {
Identifiers = new string[] { "yes", "YES" }, Identifiers = new string[] { "yes", "YES" },
@@ -27,9 +30,16 @@ fancyCommand.Register(new Command()
Method = (i) => { Console.WriteLine($"Yes, {i} is not very fancy!"); return new CommandResult(ResultType.Success); } Method = (i) => { Console.WriteLine($"Yes, {i} is not very fancy!"); return new CommandResult(ResultType.Success); }
}); });
fancyCommand.Register(new Command()
{
Identifiers = new string[] { "no", "NO" },
HelpText = "Says that the input is not fancy",
Method = (i) => { Console.WriteLine($"Yes, {i} is not very fancy!"); return new CommandResult(ResultType.Success); }
});
while (true) while (true)
{ {
if (commander.ExecuteMultible(Console.ReadLine()!, out List<CommandResult> commandResults)) if (commander.ExecuteMultiple(Console.ReadLine()!, out List<CommandResult> commandResults))
{ {
foreach (CommandResult commandResult in commandResults) foreach (CommandResult commandResult in commandResults)
{ {