Cleaned up command builders and async func names
This commit is contained in:
@@ -7,107 +7,109 @@ namespace Discord.Commands.Builders
|
||||
{
|
||||
public class CommandBuilder
|
||||
{
|
||||
private List<PreconditionAttribute> preconditions;
|
||||
private List<ParameterBuilder> parameters;
|
||||
private List<string> aliases;
|
||||
private readonly List<PreconditionAttribute> _preconditions;
|
||||
private readonly List<ParameterBuilder> _parameters;
|
||||
private readonly List<string> _aliases;
|
||||
|
||||
internal CommandBuilder(ModuleBuilder module)
|
||||
{
|
||||
preconditions = new List<PreconditionAttribute>();
|
||||
parameters = new List<ParameterBuilder>();
|
||||
aliases = new List<string>();
|
||||
|
||||
Module = module;
|
||||
}
|
||||
public ModuleBuilder Module { get; }
|
||||
internal Func<CommandContext, object[], IDependencyMap, Task> Callback { get; set; }
|
||||
|
||||
public string Name { get; set; }
|
||||
public string Summary { get; set; }
|
||||
public string Remarks { get; set; }
|
||||
public RunMode RunMode { get; set; }
|
||||
public int Priority { get; set; }
|
||||
public Func<CommandContext, object[], IDependencyMap, Task> Callback { get; set; }
|
||||
public ModuleBuilder Module { get; }
|
||||
|
||||
public List<PreconditionAttribute> Preconditions => preconditions;
|
||||
public List<ParameterBuilder> Parameters => parameters;
|
||||
public List<string> Aliases => aliases;
|
||||
public IReadOnlyList<PreconditionAttribute> Preconditions => _preconditions;
|
||||
public IReadOnlyList<ParameterBuilder> Parameters => _parameters;
|
||||
public IReadOnlyList<string> Aliases => _aliases;
|
||||
|
||||
public CommandBuilder SetName(string name)
|
||||
//Automatic
|
||||
internal CommandBuilder(ModuleBuilder module)
|
||||
{
|
||||
Module = module;
|
||||
|
||||
_preconditions = new List<PreconditionAttribute>();
|
||||
_parameters = new List<ParameterBuilder>();
|
||||
_aliases = new List<string>();
|
||||
}
|
||||
//User-defined
|
||||
internal CommandBuilder(ModuleBuilder module, string primaryAlias, Func<CommandContext, object[], IDependencyMap, Task> callback)
|
||||
: this(module)
|
||||
{
|
||||
Discord.Preconditions.NotNull(primaryAlias, nameof(primaryAlias));
|
||||
Discord.Preconditions.NotNull(callback, nameof(callback));
|
||||
|
||||
Callback = callback;
|
||||
_aliases.Add(primaryAlias);
|
||||
}
|
||||
|
||||
public CommandBuilder WithName(string name)
|
||||
{
|
||||
Name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder SetSummary(string summary)
|
||||
public CommandBuilder WithSummary(string summary)
|
||||
{
|
||||
Summary = summary;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder SetRemarks(string remarks)
|
||||
public CommandBuilder WithRemarks(string remarks)
|
||||
{
|
||||
Remarks = remarks;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder SetRunMode(RunMode runMode)
|
||||
public CommandBuilder WithRunMode(RunMode runMode)
|
||||
{
|
||||
RunMode = runMode;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder SetPriority(int priority)
|
||||
public CommandBuilder WithPriority(int priority)
|
||||
{
|
||||
Priority = priority;
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder SetCallback(Func<CommandContext, object[], IDependencyMap, Task> callback)
|
||||
public CommandBuilder AddAliases(params string[] aliases)
|
||||
{
|
||||
Callback = callback;
|
||||
_aliases.AddRange(aliases);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder AddPrecondition(PreconditionAttribute precondition)
|
||||
{
|
||||
preconditions.Add(precondition);
|
||||
_preconditions.Add(precondition);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder AddParameter(Action<ParameterBuilder> createFunc)
|
||||
public CommandBuilder AddParameter(string name, Type type, Action<ParameterBuilder> createFunc)
|
||||
{
|
||||
var param = new ParameterBuilder();
|
||||
var param = new ParameterBuilder(this, name, type);
|
||||
createFunc(param);
|
||||
parameters.Add(param);
|
||||
_parameters.Add(param);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CommandBuilder AddAliases(params string[] newAliases)
|
||||
internal CommandBuilder AddParameter(Action<ParameterBuilder> createFunc)
|
||||
{
|
||||
aliases.AddRange(newAliases);
|
||||
var param = new ParameterBuilder(this);
|
||||
createFunc(param);
|
||||
_parameters.Add(param);
|
||||
return this;
|
||||
}
|
||||
|
||||
internal CommandInfo Build(ModuleInfo info, CommandService service)
|
||||
{
|
||||
if (aliases.Count == 0)
|
||||
throw new InvalidOperationException("Commands require at least one alias to be registered");
|
||||
|
||||
if (Callback == null)
|
||||
throw new InvalidOperationException("Commands require a callback to be built");
|
||||
|
||||
//Default name to first alias
|
||||
if (Name == null)
|
||||
Name = aliases[0];
|
||||
Name = _aliases[0];
|
||||
|
||||
if (parameters.Count > 0)
|
||||
if (_parameters.Count > 0)
|
||||
{
|
||||
var lastParam = parameters[parameters.Count - 1];
|
||||
var lastParam = _parameters[_parameters.Count - 1];
|
||||
|
||||
var firstMultipleParam = parameters.FirstOrDefault(x => x.Multiple);
|
||||
var firstMultipleParam = _parameters.FirstOrDefault(x => x.IsMultiple);
|
||||
if ((firstMultipleParam != null) && (firstMultipleParam != lastParam))
|
||||
throw new InvalidOperationException("Only the last parameter in a command may have the Multiple flag.");
|
||||
|
||||
var firstRemainderParam = parameters.FirstOrDefault(x => x.Remainder);
|
||||
var firstRemainderParam = _parameters.FirstOrDefault(x => x.IsRemainder);
|
||||
if ((firstRemainderParam != null) && (firstRemainderParam != lastParam))
|
||||
throw new InvalidOperationException("Only the last parameter in a command may have the Remainder flag.");
|
||||
}
|
||||
|
||||
@@ -1,96 +1,107 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Discord.Commands.Builders
|
||||
{
|
||||
public class ModuleBuilder
|
||||
{
|
||||
private List<CommandBuilder> commands;
|
||||
private List<ModuleBuilder> submodules;
|
||||
private List<PreconditionAttribute> preconditions;
|
||||
private List<string> aliases;
|
||||
|
||||
public ModuleBuilder()
|
||||
{
|
||||
commands = new List<CommandBuilder>();
|
||||
submodules = new List<ModuleBuilder>();
|
||||
preconditions = new List<PreconditionAttribute>();
|
||||
aliases = new List<string>();
|
||||
}
|
||||
|
||||
internal ModuleBuilder(ModuleBuilder parent)
|
||||
: this()
|
||||
{
|
||||
ParentModule = parent;
|
||||
}
|
||||
private readonly List<CommandBuilder> _commands;
|
||||
private readonly List<ModuleBuilder> _submodules;
|
||||
private readonly List<PreconditionAttribute> _preconditions;
|
||||
private readonly List<string> _aliases;
|
||||
|
||||
public CommandService Service { get; }
|
||||
public ModuleBuilder Parent { get; }
|
||||
public string Name { get; set; }
|
||||
public string Summary { get; set; }
|
||||
public string Remarks { get; set; }
|
||||
public ModuleBuilder ParentModule { get; }
|
||||
|
||||
public List<CommandBuilder> Commands => commands;
|
||||
public List<ModuleBuilder> Modules => submodules;
|
||||
public List<PreconditionAttribute> Preconditions => preconditions;
|
||||
public List<string> Aliases => aliases;
|
||||
public IReadOnlyList<CommandBuilder> Commands => _commands;
|
||||
public IReadOnlyList<ModuleBuilder> Modules => _submodules;
|
||||
public IReadOnlyList<PreconditionAttribute> Preconditions => _preconditions;
|
||||
public IReadOnlyList<string> Aliases => _aliases;
|
||||
|
||||
public ModuleBuilder SetName(string name)
|
||||
//Automatic
|
||||
internal ModuleBuilder(CommandService service, ModuleBuilder parent)
|
||||
{
|
||||
Service = service;
|
||||
Parent = parent;
|
||||
|
||||
_commands = new List<CommandBuilder>();
|
||||
_submodules = new List<ModuleBuilder>();
|
||||
_preconditions = new List<PreconditionAttribute>();
|
||||
_aliases = new List<string>();
|
||||
}
|
||||
//User-defined
|
||||
internal ModuleBuilder(CommandService service, ModuleBuilder parent, string primaryAlias)
|
||||
: this(service, parent)
|
||||
{
|
||||
Discord.Preconditions.NotNull(primaryAlias, nameof(primaryAlias));
|
||||
|
||||
_aliases = new List<string> { primaryAlias };
|
||||
}
|
||||
|
||||
public ModuleBuilder WithName(string name)
|
||||
{
|
||||
Name = name;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModuleBuilder SetSummary(string summary)
|
||||
public ModuleBuilder WithSummary(string summary)
|
||||
{
|
||||
Summary = summary;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModuleBuilder SetRemarks(string remarks)
|
||||
public ModuleBuilder WithRemarks(string remarks)
|
||||
{
|
||||
Remarks = remarks;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModuleBuilder AddAliases(params string[] newAliases)
|
||||
public ModuleBuilder AddAlias(params string[] newAliases)
|
||||
{
|
||||
aliases.AddRange(newAliases);
|
||||
_aliases.AddRange(newAliases);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModuleBuilder AddPrecondition(PreconditionAttribute precondition)
|
||||
{
|
||||
preconditions.Add(precondition);
|
||||
_preconditions.Add(precondition);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModuleBuilder AddCommand(Action<CommandBuilder> createFunc)
|
||||
public ModuleBuilder AddCommand(string primaryAlias, Func<CommandContext, object[], IDependencyMap, Task> callback, Action<CommandBuilder> createFunc)
|
||||
{
|
||||
var builder = new CommandBuilder(this, primaryAlias, callback);
|
||||
createFunc(builder);
|
||||
_commands.Add(builder);
|
||||
return this;
|
||||
}
|
||||
internal ModuleBuilder AddCommand(Action<CommandBuilder> createFunc)
|
||||
{
|
||||
var builder = new CommandBuilder(this);
|
||||
createFunc(builder);
|
||||
commands.Add(builder);
|
||||
_commands.Add(builder);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModuleBuilder AddSubmodule(Action<ModuleBuilder> createFunc)
|
||||
public ModuleBuilder AddModule(string primaryAlias, Action<ModuleBuilder> createFunc)
|
||||
{
|
||||
var builder = new ModuleBuilder(this);
|
||||
var builder = new ModuleBuilder(Service, this, primaryAlias);
|
||||
createFunc(builder);
|
||||
submodules.Add(builder);
|
||||
_submodules.Add(builder);
|
||||
return this;
|
||||
}
|
||||
internal ModuleBuilder AddModule(Action<ModuleBuilder> createFunc)
|
||||
{
|
||||
var builder = new ModuleBuilder(Service, this);
|
||||
createFunc(builder);
|
||||
_submodules.Add(builder);
|
||||
return this;
|
||||
}
|
||||
|
||||
public ModuleInfo Build(CommandService service)
|
||||
{
|
||||
if (aliases.Count == 0)
|
||||
throw new InvalidOperationException("Modules require at least one alias to be registered");
|
||||
|
||||
if (commands.Count == 0 && submodules.Count == 0)
|
||||
throw new InvalidOperationException("Tried to build empty module");
|
||||
|
||||
//Default name to first alias
|
||||
if (Name == null)
|
||||
Name = aliases[0];
|
||||
Name = _aliases[0];
|
||||
|
||||
return new ModuleInfo(this, service);
|
||||
}
|
||||
|
||||
229
src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
Normal file
229
src/Discord.Net.Commands/Builders/ModuleClassBuilder.cs
Normal file
@@ -0,0 +1,229 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Discord.Commands.Builders;
|
||||
|
||||
namespace Discord.Commands
|
||||
{
|
||||
internal static class ModuleClassBuilder
|
||||
{
|
||||
private static readonly TypeInfo _moduleTypeInfo = typeof(ModuleBase).GetTypeInfo();
|
||||
|
||||
public static IEnumerable<TypeInfo> Search(Assembly assembly)
|
||||
{
|
||||
foreach (var type in assembly.ExportedTypes)
|
||||
{
|
||||
var typeInfo = type.GetTypeInfo();
|
||||
if (IsValidModuleDefinition(typeInfo) &&
|
||||
!typeInfo.IsDefined(typeof(DontAutoLoadAttribute)))
|
||||
{
|
||||
yield return typeInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Dictionary<Type, ModuleInfo> Build(CommandService service, params TypeInfo[] validTypes) => Build(validTypes, service);
|
||||
public static Dictionary<Type, ModuleInfo> Build(IEnumerable<TypeInfo> validTypes, CommandService service)
|
||||
{
|
||||
if (!validTypes.Any())
|
||||
throw new InvalidOperationException("Could not find any valid modules from the given selection");
|
||||
|
||||
var topLevelGroups = validTypes.Where(x => x.DeclaringType == null);
|
||||
var subGroups = validTypes.Intersect(topLevelGroups);
|
||||
|
||||
var builtTypes = new List<TypeInfo>();
|
||||
|
||||
var result = new Dictionary<Type, ModuleInfo>();
|
||||
|
||||
foreach (var typeInfo in topLevelGroups)
|
||||
{
|
||||
// TODO: This shouldn't be the case; may be safe to remove?
|
||||
if (result.ContainsKey(typeInfo.AsType()))
|
||||
continue;
|
||||
|
||||
var module = new ModuleBuilder(service, null);
|
||||
|
||||
BuildModule(module, typeInfo, service);
|
||||
BuildSubTypes(module, typeInfo.DeclaredNestedTypes, builtTypes, service);
|
||||
|
||||
result[typeInfo.AsType()] = module.Build(service);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void BuildSubTypes(ModuleBuilder builder, IEnumerable<TypeInfo> subTypes, List<TypeInfo> builtTypes, CommandService service)
|
||||
{
|
||||
foreach (var typeInfo in subTypes)
|
||||
{
|
||||
if (!IsValidModuleDefinition(typeInfo))
|
||||
continue;
|
||||
|
||||
if (builtTypes.Contains(typeInfo))
|
||||
continue;
|
||||
|
||||
builder.AddModule((module) => {
|
||||
BuildModule(module, typeInfo, service);
|
||||
BuildSubTypes(module, typeInfo.DeclaredNestedTypes, builtTypes, service);
|
||||
});
|
||||
|
||||
builtTypes.Add(typeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
private static void BuildModule(ModuleBuilder builder, TypeInfo typeInfo, CommandService service)
|
||||
{
|
||||
var attributes = typeInfo.GetCustomAttributes();
|
||||
|
||||
foreach (var attribute in attributes)
|
||||
{
|
||||
// TODO: C#7 type switch
|
||||
if (attribute is NameAttribute)
|
||||
builder.Name = (attribute as NameAttribute).Text;
|
||||
else if (attribute is SummaryAttribute)
|
||||
builder.Summary = (attribute as SummaryAttribute).Text;
|
||||
else if (attribute is RemarksAttribute)
|
||||
builder.Remarks = (attribute as RemarksAttribute).Text;
|
||||
else if (attribute is AliasAttribute)
|
||||
builder.AddAlias((attribute as AliasAttribute).Aliases);
|
||||
else if (attribute is GroupAttribute)
|
||||
{
|
||||
var groupAttr = attribute as GroupAttribute;
|
||||
builder.Name = builder.Name ?? groupAttr.Prefix;
|
||||
builder.AddAlias(groupAttr.Prefix);
|
||||
}
|
||||
else if (attribute is PreconditionAttribute)
|
||||
builder.AddPrecondition(attribute as PreconditionAttribute);
|
||||
}
|
||||
|
||||
if (builder.Name == null)
|
||||
builder.Name = typeInfo.Name;
|
||||
|
||||
var validCommands = typeInfo.DeclaredMethods.Where(x => IsValidCommandDefinition(x));
|
||||
|
||||
foreach (var method in validCommands)
|
||||
{
|
||||
builder.AddCommand((command) => {
|
||||
BuildCommand(command, typeInfo, method, service);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private static void BuildCommand(CommandBuilder builder, TypeInfo typeInfo, MethodInfo method, CommandService service)
|
||||
{
|
||||
var attributes = method.GetCustomAttributes();
|
||||
|
||||
foreach (var attribute in attributes)
|
||||
{
|
||||
// TODO: C#7 type switch
|
||||
if (attribute is CommandAttribute)
|
||||
{
|
||||
var cmdAttr = attribute as CommandAttribute;
|
||||
builder.AddAliases(cmdAttr.Text);
|
||||
builder.RunMode = cmdAttr.RunMode;
|
||||
builder.Name = builder.Name ?? cmdAttr.Text;
|
||||
}
|
||||
else if (attribute is NameAttribute)
|
||||
builder.Name = (attribute as NameAttribute).Text;
|
||||
else if (attribute is PriorityAttribute)
|
||||
builder.Priority = (attribute as PriorityAttribute).Priority;
|
||||
else if (attribute is SummaryAttribute)
|
||||
builder.Summary = (attribute as SummaryAttribute).Text;
|
||||
else if (attribute is RemarksAttribute)
|
||||
builder.Remarks = (attribute as RemarksAttribute).Text;
|
||||
else if (attribute is AliasAttribute)
|
||||
builder.AddAliases((attribute as AliasAttribute).Aliases);
|
||||
else if (attribute is PreconditionAttribute)
|
||||
builder.AddPrecondition(attribute as PreconditionAttribute);
|
||||
}
|
||||
|
||||
var parameters = method.GetParameters();
|
||||
int pos = 0, count = parameters.Length;
|
||||
foreach (var paramInfo in parameters)
|
||||
{
|
||||
builder.AddParameter((parameter) => {
|
||||
BuildParameter(parameter, paramInfo, pos++, count, service);
|
||||
});
|
||||
}
|
||||
|
||||
var createInstance = ReflectionUtils.CreateBuilder<ModuleBase>(typeInfo, service);
|
||||
|
||||
builder.Callback = (ctx, args, map) => {
|
||||
var instance = createInstance(map);
|
||||
instance.Context = ctx;
|
||||
try
|
||||
{
|
||||
return method.Invoke(instance, args) as Task ?? Task.CompletedTask;
|
||||
}
|
||||
finally{
|
||||
(instance as IDisposable)?.Dispose();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static void BuildParameter(ParameterBuilder builder, System.Reflection.ParameterInfo paramInfo, int position, int count, CommandService service)
|
||||
{
|
||||
var attributes = paramInfo.GetCustomAttributes();
|
||||
var paramType = paramInfo.ParameterType;
|
||||
|
||||
builder.Name = paramInfo.Name;
|
||||
|
||||
builder.IsOptional = paramInfo.IsOptional;
|
||||
builder.DefaultValue = paramInfo.HasDefaultValue ? paramInfo.DefaultValue : null;
|
||||
|
||||
foreach (var attribute in attributes)
|
||||
{
|
||||
// TODO: C#7 type switch
|
||||
if (attribute is SummaryAttribute)
|
||||
builder.Summary = (attribute as SummaryAttribute).Text;
|
||||
else if (attribute is ParamArrayAttribute)
|
||||
{
|
||||
builder.IsMultiple = true;
|
||||
paramType = paramType.GetElementType();
|
||||
}
|
||||
else if (attribute is RemainderAttribute)
|
||||
{
|
||||
if (position != count-1)
|
||||
throw new InvalidOperationException("Remainder parameters must be the last parameter in a command.");
|
||||
|
||||
builder.IsRemainder = true;
|
||||
}
|
||||
}
|
||||
|
||||
var reader = service.GetTypeReader(paramType);
|
||||
if (reader == null)
|
||||
{
|
||||
var paramTypeInfo = paramType.GetTypeInfo();
|
||||
if (paramTypeInfo.IsEnum)
|
||||
{
|
||||
reader = EnumTypeReader.GetReader(paramType);
|
||||
service.AddTypeReader(paramType, reader);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException($"{paramType.FullName} is not supported as a command parameter, are you missing a TypeReader?");
|
||||
}
|
||||
}
|
||||
|
||||
builder.ParameterType = paramType;
|
||||
builder.TypeReader = reader;
|
||||
}
|
||||
|
||||
private static bool IsValidModuleDefinition(TypeInfo typeInfo)
|
||||
{
|
||||
return _moduleTypeInfo.IsAssignableFrom(typeInfo) &&
|
||||
!typeInfo.IsAbstract;
|
||||
}
|
||||
|
||||
private static bool IsValidCommandDefinition(MethodInfo methodInfo)
|
||||
{
|
||||
return methodInfo.IsDefined(typeof(CommandAttribute)) &&
|
||||
methodInfo.ReturnType == typeof(Task) &&
|
||||
!methodInfo.IsStatic &&
|
||||
!methodInfo.IsGenericMethod;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,100 +1,79 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Discord.Commands.Builders
|
||||
{
|
||||
public class ParameterBuilder
|
||||
{
|
||||
public ParameterBuilder()
|
||||
{ }
|
||||
|
||||
public ParameterBuilder(string name)
|
||||
{
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public string Name { get; set; }
|
||||
public string Summary { get; set; }
|
||||
public object DefaultValue { get; set; }
|
||||
public Type ParameterType { get; set; }
|
||||
public CommandBuilder Command { get; }
|
||||
public string Name { get; internal set; }
|
||||
public Type ParameterType { get; internal set; }
|
||||
|
||||
public TypeReader TypeReader { get; set; }
|
||||
public bool IsOptional { get; set; }
|
||||
public bool IsRemainder { get; set; }
|
||||
public bool IsMultiple { get; set; }
|
||||
public object DefaultValue { get; set; }
|
||||
public string Summary { get; set; }
|
||||
|
||||
public bool Optional { get; set; }
|
||||
public bool Remainder { get; set; }
|
||||
public bool Multiple { get; set; }
|
||||
|
||||
public ParameterBuilder SetName(string name)
|
||||
//Automatic
|
||||
internal ParameterBuilder(CommandBuilder command)
|
||||
{
|
||||
Command = command;
|
||||
}
|
||||
//User-defined
|
||||
internal ParameterBuilder(CommandBuilder command, string name, Type type)
|
||||
: this(command)
|
||||
{
|
||||
Preconditions.NotNull(name, nameof(name));
|
||||
|
||||
Name = name;
|
||||
return this;
|
||||
SetType(type);
|
||||
}
|
||||
|
||||
public ParameterBuilder SetSummary(string summary)
|
||||
internal void SetType(Type type)
|
||||
{
|
||||
TypeReader = Command.Module.Service.GetTypeReader(type);
|
||||
|
||||
if (type.GetTypeInfo().IsValueType)
|
||||
DefaultValue = Activator.CreateInstance(type);
|
||||
else if (type.IsArray)
|
||||
type = ParameterType.GetElementType();
|
||||
ParameterType = type;
|
||||
}
|
||||
|
||||
public ParameterBuilder WithSummary(string summary)
|
||||
{
|
||||
Summary = summary;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetDefault<T>(T defaultValue)
|
||||
public ParameterBuilder WithDefault(object defaultValue)
|
||||
{
|
||||
Optional = true;
|
||||
DefaultValue = defaultValue;
|
||||
ParameterType = typeof(T);
|
||||
|
||||
if (ParameterType.IsArray)
|
||||
ParameterType = ParameterType.GetElementType();
|
||||
|
||||
DefaultValue = defaultValue;
|
||||
return this;
|
||||
}
|
||||
public ParameterBuilder WithIsOptional(bool isOptional)
|
||||
{
|
||||
IsOptional = isOptional;
|
||||
return this;
|
||||
}
|
||||
public ParameterBuilder WithIsRemainder(bool isRemainder)
|
||||
{
|
||||
IsRemainder = isRemainder;
|
||||
return this;
|
||||
}
|
||||
public ParameterBuilder WithIsMultiple(bool isMultiple)
|
||||
{
|
||||
IsMultiple = isMultiple;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetType(Type parameterType)
|
||||
internal ParameterInfo Build(CommandInfo info)
|
||||
{
|
||||
ParameterType = parameterType;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetTypeReader(TypeReader reader)
|
||||
{
|
||||
TypeReader = reader;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetOptional(bool isOptional)
|
||||
{
|
||||
Optional = isOptional;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetRemainder(bool isRemainder)
|
||||
{
|
||||
Remainder = isRemainder;
|
||||
return this;
|
||||
}
|
||||
|
||||
public ParameterBuilder SetMultiple(bool isMultiple)
|
||||
{
|
||||
Multiple = isMultiple;
|
||||
return this;
|
||||
}
|
||||
|
||||
internal ParameterInfo Build(CommandInfo info, CommandService service)
|
||||
{
|
||||
// TODO: should we throw when we don't have a name?
|
||||
if (Name == null)
|
||||
Name = "[unknown parameter]";
|
||||
|
||||
if (ParameterType == null)
|
||||
throw new InvalidOperationException($"Could not build parameter {Name} from command {info.Name} - An invalid parameter type was given");
|
||||
|
||||
if (TypeReader == null)
|
||||
TypeReader = service.GetTypeReader(ParameterType);
|
||||
throw new InvalidOperationException($"No default TypeReader found, one must be specified");
|
||||
|
||||
if (TypeReader == null)
|
||||
throw new InvalidOperationException($"Could not build parameter {Name} from command {info.Name} - A valid TypeReader could not be found");
|
||||
|
||||
return new ParameterInfo(this, info, service);
|
||||
return new ParameterInfo(this, info, Command.Module.Service);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user