Added ParameterType

This commit is contained in:
RogueException
2015-10-30 09:55:08 -03:00
parent 91cfab1b5d
commit db556c358d
3 changed files with 30 additions and 19 deletions

View File

@@ -4,17 +4,26 @@ using System.Threading.Tasks;
namespace Discord.Commands namespace Discord.Commands
{ {
public enum ParameterType
{
/// <summary> Catches a single required parameter. </summary>
Required,
/// <summary> Catches a single optional parameter. </summary>
Optional,
/// <summary> Catches a zero or more optional parameters. </summary>
Multiple,
/// <summary> Catches all remaining text as a single optional parameter. </summary>
Unparsed
}
public sealed class CommandParameter public sealed class CommandParameter
{ {
public string Name { get; } public string Name { get; }
public bool IsOptional { get; } public ParameterType Type { get; }
public bool IsCatchAll { get; }
public CommandParameter(string name, bool isOptional, bool isCatchAll) public CommandParameter(string name, ParameterType type)
{ {
Name = name; Name = name;
IsOptional = isOptional; Type = type;
IsCatchAll = isCatchAll;
} }
} }
@@ -60,7 +69,7 @@ namespace Discord.Commands
} }
else else
{ {
if (parameters[parameters.Length - 1].IsCatchAll) if (parameters[parameters.Length - 1].Type == ParameterType.Multiple)
MaxArgs = null; MaxArgs = null;
else else
MaxArgs = parameters.Length; MaxArgs = parameters.Length;
@@ -68,7 +77,7 @@ namespace Discord.Commands
int? optionalStart = null; int? optionalStart = null;
for (int i = parameters.Length - 1; i >= 0; i--) for (int i = parameters.Length - 1; i >= 0; i--)
{ {
if (parameters[i].IsOptional) if (parameters[i].Type == ParameterType.Optional)
optionalStart = i; optionalStart = i;
else else
break; break;

View File

@@ -10,7 +10,7 @@ namespace Discord.Commands
private readonly CommandsPlugin _plugin; private readonly CommandsPlugin _plugin;
private readonly Command _command; private readonly Command _command;
private List<CommandParameter> _params; private List<CommandParameter> _params;
private bool _hasOptional, _hasCatchAll; private bool _allowRequired, _isClosed;
private string _prefix; private string _prefix;
public CommandBuilder(CommandsPlugin plugin, Command command, string prefix) public CommandBuilder(CommandsPlugin plugin, Command command, string prefix)
@@ -19,6 +19,8 @@ namespace Discord.Commands
_command = command; _command = command;
_params = new List<CommandParameter>(); _params = new List<CommandParameter>();
_prefix = prefix; _prefix = prefix;
_allowRequired = true;
_isClosed = false;
} }
public CommandBuilder Alias(params string[] aliases) public CommandBuilder Alias(params string[] aliases)
@@ -32,19 +34,19 @@ namespace Discord.Commands
_command.Description = description; _command.Description = description;
return this; return this;
} }
public CommandBuilder Parameter(string name, bool isOptional = false, bool isCatchAll = false) public CommandBuilder Parameter(string name, ParameterType type = ParameterType.Required)
{ {
if (_hasCatchAll) if (_isClosed)
throw new Exception("No parameters may be added after the catch-all"); throw new Exception($"No parameters may be added after a {nameof(ParameterType.Multiple)} or {nameof(ParameterType.Unparsed)} parameter.");
if (_hasOptional && !isOptional) if (!_allowRequired && type != ParameterType.Required)
throw new Exception("Non-optional parameters may not be added after an optional one"); throw new Exception($"{nameof(ParameterType.Required)} parameters may not be added after an optional one");
_params.Add(new CommandParameter(name, isOptional, isCatchAll)); _params.Add(new CommandParameter(name, type));
if (isOptional) if (type == ParameterType.Optional)
_hasOptional = true; _allowRequired = false;
if (isCatchAll) if (type == ParameterType.Multiple || type == ParameterType.Unparsed)
_hasCatchAll = true; _isClosed = true;
return this; return this;
} }
public CommandBuilder IsHidden() public CommandBuilder IsHidden()

View File

@@ -41,7 +41,7 @@ namespace Discord.Commands
if (builtInHelp) if (builtInHelp)
{ {
CreateCommand("help") CreateCommand("help")
.Parameter("command", isOptional: true) .Parameter("command", ParameterType.Optional)
.IsHidden() .IsHidden()
.Info("Returns information about commands.") .Info("Returns information about commands.")
.Do(async e => .Do(async e =>