-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMaxLogic.CommandLineActionDispatcher.pas
More file actions
684 lines (596 loc) · 19.5 KB
/
Copy pathMaxLogic.CommandLineActionDispatcher.pas
File metadata and controls
684 lines (596 loc) · 19.5 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
unit MaxLogic.CommandLineActionDispatcher;
{
Version: 1.24
uses and expands on the GpCommandLineParser, see:
https://www.thedelphigeek.com/2014/12/attribute-based-command-line-parsing.html
https://github.com/gabr42/GpDelphiUnits
it is primary intended for CLI tools that accept a command / action as the first parameter which will be followed by options specific to that command
Names used here:
Action - this is the first argument in the command line prompt
$ cmd.exe MyAction -Switch1
Arguments - command line switches or parameters. All except the first which we are calling here the Action
Service - the class that implements the logic needed to execute the action.
This small Framework is inspired by Microservices architecture or the various web server dispatcher
Simply declare a Service like a normal class,
mark the method that should be called when the Action is indeed the first command line argument with the [CLPAction('ActionName')] atribute, like this:
TMyService = class
public
[CLPAction('run,Execute'),
CLPDescription('my action description')]
Procedure Execute(aArgs: TMyArgs);
end;
valid is also a method without any parameters
TMyService = class
public
[CLPAction('run,Execute')]
Procedure Execute;
end;
now register it in the dispatcher
TCLPActionDispatcher .Default.Register(TMyService);
now when you execute your application like this:
$ myAppexe run -arg1 -arg2
or
$ myAppexe execute -arg1 -arg2
the dispatcher will create an instance of TMyArgs,
it will call the gpCommandParser to populate it with values
then the dispatcher will create an instance of TMyService and invoke the execute method of that class passing the TMyArgs as the single parameter
after all is done, both instances will be destroyed
in case your wonder, "CL" stands for Command Line
}
interface
uses
System.SysUtils,
System.TypInfo,
System.RTTI,
System.Classes,
System.Generics.Defaults,
System.Generics.Collections,
GpCommandLineParser;
type
// raw form of the usage information
// use one of the formatters to output it into a different format
TRawCommandHelp = record
Names: TArray<String>; // names of the actions
Description: String;
CommandLineParser: IGpCommandLineParser;
end;
TUsageRawhelp = record
Title: String; // contains exe name + version
Version: String; // contains just the version of the exe
Description: String;
ParamUsageInfo: String;
Commands: TArray<TRawCommandHelp>;
end;
TPlainTextHelpFormatter = class
public
// set aWrapAtColumn=0 to disable line wrapping
class procedure Execute(const aRaw: TUsageRawhelp; aOutputUsage: TStrings; aWrapAtColumn: Integer = 80);
end;
TMarkDownHelpFormatter = class
private
procedure DoExec(const aRaw: TUsageRawhelp; aOutputUsage: TStrings);
function preProcessParamNames(const aValue: String; var aMaxLen: Integer): String;
function preProcessParamDesc(const aValue: String; var aMaxLen: Integer): String;
function writeCell(const aText: String; aChar: Char; aLen: Integer): String;
public
// set aWrapAtColumn=0 to disable line wrapping
class procedure Execute(const aRaw: TUsageRawhelp; aOutputUsage: TStrings);
end;
TCLActionDispatcher = class
private
class var fInstance: TCLActionDispatcher;
Class Constructor CreateClass;
class Destructor DestroyClass;
private type
TActionKind = (akNormal, akSimple);
TCLPActionEntry = class
kind: TActionKind; // should we ever need more then those 2 kinds... then we probably should split this class up inheriting from the same base class, but for now, that is simpler
// comma separated list
ActionNames: String;
MethodName: String;
ArgsParam: TRttiType;
ServiceClass: TClass;
Description: String;
SimpleProc: TProc;
// ActionNames contains a comma separated list of action names,
// where the first is the main name and the others are aliases,
// but sometimes we need just the first one
Function FirstActionName: String;
end;
private
// key is the lowercased action name, there may be multipe same entries here if the action has multiple aliases
fActionDic: TDictionary<String, TCLPActionEntry>;
fActionList: TObjectList<TCLPActionEntry>;
fDescription: String;
Function CreateArgsInstance(aEntry: TCLPActionEntry; out aArgs: Tobject): Boolean;
// if called with an empty aCommandLine it will parse the arguments from the command line, otherwise it wil use the aCommandLine
function DoExecute(const aCommandLine: String = ''): Boolean;
// without the first argumet, so we skip the action itself
Function GetAdjustedCommandLine: String;
public
Constructor Create;
Destructor Destroy; override;
Procedure Register(aServiceClass: TClass); overload;
// for a simpler approach just with a action and a method to be executed
// actionNames is a comma separated list of names for the action, so you can define some aliases for the same command
Procedure Register(const aActionNames: String; aProc: TProc; const aDescription: String = '')overload;
Procedure OutputUsageToConsole;
// set wrapAtColumn=0 to disable line breaks
procedure OutputUsageToStrings(aUsage: TStrings; aWrapAtColumn: Integer = 80);
function OutputUsage: TUsageRawhelp;
Function Execute: Boolean; overload;
Function Execute(const aCommandLine: String): Boolean; overload;
class property Default: TCLActionDispatcher read fInstance;
/// <summary>
/// Description property - provide a description for your command line tool
/// </summary>
property Description: String read fDescription write fDescription;
end;
/// <summary>
/// Command Line Parser Action
/// You have to mark your Action Method with this attribute where the ActionName must be the first CL Argument
/// </summary>
CLPActionAttribute = class(TCustomAttribute)
private
fActionName: String;
public
constructor Create(const aActionName: String);
property ActionName: String read fActionName;
end;
implementation
uses
System.StrUtils,
MaxLogic.RTTIHelper,
MaxLogic.StrUtils,
autoFree,
MaxLogic.ioUtils;
{ TCLPActionDispatcher }
constructor TCLActionDispatcher.Create;
begin
inherited Create;
fActionDic := TDictionary<String, TCLPActionEntry>.Create;
fActionList := TObjectList<TCLPActionEntry>.Create;
end;
function TCLActionDispatcher.CreateArgsInstance(aEntry: TCLPActionEntry;
out aArgs: Tobject): Boolean;
begin
Result := false;
if aEntry.kind <> akNormal then
Exit;
if aEntry.ArgsParam <> nil then
begin
aArgs := TRTTIHelper.CreateClassInstanceFromRttiType(aEntry.ArgsParam);
Result := True;
end;
end;
class constructor TCLActionDispatcher.CreateClass;
begin
fInstance := TCLActionDispatcher.Create;
end;
destructor TCLActionDispatcher.Destroy;
begin
fActionDic.Free;
fActionList.Free;
inherited;
end;
class destructor TCLActionDispatcher.DestroyClass;
begin
FreeAndNil(fInstance);
end;
Function TCLActionDispatcher.Execute: Boolean;
begin
Result := DoExecute;
end;
Function TCLActionDispatcher.DoExecute(const aCommandLine: String = ''): Boolean;
var
lActionName: String;
lEntry: TCLPActionEntry;
cp: IGpCommandLineParser;
lArgs: Tobject;
lService: Tobject;
lCommandLine: String;
i: Integer;
begin
Result := false;
lCommandLine := aCommandLine;
if lCommandLine = '' then
begin
if ParamCount = 0 then
Exit(false);
lActionName := ParamStr(1);
end else begin
i := pos(' ', lCommandLine);
if i < 1 then
begin
lActionName := trim(lCommandLine);
lCommandLine := '';
end else begin
lActionName := trim(copy(lCommandLine, 1, i - 1));
lCommandLine := trim(copy(lCommandLine, i + 1, length(lCommandLine)));
end;
end;
if fActionDic.TryGetValue(ansiLowercase(lActionName), lEntry) then
begin
case lEntry.kind of
akSimple:
begin
if assigned(lEntry.SimpleProc) then
begin
lEntry.SimpleProc();
Result := True;
end;
end;
akNormal:
begin
// init variables, so we know if we need to free them in try finally
lArgs := nil;
lService := nil;
try
// is the action method without parameters or do we need to parse the command line and pass the arguments to the method?
if (lEntry.ArgsParam = nil) then
begin
Result := True;
end else begin
CreateArgsInstance(lEntry, lArgs);
cp := CreateCommandLineParser;
if lCommandLine <> '' then
Result := cp.Parse(lCommandLine, lArgs)
else
Result := cp.Parse(GetAdjustedCommandLine, lArgs);
end;
if Result then
begin
lService := TRTTIHelper.CreateInstanceFromTClass(lEntry.ServiceClass);
if assigned(lArgs) then
TRTTIHelper.Call(lService, lEntry.MethodName, [lArgs])
else
TRTTIHelper.Call(lService, lEntry.MethodName, []);
end;
finally
FreeAndNil(lArgs);
FreeAndNil(lService);
end;
end;
end;
end;
end;
Function TCLActionDispatcher.Execute(const aCommandLine: String): Boolean;
begin
if aCommandLine = '' then
Result := false
else
Result := DoExecute(aCommandLine);
end;
function TCLActionDispatcher.GetAdjustedCommandLine: String;
var
i: Integer;
begin
Result := '';
for i := 2 to ParamCount do
begin
if Result <> '' then
Result := Result + ' ';
if pos(' ', ParamStr(i)) > 0 then
Result := Result + '"' + ParamStr(i) + '"'
else
Result := Result + ParamStr(i);
end;
end;
procedure TCLActionDispatcher.OutputUsageToConsole;
var
l: TStringList;
x: Integer;
begin
gc(l, TStringList.Create);
OutputUsageToStrings(l);
for x := 0 to l.Count - 1 do
writeLn(l[x]);
end;
procedure TCLActionDispatcher.OutputUsageToStrings(aUsage: TStrings; aWrapAtColumn: Integer = 80);
var
raw: TUsageRawhelp;
begin
raw := OutputUsage;
TPlainTextHelpFormatter.Execute(raw, aUsage, aWrapAtColumn);
end;
function TCLActionDispatcher.OutputUsage: TUsageRawhelp;
const
br = sLineBreak;
var
i: Integer;
cp: IGpCommandLineParser;
lEntry: TCLPActionEntry;
ar: TArray<TCLPActionEntry>;
lArgs: Tobject;
begin
Result := System.default(TUsageRawhelp);
lArgs := nil;
Result.Version := MaxLogic.ioUtils.GetVersionString;
Result.Title := ExtractFilename(ParamStr(0)) + ' ' + Result.Version;
Result.Description := Description;
Result.ParamUsageInfo :=
'Parameters can be introduced by `/`, `-` and `--`.' + br +
'Values can be separated from parameter names by `:` or `=`.' + br +
'When a short (single letter) name is used, a separator is not required';
ar := fActionList.ToArray;
TArray.Sort<TCLPActionEntry>(
ar, TComparer<TCLPActionEntry>.Construct(
function(const Left, Right: TCLPActionEntry): Integer
begin
Result := TComparer<String>.default.Compare(Left.FirstActionName, Right.FirstActionName);
end));
setLength(Result.Commands, length(ar));
i := 0;
for lEntry in ar do
begin
Result.Commands[i].Names := lEntry.ActionNames.Split([',']);
Result.Commands[i].Description := lEntry.Description;
case lEntry.kind of
akNormal:
begin
Try
if CreateArgsInstance(lEntry, lArgs) then
begin
cp := CreateCommandLineParser;
cp.Parse(lArgs);
Result.Commands[i].CommandLineParser := cp;
end;
Finally
FreeAndNil(lArgs);
End;
end;
end; // case
inc(i); // next element
end;
end;
procedure TCLActionDispatcher.Register(const aActionNames: String;
aProc: TProc; const aDescription: String = '');
var
lActionName: String;
lEntry: TCLPActionEntry;
begin
lEntry := TCLPActionEntry.Create;
lEntry.ActionNames := aActionNames;
lEntry.Description := aDescription;
lEntry.kind := akSimple;
lEntry.SimpleProc := aProc;
fActionList.Add(lEntry);
for lActionName in lEntry.ActionNames.Split([',']) do
fActionDic.Add(ansiLowercase(lActionName), lEntry)
end;
procedure TCLActionDispatcher.Register(aServiceClass: TClass);
var
lActionAttribute: CLPActionAttribute;
lMethodName: String;
lActionName: String;
lEntry: TCLPActionEntry;
lRttiMethod: TRttiMethod;
lParams: TArray<TRttiParameter>;
lArg: TRttiType;
begin
if not TRTTIHelper.FindMethodByAttribute<CLPActionAttribute>(aServiceClass, lMethodName, lActionAttribute, lRttiMethod) then
raise Exception.Create(ClassName + '.Register: ' + aServiceClass.ClassName + ' must have the ' + CLPActionAttribute.ClassName + ' attribute');
lParams := lRttiMethod.GetParameters;
case length(lParams) of
0:
lArg := nil;
1:
begin
if lParams[0].ParamType.TypeKind <> tkClass then
raise Exception.Create(ClassName + '.Register: Invalid Parameter type for ' + aServiceClass.ClassName + '.' + lMethodName + '. It may have either no parameters or one parameter descending from TObject');
lArg := lParams[0].ParamType;
end;
else
raise Exception.Create(ClassName + '.Register: Invalid number of Parameters for ' + aServiceClass.ClassName + '.' + lMethodName + '. It may have either no parameters or one parameter descending from TObject');
end;
lEntry := TCLPActionEntry.Create;
lEntry.kind := akNormal;
if lRttiMethod.hasAttribute<CLPDescriptionAttribute> Then
lEntry.Description := lRttiMethod.GetAttribute<CLPDescriptionAttribute>.Description;
lEntry.ActionNames := lActionAttribute.ActionName;
lEntry.ServiceClass := aServiceClass;
lEntry.MethodName := lMethodName;
lEntry.ArgsParam := lArg;
fActionList.Add(lEntry);
for lActionName in lEntry.ActionNames.Split([',']) do
begin
fActionDic.Add(ansiLowercase(trim(lActionName)), lEntry);
end;
end;
{ CLPActionAttribute }
constructor CLPActionAttribute.Create(const aActionName: String);
begin
inherited Create;
fActionName := aActionName;
end;
{ TCLPActionDispatcher.TCLPActionEntry }
function TCLActionDispatcher.TCLPActionEntry.FirstActionName: String;
begin
if ActionNames = '' then
Result := ''
else
Result := ActionNames.Split([','], 1)[0];
end;
{ TPlainTextHelpFormatter }
class procedure TPlainTextHelpFormatter.Execute(const aRaw: TUsageRawhelp;
aOutputUsage: TStrings; aWrapAtColumn: Integer);
const
br = sLineBreak;
var
l: TStrings;
lCommand: TRawCommandHelp;
cp: IGpCommandLineParser;
lFirst: Boolean;
s: String;
begin
l := aOutputUsage;
l.Add(aRaw.Title + br);
if aRaw.Description <> '' then
l.Add(aRaw.Description + br);
l.Add(aRaw.ParamUsageInfo + br);
for lCommand in aRaw.Commands do
begin
if length(lCommand.Names) <> 0 then
begin
l.Add('Action: ' + lCommand.Names[0]);
if length(lCommand.Names) > 1 then
begin
l.Add('Action aliases: ' +
String.join(', ', lCommand.Names, 2, length(lCommand.Names) - 1));
end;
end;
if lCommand.Description <> '' then
l.Add('Description: ' + lCommand.Description);
cp := lCommand.CommandLineParser;
if cp <> nil then
begin
lFirst := True;
if aWrapAtColumn <> 0 then
dec(aWrapAtColumn, 2); // actually we are adding an additional ident of 2 spaces, so we need to ask the parser to take this into account and break 2 chars erlier
for s in cp.Usage(aWrapAtColumn) do
begin
if lFirst then // we want skip the First item in the list of the cp.Usage list
begin
l.Add('Parameters / Options:');
lFirst := false;
end else if s <> '' then
l.Add(' ' + s);
end;
end;
l.Add(''); // leave empty line between the commands
end;
end;
{ TMarkDownHelpFormatter }
class procedure TMarkDownHelpFormatter.Execute(const aRaw: TUsageRawhelp;
aOutputUsage: TStrings);
var
lFormater: TMarkDownHelpFormatter;
begin
gc(lFormater, TMarkDownHelpFormatter.Create);
lFormater.DoExec(aRaw, aOutputUsage);
end;
function TMarkDownHelpFormatter.preProcessParamDesc(const aValue: String;
var aMaxLen: Integer): String;
var
ar: TArray<String>;
s: String;
x: Integer;
lLen: Integer;
begin
s := aValue;
s := StringReplace(s, sLineBreak, #10, [rfReplaceAll]);
ar := s.Split([#10]);
for x := 0 to length(ar) - 1 do
begin
lLen := length(ar[x]);
if (x <> length(ar) - 1) then
ar[x] := trim(ar[x]) + '<br>';
if lLen > aMaxLen then
aMaxLen := lLen;
end;
Result := String.join('', ar);
end;
function TMarkDownHelpFormatter.preProcessParamNames(const aValue: String;
var aMaxLen: Integer): String;
var
ar: TArray<String>;
lIsOptional: Boolean;
s: String;
x: Integer;
begin
s := aValue;
lIsOptional := false;
if s[1] = '[' then
begin
lIsOptional := True;
s := copy(s, 2, length(s) - 2);
end;
ar := s.Split([',']);
for x := 0 to length(ar) - 1 do
ar[x] := '`' + trim(ar[x]) + '`';
Result := String.join(', ', ar);
if lIsOptional then
Result := '[' + Result + ']';
if length(Result) > aMaxLen then
aMaxLen := length(Result);
end;
function TMarkDownHelpFormatter.writeCell(const aText: String; aChar: Char;
aLen: Integer): String;
var
i: Integer;
begin
Result := aText;
i := aLen - length(Result);
if i > 0 then
Result := Result + StringofChar(aChar, i);
end;
procedure TMarkDownHelpFormatter.DoExec(const aRaw: TUsageRawhelp; aOutputUsage: TStrings);
const
br = sLineBreak;
var
l: TStrings;
x, i: Integer;
lFirst: Boolean;
lCommand: TRawCommandHelp;
cp: IGpCommandLineParser;
s: String;
lFormatter: TMarkDownHelpFormatter;
lRows: TList<TArray<String>>;
lRow: TArray<String>;
lMaxParamNameLen, lMaxParamDescLineLen: Integer;
begin
gc(lRows, TList < TArray < String >>.Create);
l := aOutputUsage;
l.Add('# ' + aRaw.Title + br);
if aRaw.Description <> '' then
l.Add(aRaw.Description + br);
l.Add(aRaw.ParamUsageInfo + br);
for lCommand in aRaw.Commands do
begin
if length(lCommand.Names) <> 0 then
begin
l.Add('## ' + lCommand.Names[0]);
if length(lCommand.Names) > 1 then
l.Add('Action aliases: ' + String.join(', ', lCommand.Names, 1, length(lCommand.Names) - 1) + br);
end;
if lCommand.Description <> '' then
l.Add(lCommand.Description);
lRows.Clear;
cp := lCommand.CommandLineParser;
if cp <> nil then
begin
lFirst := True;
for s in cp.Usage(0) do
begin
if lFirst then // we want skip the First item in the list of the cp.Usage list
begin
lFirst := false;
lRows.Add(['Parameter', 'Description']);
lMaxParamNameLen := length(lRows[0][0]);
lMaxParamDescLineLen := length(lRows[0][1]);
continue;
end;
if s <> '' then
begin
i := pos(' - ', s);
lRows.Add([
preProcessParamNames(trim(copy(s, 1, i - 1)), lMaxParamNameLen),
preProcessParamDesc(trim(copy(s, i + 3, length(s))), lMaxParamDescLineLen)]);
end;
end;
// now write them
l.Add('');
for x := 0 to lRows.Count - 1 do
begin
lRow := lRows[x];
s := '| ' + writeCell(lRow[0], ' ', lMaxParamNameLen) + ' | ' + writeCell(lRow[1], ' ', lMaxParamDescLineLen) + ' |';
l.Add(s);
if x = 0 then
l.Add(
'| ' + writeCell('---', '-', lMaxParamNameLen) + ' | ' + writeCell('---', '-', lMaxParamDescLineLen) + ' |');
end;
l.Add(''); // leave empty line between the commands
end;
end;
end;
end.