Change CommandResult properties to methods

This commit is contained in:
Stone_Red
2023-04-20 16:45:58 +02:00
parent ce73fdd485
commit 83e7ea7451
4 changed files with 21 additions and 12 deletions
+1 -1
View File
@@ -21,7 +21,7 @@ public class Command
{ {
Identifiers = Array.Empty<string>(); Identifiers = Array.Empty<string>();
HelpText = string.Empty; HelpText = string.Empty;
Method = new Func<string, CommandResult>((_) => CommandResult.Success); Method = new Func<string, CommandResult>((_) => CommandResult.Success());
} }
public Command Register(Action<string> method, params string[] identifiers) public Command Register(Action<string> method, params string[] identifiers)
+15 -4
View File
@@ -5,13 +5,24 @@ public class CommandResult
public ResultType ResultType { get; } public ResultType ResultType { get; }
public string Message { get; } public string Message { get; }
public static CommandResult Success => new CommandResult(ResultType.Success);
public static CommandResult InvalidInput => new CommandResult(ResultType.InvalidInput);
public static CommandResult InternalError => new CommandResult(ResultType.InternalError);
public CommandResult(ResultType type, string message = "") public CommandResult(ResultType type, string message = "")
{ {
ResultType = type; ResultType = type;
Message = message; Message = message;
} }
public static CommandResult Success(string message = "")
{
return new CommandResult(ResultType.Success, message);
}
public static CommandResult InvalidInput(string message = "")
{
return new CommandResult(ResultType.InvalidInput, message);
}
public static CommandResult Error(string message = "")
{
return new CommandResult(ResultType.Error, message);
}
} }
+1 -1
View File
@@ -4,6 +4,6 @@ public enum ResultType
{ {
Success, Success,
InvalidInput, InvalidInput,
InternalError, Error,
UnexpectedError UnexpectedError
} }
+4 -6
View File
@@ -9,14 +9,13 @@ sayCommand.Register(HahaYesSub, "kek");
commander.Register(HahaNo, "scream", "yell"); commander.Register(HahaNo, "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((input) => commander.PrintHelp(input) ? CommandResult.Success() : CommandResult.InvalidInput(), "help");
Command fancyCommand = commander.Register(_ => new CommandResult(ResultType.InvalidInput, "How about no?"), "fancy"); Command fancyCommand = commander.Register(_ => CommandResult.InvalidInput("Nonono"), "fancy");
fancyCommand.Register(new Command() fancyCommand.Register(new Command()
{ {
Identifiers = new string[] { "yes", "YES" }, Identifiers = new string[] { "yes", "YES" },
HelpText = "Says that the input is fancy", HelpText = "Says that the input is fancy",
Method = (i) => { Console.WriteLine($"Yes, {i} is very fancy!"); return new CommandResult(ResultType.Success); } Method = (i) => { Console.WriteLine($"Yes, {i} is very fancy!"); return new CommandResult(ResultType.Success); }
}); });
@@ -37,7 +36,7 @@ while (true)
{ {
ResultType.Success => ConsoleColor.Green, ResultType.Success => ConsoleColor.Green,
ResultType.InvalidInput => ConsoleColor.DarkYellow, ResultType.InvalidInput => ConsoleColor.DarkYellow,
ResultType.InternalError => ConsoleColor.Red, ResultType.Error => ConsoleColor.Red,
ResultType.UnexpectedError => ConsoleColor.DarkRed, ResultType.UnexpectedError => ConsoleColor.DarkRed,
_ => ConsoleColor.Magenta _ => ConsoleColor.Magenta
}; };
@@ -53,7 +52,6 @@ while (true)
Console.ResetColor(); Console.ResetColor();
_ = commander.PrintHelp(); _ = commander.PrintHelp();
} }
Console.WriteLine(); Console.WriteLine();
} }
@@ -88,5 +86,5 @@ CommandResult Add(string input)
return new CommandResult(ResultType.InvalidInput, "Invalid input!"); return new CommandResult(ResultType.InvalidInput, "Invalid input!");
} }
Console.WriteLine(int.Parse(parts[0]) + int.Parse(parts[1])); Console.WriteLine(int.Parse(parts[0]) + int.Parse(parts[1]));
return CommandResult.Success; return CommandResult.Success();
} }