-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppBuilder.cs
306 lines (249 loc) · 10.4 KB
/
AppBuilder.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
using System.CommandLine;
using System.CommandLine.Binding;
using System.CommandLine.Builder;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
class ProxyHandler(ICommandHandler handler, Func<InvocationContext, ICommandHandler, Task<int>> proxy) : ICommandHandler
{
public int Invoke(InvocationContext context) => throw new NotImplementedException();
public Task<int> InvokeAsync(InvocationContext ctx) => proxy(ctx, handler);
}
class AppBuilder
{
Func<ICommandHandler, ICommandHandler> _wrapHandler = h => h;
Func<CancellationToken> _createCancellation = () => CancellationToken.None;
RootCommand _root;
public AppBuilder()
{
_root = new RootCommand();
}
public AppBuilder Name(string name, string description)
{
_root.Name = name;
_root.Description = description;
return this;
}
public AppBuilder GlobalHandler<T1>(Func<InvocationContext, ICommandHandler, T1, Task<int>> proxy, Option<T1> d1)
{
_root.AddGlobalOption(d1);
_wrapHandler = handler => new ProxyHandler(handler, (ctx, handler) =>
{
var v1 = ctx.ParseResult.GetValueForOption(d1)!;
return proxy(ctx, handler, v1);
});
return this;
}
public AppBuilder GlobalHandler<T1, T2>(Func<InvocationContext, ICommandHandler, T1, T2, Task<int>> proxy, Option<T1> d1, Option<T2> d2)
{
_root.AddGlobalOption(d1);
_root.AddGlobalOption(d2);
_wrapHandler = handler => new ProxyHandler(handler, (ctx, handler) =>
{
var v1 = ctx.ParseResult.GetValueForOption(d1)!;
var v2 = ctx.ParseResult.GetValueForOption(d2)!;
return proxy(ctx, handler, v1, v2);
});
return this;
}
public AppBuilder GlobalHandler<T1, T2, T3>(Func<InvocationContext, ICommandHandler, T1, T2, T3, Task<int>> proxy, Option<T1> d1, Option<T2> d2, Option<T3> d3)
{
_root.AddGlobalOption(d1);
_root.AddGlobalOption(d2);
_root.AddGlobalOption(d3);
_wrapHandler = handler => new ProxyHandler(handler, (ctx, handler) =>
{
var v1 = ctx.ParseResult.GetValueForOption(d1)!;
var v2 = ctx.ParseResult.GetValueForOption(d2)!;
var v3 = ctx.ParseResult.GetValueForOption(d3)!;
return proxy(ctx, handler, v1, v2, v3);
});
return this;
}
public AppBuilder GlobalHandler<T1, T2, T3, T4>(Func<InvocationContext, ICommandHandler, T1, T2, T3, T4, Task<int>> proxy, Option<T1> d1, Option<T2> d2, Option<T3> d3, Option<T4> d4)
{
_root.AddGlobalOption(d1);
_root.AddGlobalOption(d2);
_root.AddGlobalOption(d3);
_root.AddGlobalOption(d4);
_wrapHandler = handler => new ProxyHandler(handler, (ctx, handler) =>
{
var v1 = ctx.ParseResult.GetValueForOption(d1)!;
var v2 = ctx.ParseResult.GetValueForOption(d2)!;
var v3 = ctx.ParseResult.GetValueForOption(d3)!;
var v4 = ctx.ParseResult.GetValueForOption(d4)!;
return proxy(ctx, handler, v1, v2, v3, v4);
});
return this;
}
public AppBuilder GlobalHandler<T1, T2, T3, T4, T5>(Func<InvocationContext, ICommandHandler, T1, T2, T3, T4, T5, Task<int>> proxy, Option<T1> d1, Option<T2> d2, Option<T3> d3, Option<T4> d4, Option<T5> d5)
{
_root.AddGlobalOption(d1);
_root.AddGlobalOption(d2);
_root.AddGlobalOption(d3);
_root.AddGlobalOption(d4);
_root.AddGlobalOption(d5);
_wrapHandler = handler => new ProxyHandler(handler, (ctx, handler) =>
{
var v1 = ctx.ParseResult.GetValueForOption(d1)!;
var v2 = ctx.ParseResult.GetValueForOption(d2)!;
var v3 = ctx.ParseResult.GetValueForOption(d3)!;
var v4 = ctx.ParseResult.GetValueForOption(d4)!;
var v5 = ctx.ParseResult.GetValueForOption(d5)!;
return proxy(ctx, handler, v1, v2, v3, v4, v5);
});
return this;
}
public AppBuilder Cancellation(Func<CancellationToken> createCancellation)
{
_createCancellation = createCancellation;
return this;
}
public AppBuilder Command(Action<CommandBuilder> action)
{
var cmd = new Command(".");
_root.AddCommand(cmd);
var builder = new CommandBuilder(cmd, _wrapHandler, _createCancellation);
action(builder);
return this;
}
public Parser Build()
{
return new CommandLineBuilder(_root)
.UseVersionOption()
.UseHelp()
.UseEnvironmentVariableDirective()
.UseParseDirective()
.UseSuggestDirective()
.RegisterWithDotnetSuggest()
.UseTypoCorrections()
.UseParseErrorReporting()
.Build();
}
}
class CommandBuilder
{
Command _cmd;
Func<ICommandHandler, ICommandHandler> _wrapHandler;
Func<CancellationToken> _createCancellation;
public CommandBuilder(Command cmd, Func<ICommandHandler, ICommandHandler> wrapHandler, Func<CancellationToken> createCancellation)
{
_cmd = cmd;
_wrapHandler = wrapHandler;
_createCancellation = createCancellation;
}
public CommandBuilder Name(string name, string description)
{
_cmd.Name = name;
_cmd.Description = description;
return this;
}
public CommandBuilder Alias(string alias)
{
_cmd.AddAlias(alias);
return this;
}
public CommandBuilder Around(Func<InvocationContext, ICommandHandler, Task<int>> proxy)
{
var prevWrap = _wrapHandler;
var newWrap = (ICommandHandler handler) => new ProxyHandler(handler, proxy);
_wrapHandler = handler => prevWrap(newWrap(handler));
return this;
}
public CommandBuilder Handler(Func<CancellationToken, Task> handler)
{
_cmd.SetHandler(() => handler(_createCancellation()));
_cmd.Handler = _wrapHandler(_cmd.Handler!);
return this;
}
public CommandBuilder Handler<T1>(Func<T1, CancellationToken, Task> handler, IValueDescriptor<T1> d1)
{
var ds = new IValueDescriptor[] { d1 };
AddDescriptors(ds);
_cmd.SetHandler((T1 v1) => handler(v1, _createCancellation()), d1);
_cmd.Handler = _wrapHandler(_cmd.Handler!);
return this;
}
public CommandBuilder Handler<T1, T2>(Func<T1, T2, CancellationToken, Task> handler, IValueDescriptor<T1> d1, IValueDescriptor<T2> d2)
{
var ds = new IValueDescriptor[] { d1, d2 };
AddDescriptors(ds);
_cmd.SetHandler((T1 v1, T2 v2) => handler(v1, v2, _createCancellation()), d1, d2);
_cmd.Handler = _wrapHandler(_cmd.Handler!);
return this;
}
public CommandBuilder Handler<T1, T2, T3>(Func<T1, T2, T3, CancellationToken, Task> handler, IValueDescriptor<T1> d1, IValueDescriptor<T2> d2, IValueDescriptor<T3> d3)
{
var ds = new IValueDescriptor[] { d1, d2, d3 };
AddDescriptors(ds);
_cmd.SetHandler((T1 v1, T2 v2, T3 v3) => handler(v1, v2, v3, _createCancellation()), d1, d2, d3);
_cmd.Handler = _wrapHandler(_cmd.Handler!);
return this;
}
public CommandBuilder Handler<T1, T2, T3, T4>(Func<T1, T2, T3, T4, CancellationToken, Task> handler, IValueDescriptor<T1> d1, IValueDescriptor<T2> d2, IValueDescriptor<T3> d3, IValueDescriptor<T4> d4)
{
var ds = new IValueDescriptor[] { d1, d2, d3, d4 };
AddDescriptors(ds);
_cmd.SetHandler((T1 v1, T2 v2, T3 v3, T4 v4) => handler(v1, v2, v3, v4, _createCancellation()), d1, d2, d3, d4);
_cmd.Handler = _wrapHandler(_cmd.Handler!);
return this;
}
public CommandBuilder Handler<T1, T2, T3, T4, T5>(Func<T1, T2, T3, T4, T5, CancellationToken, Task> handler, IValueDescriptor<T1> d1, IValueDescriptor<T2> d2, IValueDescriptor<T3> d3, IValueDescriptor<T4> d4, IValueDescriptor<T5> d5)
{
var ds = new IValueDescriptor[] { d1, d2, d3, d4, d5 };
AddDescriptors(ds);
_cmd.SetHandler((T1 v1, T2 v2, T3 v3, T4 v4, T5 v5) => handler(v1, v2, v3, v4, v5, _createCancellation()), d1, d2, d3, d4, d5);
_cmd.Handler = _wrapHandler(_cmd.Handler!);
return this;
}
public CommandBuilder Handler<T1, T2, T3, T4, T5, T6>(Func<T1, T2, T3, T4, T5, T6, CancellationToken, Task> handler, IValueDescriptor<T1> d1, IValueDescriptor<T2> d2, IValueDescriptor<T3> d3, IValueDescriptor<T4> d4, IValueDescriptor<T5> d5, IValueDescriptor<T6> d6)
{
var ds = new IValueDescriptor[] { d1, d2, d3, d4, d5, d6 };
AddDescriptors(ds);
_cmd.SetHandler((T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6) => handler(v1, v2, v3, v4, v5, v6, _createCancellation()), d1, d2, d3, d4, d5, d6);
_cmd.Handler = _wrapHandler(_cmd.Handler!);
return this;
}
public CommandBuilder Handler<T1, T2, T3, T4, T5, T6, T7>(Func<T1, T2, T3, T4, T5, T6, T7, CancellationToken, Task> handler, IValueDescriptor<T1> d1, IValueDescriptor<T2> d2, IValueDescriptor<T3> d3, IValueDescriptor<T4> d4, IValueDescriptor<T5> d5, IValueDescriptor<T6> d6, IValueDescriptor<T7> d7)
{
var ds = new IValueDescriptor[] { d1, d2, d3, d4, d5, d6, d7 };
AddDescriptors(ds);
_cmd.SetHandler((T1 v1, T2 v2, T3 v3, T4 v4, T5 v5, T6 v6, T7 v7) => handler(v1, v2, v3, v4, v5, v6, v7, _createCancellation()), d1, d2, d3, d4, d5, d6, d7);
_cmd.Handler = _wrapHandler(_cmd.Handler!);
return this;
}
public CommandBuilder Command(Action<CommandBuilder> action)
{
var cmd = new Command(".");
_cmd.AddCommand(cmd);
var builder = new CommandBuilder(cmd, _wrapHandler, _createCancellation);
action(builder);
return this;
}
void AddDescriptors(IValueDescriptor[] descriptors)
{
foreach (var descriptor in descriptors)
{
if (descriptor is Option o)
{
_cmd.AddOption(o);
}
else if (descriptor is Argument a)
{
_cmd.AddArgument(a);
}
}
}
}
static class OptionExtensions
{
public static Option<T> WithMultipleArguments<T>(this Option<T> option, bool allowMultipleArgumentsPerToken)
{
option.AllowMultipleArgumentsPerToken = allowMultipleArgumentsPerToken;
return option;
}
public static Option<T> WithRequired<T>(this Option<T> option, bool isRequired)
{
option.IsRequired = isRequired;
return option;
}
}