Skip to content

Commit

Permalink
Allow setting IgnoreExtraArgs on an individual basis (#998)
Browse files Browse the repository at this point in the history
* 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 😒
  • Loading branch information
Joe4evr authored and foxbot committed Apr 29, 2018
1 parent a3ce80c commit 6d30100
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/Discord.Net.Commands/Attributes/CommandAttribute.cs
Expand Up @@ -7,6 +7,7 @@ public class CommandAttribute : Attribute
{
public string Text { get; }
public RunMode RunMode { get; set; } = RunMode.Default;
public bool? IgnoreExtraArgs { get; set; }

public CommandAttribute()
{
Expand Down
6 changes: 3 additions & 3 deletions src/Discord.Net.Commands/Builders/CommandBuilder.cs
@@ -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
{
Expand All @@ -22,6 +21,7 @@ public class CommandBuilder
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;
Expand Down Expand Up @@ -140,4 +140,4 @@ internal CommandInfo Build(ModuleInfo info, CommandService service)
return new CommandInfo(this, info, service);
}
}
}
}
1 change: 0 additions & 1 deletion src/Discord.Net.Commands/Builders/ModuleBuilder.cs
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;

namespace Discord.Commands.Builders
{
Expand Down
1 change: 1 addition & 0 deletions src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
Expand Up @@ -158,6 +158,7 @@ private static void BuildCommand(CommandBuilder builder, TypeInfo typeInfo, Meth
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;
Expand Down
6 changes: 3 additions & 3 deletions src/Discord.Net.Commands/CommandParser.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Immutable;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -14,7 +14,7 @@ private enum ParserPart
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);
Expand Down Expand Up @@ -110,7 +110,7 @@ public static async Task<ParseResult> ParseArgsAsync(CommandInfo command, IComma
{
if (curParam == null)
{
if (ignoreExtraArgs)
if (command.IgnoreExtraArgs)
break;
else
return ParseResult.FromError(CommandError.BadArgCount, "The input text has too many parameters.");
Expand Down
1 change: 0 additions & 1 deletion src/Discord.Net.Commands/CommandService.cs
Expand Up @@ -6,7 +6,6 @@
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Discord.Commands.Builders;
using Discord.Logging;

Expand Down
6 changes: 0 additions & 6 deletions src/Discord.Net.Commands/CommandServiceConfig.cs
Expand Up @@ -20,11 +20,5 @@ public class CommandServiceConfig

/// <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;
}
}
4 changes: 3 additions & 1 deletion src/Discord.Net.Commands/Info/CommandInfo.cs
Expand Up @@ -27,6 +27,7 @@ public class CommandInfo
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; }
Expand Down Expand Up @@ -63,6 +64,7 @@ internal CommandInfo(CommandBuilder builder, ModuleInfo module, CommandService s

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;
Expand Down Expand Up @@ -119,7 +121,7 @@ public async Task<ParseResult> ParseAsync(ICommandContext context, int startInde
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)
Expand Down
5 changes: 0 additions & 5 deletions src/Discord.Net.Commands/Info/ModuleInfo.cs
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Reflection;
using Discord.Commands.Builders;

namespace Discord.Commands
Expand All @@ -23,8 +22,6 @@ public class ModuleInfo
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;
Expand All @@ -35,8 +32,6 @@ internal ModuleInfo(ModuleBuilder builder, CommandService service, IServiceProvi
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();
Expand Down

0 comments on commit 6d30100

Please sign in to comment.