mirror of
https://github.com/Stone-Red-Code/Commander.NET.git
synced 2026-09-04 08:56:09 +02:00
Change CommandResult properties to methods
This commit is contained in:
@@ -21,7 +21,7 @@ public class Command
|
||||
{
|
||||
Identifiers = Array.Empty<string>();
|
||||
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)
|
||||
|
||||
@@ -5,13 +5,24 @@ public class CommandResult
|
||||
public ResultType ResultType { 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 = "")
|
||||
{
|
||||
ResultType = type;
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,6 @@ public enum ResultType
|
||||
{
|
||||
Success,
|
||||
InvalidInput,
|
||||
InternalError,
|
||||
Error,
|
||||
UnexpectedError
|
||||
}
|
||||
@@ -9,14 +9,13 @@ 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");
|
||||
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()
|
||||
{
|
||||
Identifiers = new string[] { "yes", "YES" },
|
||||
HelpText = "Says that the input is fancy",
|
||||
|
||||
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.InvalidInput => ConsoleColor.DarkYellow,
|
||||
ResultType.InternalError => ConsoleColor.Red,
|
||||
ResultType.Error => ConsoleColor.Red,
|
||||
ResultType.UnexpectedError => ConsoleColor.DarkRed,
|
||||
_ => ConsoleColor.Magenta
|
||||
};
|
||||
@@ -53,7 +52,6 @@ while (true)
|
||||
Console.ResetColor();
|
||||
|
||||
_ = commander.PrintHelp();
|
||||
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
@@ -88,5 +86,5 @@ CommandResult Add(string input)
|
||||
return new CommandResult(ResultType.InvalidInput, "Invalid input!");
|
||||
}
|
||||
Console.WriteLine(int.Parse(parts[0]) + int.Parse(parts[1]));
|
||||
return CommandResult.Success;
|
||||
return CommandResult.Success();
|
||||
}
|
||||
Reference in New Issue
Block a user