Code improvements

This commit is contained in:
Stone_Red
2023-04-20 13:13:05 +02:00
parent aad3ad502c
commit ce73fdd485
3 changed files with 49 additions and 18 deletions
+9 -9
View File
@@ -36,11 +36,11 @@ public class Command
public Command Register(Action<string> method, HelpText description, params string[] identifiers) public Command Register(Action<string> method, HelpText description, params string[] identifiers)
{ {
Func<string, CommandResult> commandMethod = (input) => CommandResult commandMethod(string input)
{ {
method(input); method(input);
return new CommandResult(ResultType.Success); return new CommandResult(ResultType.Success);
}; }
return Register(commandMethod, description, identifiers); return Register(commandMethod, description, identifiers);
} }
@@ -48,7 +48,7 @@ public class Command
public Command Register(Func<string, CommandResult> method, HelpText description, params string[] identifiers) public Command Register(Func<string, CommandResult> method, HelpText description, params string[] identifiers)
{ {
Command command = new Command(method, description, identifiers); Command command = new Command(method, description, identifiers);
Register(command); _ = Register(command);
return command; return command;
} }
@@ -67,7 +67,7 @@ public class Command
foreach (Command? command in subCommands.Where(command => command.Identifiers.Contains(inputParts[0]))) 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; found = true;
} }
@@ -78,9 +78,9 @@ public class Command
{ {
result = Method(input); result = Method(input);
} }
catch (Exception) catch (Exception ex)
{ {
result = new CommandResult(ResultType.UnexpectedError); result = new CommandResult(ResultType.UnexpectedError, ex.ToString());
} }
commandResults.Add(result); commandResults.Add(result);
@@ -104,9 +104,9 @@ public class Command
{ {
return Method(input); 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))) 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(); return helpText.ToString();
+5 -5
View File
@@ -18,11 +18,11 @@ public class Commander
public Command Register(Action<string> method, HelpText description, params string[] identifiers) public Command Register(Action<string> method, HelpText description, params string[] identifiers)
{ {
Func<string, CommandResult> commandMethod = (input) => CommandResult commandMethod(string input)
{ {
method(input); method(input);
return new CommandResult(ResultType.Success); return new CommandResult(ResultType.Success);
}; }
return Register(commandMethod, description, identifiers); return Register(commandMethod, description, identifiers);
} }
@@ -30,7 +30,7 @@ public class Commander
public Command Register(Func<string, CommandResult> method, HelpText description, params string[] identifiers) public Command Register(Func<string, CommandResult> method, HelpText description, params string[] identifiers)
{ {
Command command = new Command(method, description, identifiers); Command command = new Command(method, description, identifiers);
Register(command); _ = Register(command);
return 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))) 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(); helpText = internalHelpText.ToString();
return !string.IsNullOrWhiteSpace(helpText); return !string.IsNullOrWhiteSpace(helpText);
} }
public bool PrintHelp(string input) public bool PrintHelp(string input = "")
{ {
bool result = GetHelp(input, out string helpText); bool result = GetHelp(input, out string helpText);
Console.WriteLine(helpText.TrimEnd('\n', '\r')); Console.WriteLine(helpText.TrimEnd('\n', '\r'));
+35 -4
View File
@@ -7,6 +7,7 @@ Command sayCommand = commander.Register(HahaYes, "say", "yell");
sayCommand.Register(HahaYesSub, "kek"); sayCommand.Register(HahaYesSub, "kek");
commander.Register(HahaNo, "scream", "yell"); commander.Register(HahaNo, "scream", "yell");
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((input) => commander.PrintHelp(input) ? CommandResult.Success : CommandResult.InvalidInput, "help");
@@ -28,14 +29,33 @@ fancyCommand.Register(new Command()
while (true) while (true)
{ {
if (commander.ExecuteMultible(Console.ReadLine()!, out List<CommandResult> commandResult)) if (commander.ExecuteMultible(Console.ReadLine()!, out List<CommandResult> 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 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) void HahaYes(string yes)
@@ -52,10 +72,21 @@ CommandResult HahaNo(string no)
{ {
if (string.IsNullOrWhiteSpace(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()}"); Console.WriteLine($">>> {no.ToUpper()}");
return new CommandResult(ResultType.Success); 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;
} }