From 463c6baf499851cb29ad9eeeeecccac5ff0b9e84 Mon Sep 17 00:00:00 2001 From: D1GQ Date: Thu, 18 Jun 2026 22:04:08 -0500 Subject: [PATCH] Add generic BaseArgument --- src/Commands/Arguments/BaseArgument.cs | 30 ++++++++++++++++++++---- src/Commands/Arguments/BoolArgument.cs | 6 ++--- src/Commands/Arguments/PlayerArgument.cs | 6 ++--- src/Commands/Arguments/StringArgument.cs | 11 ++++----- src/Commands/KickCommand.cs | 4 ++-- src/Commands/PlayerInfoCommand.cs | 2 +- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/Commands/Arguments/BaseArgument.cs b/src/Commands/Arguments/BaseArgument.cs index 94f5b46..4af05b4 100644 --- a/src/Commands/Arguments/BaseArgument.cs +++ b/src/Commands/Arguments/BaseArgument.cs @@ -1,37 +1,41 @@ namespace BetterAmongUs.Commands.Arguments; /// -/// Abstract base class for command arguments in BetterAmongUs. +/// Abstract base class for command arguments. /// /// The command this argument belongs to. -/// Information about the argument. +/// Information about the argument, such as its name, description, or format. internal abstract class BaseArgument(BaseCommand command, string argInfo) { /// /// Gets the command this argument belongs to. /// + /// The parent instance that owns this argument. internal BaseCommand Command { get; } = command; /// /// Gets information about the argument. /// + /// A string containing metadata about the argument, such as its name, description, or expected format. internal string ArgInfo { get; } = argInfo; /// - /// Gets or sets the current argument value. + /// Gets or sets the current argument value as a string. /// + /// The raw string value of the argument. Defaults to an empty string. internal string Arg { get; set; } = string.Empty; /// /// Gets the argument suggestions for auto-completion. /// + /// An array of suggestion strings for the current argument. protected virtual string[] GetArgSuggestions() { return []; } /// - /// Resets the Argument back to its default state. + /// Resets the argument back to its default state. /// internal void Reset() { @@ -41,7 +45,23 @@ internal abstract class BaseArgument(BaseCommand command, string argInfo) /// /// Gets the closest suggestion for the current argument value. /// - /// The closest matching suggestion, or empty string if none found. + /// The closest matching suggestion, or an empty string if none found. internal string GetClosestSuggestion() => GetArgSuggestions().FirstOrDefault(name => name.StartsWith(Arg, StringComparison.OrdinalIgnoreCase)) ?? string.Empty; +} + +/// +/// Generic abstract base class for typed command arguments. +/// +/// The target type that this argument will be parsed into. +/// The command this argument belongs to. +/// Information about the argument, such as its name, description, or format. +internal abstract class BaseArgument(BaseCommand command, string argInfo) : BaseArgument(command, argInfo) +{ + /// + /// Attempts to parse the current argument value to type . + /// + /// When this method returns, contains the parsed value if parsing succeeded; otherwise, the default value for . + /// true if the argument was successfully parsed; otherwise, false. + internal abstract bool TryParse(out T result); } \ No newline at end of file diff --git a/src/Commands/Arguments/BoolArgument.cs b/src/Commands/Arguments/BoolArgument.cs index bbd7137..24a7f82 100644 --- a/src/Commands/Arguments/BoolArgument.cs +++ b/src/Commands/Arguments/BoolArgument.cs @@ -5,7 +5,7 @@ /// /// The command this argument belongs to. /// Information about the argument (default: "{bool}"). -internal sealed class BoolArgument(BaseCommand command, string argInfo = "{bool}") : BaseArgument(command, argInfo) +internal sealed class BoolArgument(BaseCommand command, string argInfo = "{bool}") : BaseArgument(command, argInfo) { protected override string[] GetArgSuggestions() { @@ -17,7 +17,7 @@ internal sealed class BoolArgument(BaseCommand command, string argInfo = "{bool} /// /// /// - internal bool TryParseBool(out bool result) + internal override bool TryParse(out bool result) { if (Arg.ToLower() == "true") { @@ -32,7 +32,7 @@ internal sealed class BoolArgument(BaseCommand command, string argInfo = "{bool} else { BaseCommand.CommandErrorText($"Invalid Syntax!"); - result = false; + result = default; return false; } } diff --git a/src/Commands/Arguments/PlayerArgument.cs b/src/Commands/Arguments/PlayerArgument.cs index df03ef0..62d7f68 100644 --- a/src/Commands/Arguments/PlayerArgument.cs +++ b/src/Commands/Arguments/PlayerArgument.cs @@ -7,7 +7,7 @@ namespace BetterAmongUs.Commands.Arguments; /// /// The command this argument belongs to. /// Information about the argument (default: "{player}"). -internal sealed class PlayerArgument(BaseCommand command, string argInfo = "{player}") : BaseArgument(command, argInfo) +internal sealed class PlayerArgument(BaseCommand command, string argInfo = "{player}") : BaseArgument(command, argInfo) { protected override string[] GetArgSuggestions() { @@ -38,7 +38,7 @@ internal sealed class PlayerArgument(BaseCommand command, string argInfo = "{pla /// /// /// - internal bool TryParsePlayer(out PlayerControl? result) + internal override bool TryParse(out PlayerControl result) { foreach (var player in BAUPlugin.AllPlayerControls) { @@ -51,7 +51,7 @@ internal sealed class PlayerArgument(BaseCommand command, string argInfo = "{pla } } - result = null; + result = default!; BaseCommand.CommandErrorText("Player not found!"); return false; } diff --git a/src/Commands/Arguments/StringArgument.cs b/src/Commands/Arguments/StringArgument.cs index 153a564..fc8180f 100644 --- a/src/Commands/Arguments/StringArgument.cs +++ b/src/Commands/Arguments/StringArgument.cs @@ -5,7 +5,7 @@ /// /// The command this argument belongs to. /// Information about the argument (default: "{String}"). -internal sealed class StringArgument(BaseCommand command, string argInfo = "{String}") : BaseArgument(command, argInfo) +internal sealed class StringArgument(BaseCommand command, string argInfo = "{String}") : BaseArgument(command, argInfo) { protected override string[] GetArgSuggestions() { @@ -17,12 +17,9 @@ internal sealed class StringArgument(BaseCommand command, string argInfo = "{Str /// internal Func ArgSuggestions { get; set; } = () => { return []; }; - /// - /// Gets the string argument. - /// - /// - internal string GetString() + internal override bool TryParse(out string result) { - return Arg; + result = Arg; + return true; } } \ No newline at end of file diff --git a/src/Commands/KickCommand.cs b/src/Commands/KickCommand.cs index 6f6674e..937f7c7 100644 --- a/src/Commands/KickCommand.cs +++ b/src/Commands/KickCommand.cs @@ -31,10 +31,10 @@ internal sealed class KickCommand : BaseCommand internal override void Run() { - if (!_playerArgument.TryParsePlayer(out var player)) + if (!_playerArgument.TryParse(out var player)) return; - var isBan = _boolArgument.TryParseBool(out var ban); + var isBan = _boolArgument.TryParse(out var ban); if (!isBan) return; diff --git a/src/Commands/PlayerInfoCommand.cs b/src/Commands/PlayerInfoCommand.cs index e0511cd..b4b1caf 100644 --- a/src/Commands/PlayerInfoCommand.cs +++ b/src/Commands/PlayerInfoCommand.cs @@ -20,7 +20,7 @@ internal sealed class PlayerInfoCommand : BaseCommand internal override void Run() { - if (!_playerArgument.TryParsePlayer(out var player)) + if (!_playerArgument.TryParse(out var player)) return; if (player.Data == null)