Skip to content

Commit e319119

Browse files
committed
1.1.1
Add argument in Command fix TargetParameterCountException CommandCheck -> Target.Method object execute -> internal
1 parent d252fbf commit e319119

File tree

10 files changed

+79
-43
lines changed

10 files changed

+79
-43
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.ionide/

CommandExecutor/Attributes/CommandCheckAttribute.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace CommandExecutor.Attributes
77
/// Check before method execute.
88
/// You can create your own check attribute by inheriting this class
99
/// </summary>
10+
[AttributeUsage(AttributeTargets.Method)]
1011
public abstract class CommandCheckAttribute : Attribute
1112
{
1213
/// <summary>

CommandExecutor/CommandExecutor.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ public void Execute(string command, string[] args) =>
124124
/// </summary>
125125
/// <param name="command">The string to search command</param>
126126
/// <param name="args">The object array to pass as arguments</param>
127-
public void Execute(string command, object[] args) =>
127+
128+
// Because of it's risk, We decided to change to internal.
129+
internal void Execute(string command, object[] args) =>
128130
FindCommand(command).Execute(args);
129131

130132
/// <summary>
@@ -135,13 +137,13 @@ public void Execute(string command, object[] args) =>
135137
/// <returns>The class that has information of command</returns>
136138
public Command FindCommand(string cmd)
137139
{
138-
(MethodInfo method, CommandModule module, var _) = FindCommand(cmd, null);
140+
(MethodInfo method, CommandModule module) = FindCommand(cmd, null);
139141

140142
Executor client = this;
141143
return new Command(ref client, module, method);
142144
}
143145

144-
internal (MethodInfo Method, CommandModule Module, object[] Parameters) FindCommand(string command, string[] args)
146+
internal (MethodInfo Method, CommandModule Module) FindCommand(string command, string _)
145147
{
146148
MethodInfo method = null;
147149
CommandModule module = null;
@@ -174,20 +176,14 @@ public Command FindCommand(string cmd)
174176
}
175177

176178
if (method == null || module == null)
177-
throw new CommandNotFoundException(new CommandRequest()
178-
{
179-
Command = command,
180-
Arguments = args
181-
});
182-
183-
object[] paras = ConvertParameter(method, args);
179+
throw new CommandNotFoundException(command);
184180

185-
return (method, module, paras.Any() ? paras.ToArray() : null);
181+
return (method, module);
186182
}
187183

