Browse Source

Added ParameterType

tags/docs-0.9
RogueException 9 years ago
parent
commit
db556c358d
3 changed files with 30 additions and 19 deletions
  1. +16
    -7
      src/Discord.Net.Commands/Command.cs
  2. +13
    -11
      src/Discord.Net.Commands/CommandBuilder.cs
  3. +1
    -1
      src/Discord.Net.Commands/CommandsPlugin.cs

+ 16
- 7
src/Discord.Net.Commands/Command.cs 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 bool IsCatchAll { get; }
public ParameterType Type { get; }


public CommandParameter(string name, bool isOptional, bool isCatchAll)
public CommandParameter(string name, ParameterType type)
{ {
Name = name; Name = name;
IsOptional = isOptional;
IsCatchAll = isCatchAll;
Type = type;
} }
} }


@@ -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;


+ 13
- 11
src/Discord.Net.Commands/CommandBuilder.cs 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)
throw new Exception("No parameters may be added after the catch-all");
if (_hasOptional && !isOptional)
throw new Exception("Non-optional parameters may not be added after an optional one");
if (_isClosed)
throw new Exception($"No parameters may be added after a {nameof(ParameterType.Multiple)} or {nameof(ParameterType.Unparsed)} parameter.");
if (!_allowRequired && type != ParameterType.Required)
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)
_hasOptional = true;
if (isCatchAll)
_hasCatchAll = true;
if (type == ParameterType.Optional)
_allowRequired = false;
if (type == ParameterType.Multiple || type == ParameterType.Unparsed)
_isClosed = true;
return this; return this;
} }
public CommandBuilder IsHidden() public CommandBuilder IsHidden()


+ 1
- 1
src/Discord.Net.Commands/CommandsPlugin.cs 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 =>


Loading…
Cancel
Save