Browse Source

Ability to ignore unused parameters instead of failing the command. (#915)

* Addition of FailOnTooManyArgs

* Correct name & only pass in bool
tags/2.0.0-beta
vim2meta Christopher F 7 years ago
parent
commit
5f46aef3a7
4 changed files with 15 additions and 4 deletions
  1. +7
    -2
      src/Discord.Net.Commands/CommandParser.cs
  2. +2
    -1
      src/Discord.Net.Commands/CommandService.cs
  3. +3
    -0
      src/Discord.Net.Commands/CommandServiceConfig.cs
  4. +3
    -1
      src/Discord.Net.Commands/Info/CommandInfo.cs

+ 7
- 2
src/Discord.Net.Commands/CommandParser.cs View File

@@ -14,7 +14,7 @@ namespace Discord.Commands
QuotedParameter
}
public static async Task<ParseResult> ParseArgsAsync(CommandInfo command, ICommandContext context, IServiceProvider services, string input, int startPos)
public static async Task<ParseResult> ParseArgsAsync(CommandInfo command, ICommandContext context, bool ignoreExtraArgs, IServiceProvider services, string input, int startPos)
{
ParameterInfo curParam = null;
StringBuilder argBuilder = new StringBuilder(input.Length);
@@ -109,7 +109,12 @@ namespace Discord.Commands
if (argString != null)
{
if (curParam == null)
return ParseResult.FromError(CommandError.BadArgCount, "The input text has too many parameters.");
{
if (ignoreExtraArgs)
break;
else
return ParseResult.FromError(CommandError.BadArgCount, "The input text has too many parameters.");
}

var typeReaderResult = await curParam.ParseAsync(context, argString, services).ConfigureAwait(false);
if (!typeReaderResult.IsSuccess && typeReaderResult.Error != CommandError.MultipleMatches)


+ 2
- 1
src/Discord.Net.Commands/CommandService.cs View File

@@ -27,7 +27,7 @@ namespace Discord.Commands
private readonly HashSet<ModuleInfo> _moduleDefs;
private readonly CommandMap _map;

internal readonly bool _caseSensitive, _throwOnError;
internal readonly bool _caseSensitive, _throwOnError, _ignoreExtraArgs;
internal readonly char _separatorChar;
internal readonly RunMode _defaultRunMode;
internal readonly Logger _cmdLogger;
@@ -42,6 +42,7 @@ namespace Discord.Commands
{
_caseSensitive = config.CaseSensitiveCommands;
_throwOnError = config.ThrowOnError;
_ignoreExtraArgs = config.IgnoreExtraArgs;
_separatorChar = config.SeparatorChar;
_defaultRunMode = config.DefaultRunMode;
if (_defaultRunMode == RunMode.Default)


+ 3
- 0
src/Discord.Net.Commands/CommandServiceConfig.cs View File

@@ -15,5 +15,8 @@

/// <summary> Determines whether RunMode.Sync commands should push exceptions up to the caller. </summary>
public bool ThrowOnError { get; set; } = true;

/// <summary> Determines whether extra parameters should be ignored. </summary>
public bool IgnoreExtraArgs { get; set; } = false;
}
}

+ 3
- 1
src/Discord.Net.Commands/Info/CommandInfo.cs View File

@@ -18,6 +18,7 @@ namespace Discord.Commands
private static readonly System.Reflection.MethodInfo _convertParamsMethod = typeof(CommandInfo).GetTypeInfo().GetDeclaredMethod(nameof(ConvertParamsList));
private static readonly ConcurrentDictionary<Type, Func<IEnumerable<object>, object>> _arrayConverters = new ConcurrentDictionary<Type, Func<IEnumerable<object>, object>>();

private readonly CommandService _commandService;
private readonly Func<ICommandContext, object[], IServiceProvider, CommandInfo, Task> _action;

public ModuleInfo Module { get; }
@@ -64,6 +65,7 @@ namespace Discord.Commands
HasVarArgs = builder.Parameters.Count > 0 ? builder.Parameters[builder.Parameters.Count - 1].IsMultiple : false;

_action = builder.Callback;
_commandService = service;
}

public async Task<PreconditionResult> CheckPreconditionsAsync(ICommandContext context, IServiceProvider services = null)
@@ -117,7 +119,7 @@ namespace Discord.Commands
return ParseResult.FromError(preconditionResult);

string input = searchResult.Text.Substring(startIndex);
return await CommandParser.ParseArgsAsync(this, context, services, input, 0).ConfigureAwait(false);
return await CommandParser.ParseArgsAsync(this, context, _commandService._ignoreExtraArgs, services, input, 0).ConfigureAwait(false);
}

public Task<IResult> ExecuteAsync(ICommandContext context, ParseResult parseResult, IServiceProvider services)


Loading…
Cancel
Save