-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
162 lines (130 loc) · 4.55 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#pragma warning disable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using CommandExecutor;
using CommandExecutor.Attributes;
using CommandExecutor.EventArgs;
using CommandExecutor.Exceptions;
using CommandExecutor.Structures;
namespace Test
{
class CustomAttribute : CommandCheckAttribute
{
public override bool Check(Command cmd)
{
return false;
}
}
class Program
{
public Executor Executor { get; private set; }
static void Main()
{
var prog = new Program();
prog.Run();
}
void Run()
{
ExceptionManager manager = ExceptionManager.Default;
manager.DefaultType = CommandThrowType.Event;
manager.Set<CommandCheckFailException>(CommandThrowType.Event);
manager.Set<ExecuteException>(CommandThrowType.Event);
Executor = new(new ExecutorConfiguration() {
IgnoreCase = true,
IgnoreExtraArguments = false,
GetPrivateMethod = true,
IncludeStaticMethod = true,
ExceptionConfiguration = manager
});
Executor.CommandErrored += CommandErrored;
Executor.CommandExecuted += CommandExecuted;
Executor.RegisterCommands<ScopeCommands>();
Executor.Execute("Test");
Executor.RegisterCommands<Commands>();
Executor.RegisterCommands<SpecialCommands>();
Executor.Execute("ping hello true");
//Executor.Execute("ping");
Executor.Execute("args 3 5 2 asdf");
Executor.Execute("inf inf this is inf arg test");
Executor.Execute("nonparam");
Executor.Execute("private");
Executor.Execute("non");
Executor.Execute("false");
Executor.Execute("exception");
}
public void CommandErrored(object sender, CommandExceptionEventArgs e)
{
Console.WriteLine($"{e.InnerException.GetType()} : {e.InnerCommand.Name} failed!");
}
public void CommandExecuted(object sender, CommandEventArgs e)
{
Console.Write($"{e.Command.Name} : ");
}
}
public class ScopeCommands : CommandModule
{
public override void BeforeExecute() =>
Console.WriteLine("Before");
public override void AfterExecute() =>
Console.WriteLine("After");
public override bool CheckExecute(Command cmd) {
Console.WriteLine(cmd.Name); return true;
}
[Command("Test")/*, CustomAttribute*/]
public void Test() => Console.WriteLine("GOOD!");
}
public class SpecialCommands : CommandModule
{
public override bool CheckExecute(Command cmd)
{
if (cmd.Name == "False") return false;
return true;
}
[Command]
public void Non()
{
Console.WriteLine("Execute check passed.");
}
[Command]
public void False()
{
Console.WriteLine("BUG");
}
[Command]
public void Exception()
{
throw new InvalidOperationException();
}
}
public class Commands : CommandModule
{
[Command("Ping")]
public void Ping(string s, bool t)
{
Console.WriteLine(t ? s : "null");
}
[Command("args")]
public void Arg([ArgumentsCount(3)] int[] i, string s)
{
Console.WriteLine("[" + string.Join(',', i) + "]");
Console.WriteLine(s);
}
[Command("in", new string[] {"inf", "ina"})]
public void Inf(string s, [ArgumentsCount(CountOption.Infinity)] string args)
{
Console.WriteLine(s);
Console.WriteLine(string.Concat(args));
}
[Command]
public void NonParam()
{
Console.WriteLine("This is method that has no parameters");
}
[Command(true)]
private void Private()
{
Console.WriteLine("This is private method");
}
}
}