From ce73fdd485d7e603a029bcace22adff9a05caba1 Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Thu, 20 Apr 2023 13:13:05 +0200 Subject: [PATCH] Code improvements --- Commander.NET/Command.cs | 18 +++++++++--------- Commander.NET/Commander.cs | 10 +++++----- Commander.Test/Program.cs | 39 ++++++++++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/Commander.NET/Command.cs b/Commander.NET/Command.cs index a60daf7..1a31187 100644 --- a/Commander.NET/Command.cs +++ b/Commander.NET/Command.cs @@ -36,11 +36,11 @@ public class Command public Command Register(Action method, HelpText description, params string[] identifiers) { - Func commandMethod = (input) => + CommandResult commandMethod(string input) { method(input); return new CommandResult(ResultType.Success); - }; + } return Register(commandMethod, description, identifiers); } @@ -48,7 +48,7 @@ public class Command public Command Register(Func method, HelpText description, params string[] identifiers) { Command command = new Command(method, description, identifiers); - Register(command); + _ = Register(command); return command; } @@ -67,7 +67,7 @@ public class Command foreach (Command? command in subCommands.Where(command => command.Identifiers.Contains(inputParts[0]))) { - command.ExecuteMultible(string.Join(' ', inputParts.Skip(1)), commandResults); + _ = command.ExecuteMultible(string.Join(' ', inputParts.Skip(1)), commandResults); found = true; } @@ -78,9 +78,9 @@ public class Command { result = Method(input); } - catch (Exception) + catch (Exception ex) { - result = new CommandResult(ResultType.UnexpectedError); + result = new CommandResult(ResultType.UnexpectedError, ex.ToString()); } commandResults.Add(result); @@ -104,9 +104,9 @@ public class Command { return Method(input); } - catch (Exception) + catch (Exception ex) { - return new CommandResult(ResultType.UnexpectedError); + return new CommandResult(ResultType.UnexpectedError, ex.ToString()); } } @@ -118,7 +118,7 @@ public class Command 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} {command.GetHelp(string.Join(' ', inputParts.Skip(1)))}"); } return helpText.ToString(); diff --git a/Commander.NET/Commander.cs b/Commander.NET/Commander.cs index 8ae735f..950f13d 100644 --- a/Commander.NET/Commander.cs +++ b/Commander.NET/Commander.cs @@ -18,11 +18,11 @@ public class Commander public Command Register(Action method, HelpText description, params string[] identifiers) { - Func commandMethod = (input) => + CommandResult commandMethod(string input) { method(input); return new CommandResult(ResultType.Success); - }; + } return Register(commandMethod, description, identifiers); } @@ -30,7 +30,7 @@ public class Commander public Command Register(Func method, HelpText description, params string[] identifiers) { Command command = new Command(method, description, identifiers); - Register(command); + _ = Register(command); return command; } @@ -80,14 +80,14 @@ 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)))); } helpText = internalHelpText.ToString(); return !string.IsNullOrWhiteSpace(helpText); } - public bool PrintHelp(string input) + public bool PrintHelp(string input = "") { bool result = GetHelp(input, out string helpText); Console.WriteLine(helpText.TrimEnd('\n', '\r')); diff --git a/Commander.Test/Program.cs b/Commander.Test/Program.cs index 7d85d3d..a951448 100644 --- a/Commander.Test/Program.cs +++ b/Commander.Test/Program.cs @@ -7,6 +7,7 @@ Command sayCommand = commander.Register(HahaYes, "say", "yell"); sayCommand.Register(HahaYesSub, "kek"); commander.Register(HahaNo, "scream", "yell"); +commander.Register(Add, "add"); commander.Register((args) => Console.Clear(), "clear"); commander.Register((input) => commander.PrintHelp(input) ? CommandResult.Success : CommandResult.InvalidInput, "help"); @@ -28,14 +29,33 @@ fancyCommand.Register(new Command() while (true) { - if (commander.ExecuteMultible(Console.ReadLine()!, out List commandResult)) + if (commander.ExecuteMultible(Console.ReadLine()!, out List commandResults)) { - commandResult.ForEach((e) => Console.WriteLine(e.ResultType + " " + e.Message)); + foreach (CommandResult commandResult in commandResults) + { + Console.ForegroundColor = commandResult.ResultType switch + { + ResultType.Success => ConsoleColor.Green, + ResultType.InvalidInput => ConsoleColor.DarkYellow, + ResultType.InternalError => ConsoleColor.Red, + ResultType.UnexpectedError => ConsoleColor.DarkRed, + _ => ConsoleColor.Magenta + }; + + Console.WriteLine($"[{commandResult.ResultType}] {commandResult.Message}"); + Console.ResetColor(); + } } else { - Console.WriteLine("xxx Command not found!"); + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("Command not found!"); + Console.ResetColor(); + + _ = commander.PrintHelp(); + } + Console.WriteLine(); } void HahaYes(string yes) @@ -52,10 +72,21 @@ CommandResult HahaNo(string no) { if (string.IsNullOrWhiteSpace(no)) { - return new CommandResult(ResultType.InvalidInput, "fuck off"); + return new CommandResult(ResultType.InvalidInput, "No the command doesn't work like that!"); } Console.WriteLine($">>> {no.ToUpper()}"); return new CommandResult(ResultType.Success); +} + +CommandResult Add(string input) +{ + string[] parts = input.Split(' '); + if (parts.Length != 2) + { + return new CommandResult(ResultType.InvalidInput, "Invalid input!"); + } + Console.WriteLine(int.Parse(parts[0]) + int.Parse(parts[1])); + return CommandResult.Success; } \ No newline at end of file