-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGS.System.CommandLineParser.pas
More file actions
262 lines (227 loc) · 6.82 KB
/
GS.System.CommandLineParser.pas
File metadata and controls
262 lines (227 loc) · 6.82 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
///*******************************************************
///
/// JsonLibsTests
///
/// 2026-2027 Grid System SAS
///
///
///*******************************************************
unit GS.System.CommandLineParser;
{$IFDEF FPC}
{$MODE Delphi}
{$ENDIF}
interface
{$IFNDEF FPC}
{$IF CompilerVersion >= 25}
{$LEGACYIFEND ON}
{$IFEND}
{$ENDIF}
uses
{$IFNDEF FPC}
{$IF CompilerVersion >= 23}
System.Classes,
System.Contnrs,
System.SysUtils,
System.StrUtils,
System.Generics.Collections;
{$ELSE}
Classes,
Contnrs,
SysUtils,
StrUtils,
Generics.Collections;
{$IFEND}
{$ELSE}
Classes,
Contnrs,
SysUtils,
StrUtils,
Generics.Collections;
{$ENDIF}
type
EPArameterAlreadyExists = class(Exception);
iGSCommandLine = Interface
procedure registerParameter(paramId : String; ParamsShortcut : TArray<String>; DescriptionHelp : string);
function hasParameter(paramName : string) : boolean;
function getParameter(paramName : string) : string;
function getHelp : string;
function CurrentParameterCount : Integer;
function checksyntax : boolean;
End;
TGSCmdLineParam = record
paramId : string;
shortcutList : string;
Desc : string;
end;
TGSCmdLineManager = class(TInterfacedObject, iGSCommandLine)
FParams : TDictionary<string,TGSCmdLineParam>;
FCMdLine : TArray<string>;
function GetCmdLineStrAsList : String;
public
constructor Create; virtual;
destructor Destroy; override;
procedure registerParameter(paramId : String; ParamsShortcut : TArray<String>; DescriptionHelp : string);
function hasParameter(paramName : string) : boolean;
function getParameter(paramName : string) : string;
function getHelp : string;
function CurrentParameterCount : Integer;
function checksyntax : boolean;
function IsShortCut(aParamName : String; out aParamItem : TGSCmdLineParam) : Boolean;
function IsShortCutInSameParam(aParam1, aParam2 : String; out aParamItem : TGSCmdLineParam) : Boolean;
end;
function gsCommandLineParser : iGSCommandLine;
/// Retire uniquement le prefixe '--' ou '-' d'un argument CLI.
/// Anciennement Replace('-',''), qui supprimait aussi les tirets internes
/// et empechait l'usage de noms d'options comme 'verbose-light' ou 'dry-run'.
function StripParamPrefix(const S : string) : string;
implementation
function gsCommandLineParser : iGSCommandLine;
begin
result := TGSCmdLineManager.Create;
end;
function StripParamPrefix(const S : string) : string;
begin
Result := S;
if Result.StartsWith('--') then
Result := Result.Substring(2)
else if Result.StartsWith('-') then
Result := Result.Substring(1);
end;
{ TGSCmdLineManager }
function TGSCmdLineManager.checksyntax: boolean;
var lp : TGSCmdLineParam;
begin
result := true;
if length(FCMdLine)>0 then
result := IsShortCut(FCMDLine[0],lp);
end;
constructor TGSCmdLineManager.Create;
begin
inherited;
FParams := TDictionary<string,TGSCmdLineParam>.create;
FCmdLine := GetCmdLineStrAsList.split([' ']);
end;
function TGSCmdLineManager.CurrentParameterCount: Integer;
begin
result := length(FCMdLine);
end;
destructor TGSCmdLineManager.Destroy;
begin
FreeAndNil(FParams);
inherited;
end;
function TGSCmdLineManager.GetCmdLineStrAsList: String;
var i: Integer;
begin
for i := 1 to ParamCount do
Result := Result + ParamStr(I) + ' ';
Result := Result.Trim;
end;
function TGSCmdLineManager.getHelp: string;
var l : TPair<string,TGSCmdLineParam>;
lsc : string;
lscAlternate : string;
lscl : TArray<string>;
i : integer;
begin
if FParams.Count=0 then
exit;
for l in FParams do begin
lsc := l.Value.paramId;
lscl := l.Value.shortcutList.Split([' ',',',';']);
if length(lscl)>1 then
for i := Low(lscl) to High(lscl) do
lscAlternate := lscAlternate + '-'+lscl[i]+ ' --'+lscl[i]+' ';
lscAlternate := lscAlternate.Trim;
writeln(format('- %s [%s])',[lsc,lscAlternate]));
writeln(format(' %s',[l.Value.Desc]));
writeln;
lscAlternate := '';
end;
end;
function TGSCmdLineManager.getParameter(paramName: string): string;
var l : TArray<String>;
i,j: Integer;
lf : string;
aParam : TGSCmdLineParam;
begin
l := FCmdLine;
for i := 0 to Length(l)-1 do
if (l[i].StartsWith('-')) or (l[i].StartsWith('--')) then begin
lf := StripParamPrefix(l[i]);
if (lf = paramName) or IsShortCutInSameParam(paramname,lf,aParam) then begin
if i<length(l)-1 then
for j := i+1 to length(l)-1 do begin
if (l[j].StartsWith('-')) or (l[j].StartsWith('--')) then
break;
result := result +l[j]+' ';
end;
end;
end;
result := result.Trim;
end;
function TGSCmdLineManager.hasParameter(paramName: string): boolean;
var l : TArray<String>;
i: Integer;
lf : string;
aParam : TGSCmdLineParam;
begin
result := False;
l := FCmdLine;
for i := 0 to Length(l)-1 do
if (l[i].StartsWith('-')) or (l[i].StartsWith('--')) then begin
lf := StripParamPrefix(l[i]);
if (lf = paramName) or IsShortCutInSameParam(paramname,lf,aParam) then begin //direct parameter Or registered one.
result := true;
break;
end;
end;
end;
function TGSCmdLineManager.IsShortCut(aParamName: String;
out aParamItem: TGSCmdLineParam): Boolean;
var l : TPair<string,TGSCmdLineParam>;
lsc : TArray<string>;
lp : string;
i : integer;
begin
result := false;
if aParamName.Trim='' then
Exit;
lp := aParamName.ToLower;
lp := StripParamPrefix(lp);
for l in FParams do begin
lsc := l.Value.shortcutList.Split([' ',',',';']);
if length(lsc)>0 then
for i := Low(lsc) to High(lsc) do
if lsc[i].ToLower = lp then begin
result := true;
aParamItem := l.Value;
exit;
end;
end;
end;
function TGSCmdLineManager.IsShortCutInSameParam(aParam1, aParam2: String;
out aParamItem: TGSCmdLineParam): Boolean;
var a,b : TGSCmdLineParam;
begin
result := IsShortCut(aParam1,a) and IsShortCut(aParam2,b) and (a.paramId = b.paramId);
end;
procedure TGSCmdLineManager.registerParameter(paramId: String;
ParamsShortcut: TArray<String>; DescriptionHelp: string);
var lp : TGSCmdLineParam;
i : integer;
begin
if FParams.TryGetValue(paramId,lp) then
raise EPArameterAlreadyExists.Create(paramId);
for i := Low(ParamsShortcut) to High(ParamsShortcut) do
if (IsShortCut(ParamsShortcut[i],lp)) Or (ParamsShortcut[i].ToLower = paramId.ToLower) then
raise Exception.Create(ParamsShortcut[i]+' is already shortcuts of '+paramId.ToLower);
lp.paramId := paramId;
for i := Low(ParamsShortcut) to High(ParamsShortcut) do
lp.shortcutList := lp.shortcutList + ParamsShortcut[i]+' ';
lp.shortcutList := lp.shortcutList + paramId;
lp.shortcutList := lp.shortcutList.Trim;
lp.Desc := DescriptionHelp;
FParams.Add(paramId,lp);
end;
end.