Allow setting IgnoreExtraArgs on an individual basis (#998)

* Allow setting IgnoreExtraArgs on an individual basis

* Remove passing in the flag as a separate parameter

* VS plz

* Push the RunMode setting out to its own attribute, because fox wants consistency.

Bonus: Removes the need for that godawful 'RunMode.Default'.

* Revert previous commit

* Fox doesn't like module-wide switches 😒
This commit is contained in:
Joe4evr
2018-04-29 17:10:00 +02:00
committed by Christopher F
parent a3ce80c1dc
commit 6d3010065f
9 changed files with 11 additions and 20 deletions

View File

@@ -7,6 +7,7 @@ namespace Discord.Commands
{
public string Text { get; }
public RunMode RunMode { get; set; } = RunMode.Default;
public bool? IgnoreExtraArgs { get; set; }
public CommandAttribute()
{

View File

@@ -1,8 +1,7 @@
using System;
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
namespace Discord.Commands.Builders
{
@@ -22,6 +21,7 @@ namespace Discord.Commands.Builders
public string PrimaryAlias { get; set; }
public RunMode RunMode { get; set; }
public int Priority { get; set; }
public bool IgnoreExtraArgs { get; set; }
public IReadOnlyList<PreconditionAttribute> Preconditions => _preconditions;
public IReadOnlyList<ParameterBuilder> Parameters => _parameters;

View File

@@ -2,7 +2,6 @@ using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
namespace Discord.Commands.Builders
{

View File

@@ -158,6 +158,7 @@ namespace Discord.Commands
builder.AddAliases(command.Text);
builder.RunMode = command.RunMode;
builder.Name = builder.Name ?? command.Text;
builder.IgnoreExtraArgs = command.IgnoreExtraArgs ?? service._ignoreExtraArgs;
break;
case NameAttribute name:
builder.Name = name.Text;

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Immutable;
using System.Text;
using System.Threading.Tasks;
@@ -14,7 +14,7 @@ namespace Discord.Commands
QuotedParameter
}
public static async Task<ParseResult> ParseArgsAsync(CommandInfo command, ICommandContext context, bool ignoreExtraArgs, IServiceProvider services, string input, int startPos)
public static async Task<ParseResult> ParseArgsAsync(CommandInfo command, ICommandContext context, IServiceProvider services, string input, int startPos)
{
ParameterInfo curParam = null;
StringBuilder argBuilder = new StringBuilder(input.Length);
@@ -110,7 +110,7 @@ namespace Discord.Commands
{
if (curParam == null)
{
if (ignoreExtraArgs)
if (command.IgnoreExtraArgs)
break;
else
return ParseResult.FromError(CommandError.BadArgCount, "The input text has too many parameters.");

View File

@@ -6,7 +6,6 @@ using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Discord.Commands.Builders;
using Discord.Logging;

View File

@@ -20,11 +20,5 @@ namespace Discord.Commands
/// <summary> Determines whether extra parameters should be ignored. </summary>
public bool IgnoreExtraArgs { get; set; } = false;
///// <summary> Gets or sets the <see cref="IServiceProvider"/> to use. </summary>
//public IServiceProvider ServiceProvider { get; set; } = null;
///// <summary> Gets or sets a factory function for the <see cref="IServiceProvider"/> to use. </summary>
//public Func<CommandService, IServiceProvider> ServiceProviderFactory { get; set; } = null;
}
}

View File

@@ -27,6 +27,7 @@ namespace Discord.Commands
public string Remarks { get; }
public int Priority { get; }
public bool HasVarArgs { get; }
public bool IgnoreExtraArgs { get; }
public RunMode RunMode { get; }
public IReadOnlyList<string> Aliases { get; }
@@ -63,6 +64,7 @@ namespace Discord.Commands
Parameters = builder.Parameters.Select(x => x.Build(this)).ToImmutableArray();
HasVarArgs = builder.Parameters.Count > 0 ? builder.Parameters[builder.Parameters.Count - 1].IsMultiple : false;
IgnoreExtraArgs = builder.IgnoreExtraArgs;
_action = builder.Callback;
_commandService = service;
@@ -119,7 +121,7 @@ namespace Discord.Commands
return ParseResult.FromError(preconditionResult);
string input = searchResult.Text.Substring(startIndex);
return await CommandParser.ParseArgsAsync(this, context, _commandService._ignoreExtraArgs, services, input, 0).ConfigureAwait(false);
return await CommandParser.ParseArgsAsync(this, context, services, input, 0).ConfigureAwait(false);
}
public Task<IResult> ExecuteAsync(ICommandContext context, ParseResult parseResult, IServiceProvider services)

View File

@@ -2,7 +2,6 @@ using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Reflection;
using Discord.Commands.Builders;
namespace Discord.Commands
@@ -23,8 +22,6 @@ namespace Discord.Commands
public ModuleInfo Parent { get; }
public bool IsSubmodule => Parent != null;
//public TypeInfo TypeInfo { get; }
internal ModuleInfo(ModuleBuilder builder, CommandService service, IServiceProvider services, ModuleInfo parent = null)
{
Service = service;
@@ -35,8 +32,6 @@ namespace Discord.Commands
Group = builder.Group;
Parent = parent;
//TypeInfo = builder.TypeInfo;
Aliases = BuildAliases(builder, service).ToImmutableArray();
Commands = builder.Commands.Select(x => x.Build(this, service)).ToImmutableArray();
Preconditions = BuildPreconditions(builder).ToImmutableArray();