188184
internal object[] ConvertParameter(MethodInfo method, string[] args)
189185
{
190-
if (args == null) return Array.Empty<object>();
186+
if (args == null) args = Array.Empty<string>();
191187

192188
ParameterInfo[] paras = method.GetParameters();
193189

CommandExecutor/CommandExecutor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<DocumentationFile>bin\CommandExecutor.xml</DocumentationFile>
99
<!--Assembly Info-->
10-
<Version>1.1.0</Version>
10+
<Version>1.1.1</Version>
1111
<Authors>Lukince</Authors>
1212
<Company>Lukince</Company>
1313
<Product>CommandExecutor</Product>

CommandExecutor/Exceptions/CommandNotFoundException.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ namespace CommandExecutor.Exceptions
99
public class CommandNotFoundException : Exception
1010
{
1111
/// <summary>
12-
/// Initialize object with <see cref="CommandRequest"/>
12+
/// Initialize object with string
1313
/// </summary>
14-
/// <param name="request"></param>
15-
public CommandNotFoundException(CommandRequest request): base(GetExceptionMessage(request))
14+
/// <param name="request">The name of command.</param>
15+
public CommandNotFoundException(string request): base(GetExceptionMessage(request))
1616
{
17-
this.Request = request;
17+
this.CommandName = request;
1818
}
1919

2020
/// <summary>
2121
/// The request object when exception raised.
2222
/// </summary>
23-
public CommandRequest Request { get; set; }
23+
public string CommandName { get; set; }
2424

25-
private static string GetExceptionMessage(CommandRequest req)
26-
=> $"Cannot found method name : {req.Command}. Check options if some options set false.";
25+
private static string GetExceptionMessage(string req)
26+
=> $"Cannot found method name : {req}. Check options if some options set false.";
2727
}
2828
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
3+
namespace CommandExecutor
4+
{
5+
/// <summary>
6+
/// A logical exception that occurs during command execution.
7+
/// It occurs trying to run command in preparation
8+
/// </summary>
9+
public class ExecuteException : Exception
10+
{
11+
/// <summary>
12+
/// Initialize object with string
13+
/// </summary>
14+
/// <param name="message">The exception message</param>
15+
public ExecuteException(string message) : base (message) { }
16+
}
17+
}

CommandExecutor/Structures/Command.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ internal Command(ref Executor client, CommandModule c, MethodInfo m)
5454
/// </summary>
5555
public IEnumerable<Attribute> CustomAttributes => _command.GetCustomAttributes();
5656

57+
/// <summary>
58+
/// The converted objects.
59+
/// </summary>
60+
/// <value></value>
61+
public object[] Arguments { get; private set; } = null;
62+
63+
/// <summary>
64+
/// The raw string passed.
65+
/// </summary>
66+
/// <value></value>
67+
public string RawArguments { get; private set; } = null;
68+
69+
private bool isExeuting;
70+
5771
private readonly MethodInfo _command;
5872

5973
private readonly CommandAttribute _attribute;
@@ -68,6 +82,10 @@ internal Command(ref Executor client, CommandModule c, MethodInfo m)
6882
/// <param name="param">The object array that is command parameters</param>
6983
public void Execute(object[] param)
7084
{
85+
if (isExeuting) throw new ExecuteException("You can't execute again while checking command.");
86+
87+
isExeuting = true;
88+
7189
if (ModuleClass.CheckExecute(this) == false) throw new CommandCheckFailException(this, $"CheckExecute returns false in {ModuleClass.GetType().Name}");
7290

7391
ModuleClass.BeforeExecute();
@@ -79,13 +97,20 @@ public void Execute(object[] param)
7997
_command.Invoke(ModuleClass, param);
8098

8199
ModuleClass.AfterExecute();
100+
101+
isExeuting = false;
82102
}
83103

84104
/// <summary>
85105
/// Run command with string which will convert as parameter type array
86106
/// </summary>
87107
/// <param name="param">The string for method parameter. parameter will split with space.</param>
88-
public void Execute(string param) =>
89-
Execute(Client.ConvertParameter(_command, param?.Split(' ')));
108+
public void Execute(string param)
109+
{
110+
RawArguments = param;
111+
object[] arg = Client.ConvertParameter(_command, param?.Split(' '));
112+
Arguments = arg;
113+
Execute(arg);
114+
}
90115
}
91116
}

CommandExecutor/Structures/CommandRequest.cs

Lines changed: 0 additions & 20 deletions
This file was deleted.

Test/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void Run()
3333
IgnoreCase = true,
3434
IgnoreExtraArguments = false,
3535
GetPrivateMethod = true,
36-
IncludeStaticMethod = false
36+
IncludeStaticMethod = true
3737
});
3838

3939
Executor.RegisterCommands<ScopeCommands>();
@@ -44,6 +44,9 @@ void Run()
4444
Executor.RegisterCommands<SpecialCommands>();
4545

4646
Executor.Execute("ping hello true");
47+
48+
//Executor.Execute("ping");
49+
4750
Executor.Execute("args 3 5 2 asdf");
4851
Executor.Execute("inf inf this is inf arg test");
4952
Executor.Execute("nonparam");

Test/TestAttribute.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using CommandExecutor.Attributes;
2+
using CommandExecutor.Structures;
3+
4+
namespace Test
5+
{
6+
public class TestAttribute : CommandCheckAttribute
7+
{
8+
public override bool Check(Command cmd)
9+
{
10+
return true;
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)