diff --git a/Commander.NET/Command.cs b/Commander.NET/Command.cs index 4ecdf09..aa4aca9 100644 --- a/Commander.NET/Command.cs +++ b/Commander.NET/Command.cs @@ -1,13 +1,13 @@ using System.Text; -namespace Commander_Net; +namespace Commander; public class Command { internal readonly List subCommands = new(); - public string[] Identifiers { get; init; } - public Func Method { get; init; } - public HelpText HelpText { get; init; } + public string[] Identifiers { get; set; } + public Func Method { get; set; } + public HelpText HelpText { get; set; } public Command() { @@ -57,6 +57,24 @@ public class 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 method) + { + Method = method; + return this; + } + internal List ExecuteMultible(string input, List? commandResults = null) { 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(' '); - 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))) { - _ = 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(); diff --git a/Commander.NET/CommandResult.cs b/Commander.NET/CommandResult.cs index a6fa843..ac07ad9 100644 --- a/Commander.NET/CommandResult.cs +++ b/Commander.NET/CommandResult.cs @@ -1,4 +1,4 @@ -namespace Commander_Net; +namespace Commander; public class CommandResult { diff --git a/Commander.NET/CommandResults.cs b/Commander.NET/CommandResults.cs index 4ad33fb..5f982c4 100644 --- a/Commander.NET/CommandResults.cs +++ b/Commander.NET/CommandResults.cs @@ -1,4 +1,4 @@ -namespace Commander_Net; +namespace Commander; public enum ResultType { diff --git a/Commander.NET/Commander.NET.csproj b/Commander.NET/Commander.NET.csproj index da42ded..af75738 100644 --- a/Commander.NET/Commander.NET.csproj +++ b/Commander.NET/Commander.NET.csproj @@ -8,7 +8,7 @@ README.md LICENSE Console.$(AssemblyName) - Commander_Net + Commander https://github.com/Stone-Red-Code/Commander.NET CLI, Console, Commands $(AssemblyName) diff --git a/Commander.NET/Commander.cs b/Commander.NET/Commander.cs index 3ad7eff..a1ca4c6 100644 --- a/Commander.NET/Commander.cs +++ b/Commander.NET/Commander.cs @@ -1,11 +1,16 @@ using System.Text; -namespace Commander_Net; +namespace Commander; public class Commander { private readonly List commands = new(); + public Command Register() + { + return Register(new Command()); + } + public Command Register(Action method, params string[] identifiers) { return Register(method, new HelpText(), identifiers); @@ -40,7 +45,7 @@ public class Commander return command; } - public bool ExecuteMultible(string input, out List commandResults) + public bool ExecuteMultiple(string input, out List commandResults) { List internalCommandResults = new List(); 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))) { - _ = internalHelpText.AppendLine(command.GetHelp(string.Join(' ', inputParts.Skip(1)))); + _ = internalHelpText.AppendLine(command.GetHelp(string.Join(' ', inputParts.Skip(1)), includeDescription: false)); } 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); } diff --git a/Commander.NET/HelpText.cs b/Commander.NET/HelpText.cs index e75587c..3c3c6b3 100644 --- a/Commander.NET/HelpText.cs +++ b/Commander.NET/HelpText.cs @@ -1,4 +1,4 @@ -namespace Commander_Net; +namespace Commander; public class HelpText { diff --git a/Commander.Test/Program.cs b/Commander.Test/Program.cs index 454ae17..67a17d1 100644 --- a/Commander.Test/Program.cs +++ b/Commander.Test/Program.cs @@ -1,18 +1,21 @@ -using Commander_Net; +using Commander; Console.WriteLine("Hello, World!"); -Commander commander = new Commander(); +Commander.Commander commander = new Commander.Commander(); Command sayCommand = commander.Register(HahaYes, "say", "yell"); Command kekCommand = sayCommand.Register(HahaYesSub, "kek"); kekCommand.Register(HahaYesSub, "lol"); -commander.Register(HahaNo, "scream", "yell"); +commander.Register(HahaNo, (HelpText)"AAAAAH", "scream", "yell"); commander.Register(Add, "add"); 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() { 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); } }); +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) { - if (commander.ExecuteMultible(Console.ReadLine()!, out List commandResults)) + if (commander.ExecuteMultiple(Console.ReadLine()!, out List commandResults)) { foreach (CommandResult commandResult in commandResults) {