| 1 |
{ |
| 2 |
This file is part of the Free Pascal run time library. |
| 3 |
Copyright (c) 2003 by the Free Pascal development team |
| 4 |
|
| 5 |
CustomApplication class. |
| 6 |
|
| 7 |
See the file COPYING.FPC, included in this distribution, |
| 8 |
for details about the copyright. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
**********************************************************************} |
| 15 |
|
| 16 |
{$I castleconf.inc} // hide useless Delphi hints/warnings |
| 17 |
|
| 18 |
{$h+} |
| 19 |
unit CustApp; |
| 20 |
|
| 21 |
Interface |
| 22 |
|
| 23 |
uses SysUtils,Classes, CastleUtils, singleinstance; |
| 24 |
|
| 25 |
Type |
| 26 |
TStringArray = Array of string; |
| 27 |
TExceptionEvent = Procedure (Sender : TObject; E : Exception) Of Object; |
| 28 |
TEventLogTypes = Set of TEventType; |
| 29 |
|
| 30 |
{ TCustomApplication } |
| 31 |
|
| 32 |
TCustomApplication = Class(TComponent) |
| 33 |
Private |
| 34 |
FEventLogFilter: TEventLogTypes; |
| 35 |
FOnException: TExceptionEvent; |
| 36 |
FSingleInstance: TBaseSingleInstance; |
| 37 |
FSingleInstanceClass: TBaseSingleInstanceClass; // set before FSingleInstance is created |
| 38 |
FSingleInstanceEnabled: Boolean; // set before Initialize is called |
| 39 |
FTerminated : Boolean; |
| 40 |
FHelpFile, |
| 41 |
FTitle : String; |
| 42 |
FOptionChar : Char; |
| 43 |
FCaseSensitiveOptions : Boolean; |
| 44 |
FStopOnException : Boolean; |
| 45 |
FExceptionExitCode : Integer; |
| 46 |
function GetEnvironmentVar(VarName : String): String; |
| 47 |
function GetExeName: string; |
| 48 |
Function GetLocation : String; |
| 49 |
function GetSingleInstance: TBaseSingleInstance; |
| 50 |
procedure SetSingleInstanceClass( |
| 51 |
const ASingleInstanceClass: TBaseSingleInstanceClass); |
| 52 |
function GetTitle: string; |
| 53 |
Protected |
| 54 |
function GetOptionAtIndex(AIndex: Integer; IsLong: Boolean): String; |
| 55 |
procedure SetTitle(const AValue: String); Virtual; |
| 56 |
Function GetConsoleApplication : boolean; Virtual; |
| 57 |
Procedure DoRun; Virtual; |
| 58 |
Function GetParams(Index : Integer) : String;virtual; |
| 59 |
function GetParamCount: Integer;Virtual; |
| 60 |
Procedure DoLog(EventType : TEventType; const Msg : String); virtual; |
| 61 |
Public |
| 62 |
constructor Create(AOwner: TComponent); override; |
| 63 |
destructor Destroy; override; |
| 64 |
// Some Delphi methods. |
| 65 |
procedure HandleException(Sender: TObject); virtual; |
| 66 |
procedure Initialize; virtual; |
| 67 |
procedure Run; |
| 68 |
procedure ShowException(E: Exception);virtual; |
| 69 |
procedure Terminate; overload; virtual; |
| 70 |
procedure Terminate(AExitCode : Integer); overload; virtual; |
| 71 |
// Extra methods. |
| 72 |
function FindOptionIndex(Const S : String; Var Longopt : Boolean; StartAt : Integer = -1) : Integer; |
| 73 |
Function GetOptionValue(Const S : String) : String; overload; |
| 74 |
Function GetOptionValue(Const C: Char; Const S : String) : String; overload; |
| 75 |
Function GetOptionValues(Const C: Char; Const S : String) : TStringArray; |
| 76 |
Function HasOption(Const S : String) : Boolean; overload; |
| 77 |
Function HasOption(Const C : Char; Const S : String) : Boolean; overload; |
| 78 |
Function CheckOptions(Const ShortOptions : String; Const Longopts : TStrings; Opts,NonOpts : TStrings; AllErrors : Boolean = False) : String; overload; |
| 79 |
Function CheckOptions(Const ShortOptions : String; Const Longopts : Array of string; Opts,NonOpts : TStrings; AllErrors : Boolean = False) : String; overload; |
| 80 |
Function CheckOptions(Const ShortOptions : String; Const Longopts : TStrings; AllErrors : Boolean = False) : String; overload; |
| 81 |
Function CheckOptions(Const ShortOptions : String; Const LongOpts : Array of string; AllErrors : Boolean = False) : String; overload; |
| 82 |
Function CheckOptions(Const ShortOptions : String; Const LongOpts : String; AllErrors : Boolean = False) : String; overload; |
| 83 |
Function GetNonOptions(Const ShortOptions : String; Const Longopts : Array of string) : TStringArray; overload; |
| 84 |
Procedure GetNonOptions(Const ShortOptions : String; Const Longopts : Array of string; NonOptions : TStrings); overload; |
| 85 |
Procedure GetEnvironmentList(List : TStrings;NamesOnly : Boolean); overload; |
| 86 |
Procedure GetEnvironmentList(List : TStrings); overload; |
| 87 |
Procedure Log(EventType : TEventType; const Msg : String); overload; |
| 88 |
Procedure Log(EventType : TEventType; const Fmt : String; const Args : array of const); overload; |
| 89 |
// Delphi properties |
| 90 |
property ExeName: string read GetExeName; |
| 91 |
property HelpFile: string read FHelpFile write FHelpFile; |
| 92 |
property Terminated: Boolean read FTerminated; |
| 93 |
property Title: string read FTitle write SetTitle; |
| 94 |
property OnException: TExceptionEvent read FOnException write FOnException; |
| 95 |
// Extra properties |
| 96 |
Property ConsoleApplication : Boolean Read GetConsoleApplication; |
| 97 |
Property Location : String Read GetLocation; |
| 98 |
Property Params [Index : integer] : String Read GetParams; |
| 99 |
Property ParamCount : Integer Read GetParamCount; |
| 100 |
Property EnvironmentVariable[envName : String] : String Read GetEnvironmentVar; |
| 101 |
Property OptionChar : Char Read FoptionChar Write FOptionChar; |
| 102 |
Property CaseSensitiveOptions : Boolean Read FCaseSensitiveOptions Write FCaseSensitiveOptions; |
| 103 |
Property StopOnException : Boolean Read FStopOnException Write FStopOnException; |
| 104 |
Property ExceptionExitCode : Integer Read FExceptionExitCode Write FExceptionExitCode; |
| 105 |
Property EventLogFilter : TEventLogTypes Read FEventLogFilter Write FEventLogFilter; |
| 106 |
Property SingleInstance: TBaseSingleInstance read GetSingleInstance; |
| 107 |
Property SingleInstanceClass: TBaseSingleInstanceClass read FSingleInstanceClass write SetSingleInstanceClass; |
| 108 |
Property SingleInstanceEnabled: Boolean read FSingleInstanceEnabled write FSingleInstanceEnabled; |
| 109 |
end; |
| 110 |
|
| 111 |
var CustomApplication : TCustomApplication = nil; |
| 112 |
|
| 113 |
Implementation |
| 114 |
|
| 115 |
{$ifdef darwin} |
| 116 |
uses |
| 117 |
MacOSAll; |
| 118 |
{$endif} |
| 119 |
|
| 120 |
{ TCustomApplication } |
| 121 |
|
| 122 |
function TCustomApplication.GetExeName: string; |
| 123 |
{$if defined(darwin)} |
| 124 |
var |
| 125 |
mainBundle: CFBundleRef; |
| 126 |
executableUrl: CFURLRef; |
| 127 |
executableFSPath: CFStringRef; |
| 128 |
utf16len: ptrint; |
| 129 |
error: boolean; |
| 130 |
begin |
| 131 |
error:=false; |
| 132 |
{ Get main bundle. This even works most of the time for command line |
| 133 |
applications |
| 134 |
} |
| 135 |
mainbundle:=CFBundleGetMainBundle; |
| 136 |
if assigned(mainbundle) then |
| 137 |
begin |
| 138 |
{ get the URL pointing to the executable of the bundle } |
| 139 |
executableUrl:=CFBundleCopyExecutableURL(mainBundle); |
| 140 |
if assigned(executableUrl) then |
| 141 |
begin |
| 142 |
{ convert the url to a POSIX path } |
| 143 |
executableFSPath:=CFURLCopyFileSystemPath(executableUrl,kCFURLPOSIXPathStyle); |
| 144 |
CFRelease(executableUrl); |
| 145 |
{ convert to UTF-8 -- this is not really clean since in theory the |
| 146 |
ansi-encoding could be different, but |
| 147 |
a) all file i/o routines on Darwin expect utf-8-encoded strings |
| 148 |
b) there is no easy way to convert the Unix LANG encoding |
| 149 |
setting to an equivalent CoreFoundation encoding |
| 150 |
} |
| 151 |
utf16len:=CFStringGetLength(executableFSPath); |
| 152 |
// +1 for extra terminating #0 in the worst case, so the pos below |
| 153 |
// will always find the #0 |
| 154 |
setlength(result,utf16len*3+1); |
| 155 |
if CFStringGetCString(executableFSPath,@result[1],length(result),kCFStringEncodingUTF8) then |
| 156 |
{ truncate to actual length, #0 cannot appear in a file path } |
| 157 |
setlength(result,pos(#0,result)-1) |
| 158 |
else |
| 159 |
error:=true; |
| 160 |
CFRelease(executableFSPath); |
| 161 |
end |
| 162 |
else |
| 163 |
error:=true; |
| 164 |
end |
| 165 |
else |
| 166 |
error:=true; |
| 167 |
if error then |
| 168 |
{ can't do better than this } |
| 169 |
Result:=Paramstr(0); |
| 170 |
end; |
| 171 |
{$else darwin} |
| 172 |
begin |
| 173 |
Result:=Paramstr(0); |
| 174 |
end; |
| 175 |
{$endif darwin} |
| 176 |
|
| 177 |
Procedure SysGetEnvironmentList(List : TStrings;NamesOnly : Boolean); |
| 178 |
{var |
| 179 |
s : string; |
| 180 |
i,l,j,count : Integer;} |
| 181 |
begin |
| 182 |
raise Exception.Create('Not implemented!'); |
| 183 |
{count:=GetEnvironmentVariableCount; |
| 184 |
if count>0 then |
| 185 |
for j:=1 to count do |
| 186 |
begin |
| 187 |
s:=GetEnvironmentString(j); |
| 188 |
l:=Length(s); |
| 189 |
If NamesOnly then |
| 190 |
begin |
| 191 |
I:=pos('=',s); |
| 192 |
If (I>0) then |
| 193 |
S:=Copy(S,1,I-1); |
| 194 |
end; |
| 195 |
List.Add(S); |
| 196 |
end;} |
| 197 |
end; |
| 198 |
|
| 199 |
function TCustomApplication.GetEnvironmentVar(VarName : String): String; |
| 200 |
begin |
| 201 |
Result:=GetEnvironmentVariable(VarName); |
| 202 |
end; |
| 203 |
|
| 204 |
procedure TCustomApplication.GetEnvironmentList(List: TStrings; |
| 205 |
NamesOnly: Boolean); |
| 206 |
|
| 207 |
begin |
| 208 |
// Routine must be in custapp.inc |
| 209 |
SysGetEnvironmentList(List,NamesOnly); |
| 210 |
end; |
| 211 |
|
| 212 |
procedure TCustomApplication.GetEnvironmentList(List: TStrings); |
| 213 |
|
| 214 |
begin |
| 215 |
GetEnvironmentList(List,False); |
| 216 |
end; |
| 217 |
|
| 218 |
function TCustomApplication.GetLocation: String; |
| 219 |
begin |
| 220 |
Result:=ExtractFilePath(GetExeName); |
| 221 |
end; |
| 222 |
|
| 223 |
function TCustomApplication.GetParamCount: Integer; |
| 224 |
begin |
| 225 |
Result:=System.ParamCount; |
| 226 |
end; |
| 227 |
|
| 228 |
function TCustomApplication.GetTitle: string; |
| 229 |
begin |
| 230 |
Result:=FTitle; |
| 231 |
end; |
| 232 |
|
| 233 |
function TCustomApplication.GetParams(Index: Integer): String; |
| 234 |
begin |
| 235 |
Result:=ParamStr(Index); |
| 236 |
end; |
| 237 |
|
| 238 |
function TCustomApplication.GetSingleInstance: TBaseSingleInstance; |
| 239 |
begin |
| 240 |
if FSingleInstance = nil then |
| 241 |
begin |
| 242 |
if FSingleInstanceClass=Nil then |
| 243 |
Raise ESingleInstance.Create('No single instance provider class set! Include a single-instance class unit such as advsingleinstance'); |
| 244 |
FSingleInstance := FSingleInstanceClass.Create(Self); |
| 245 |
end; |
| 246 |
Result := FSingleInstance; |
| 247 |
end; |
| 248 |
|
| 249 |
procedure TCustomApplication.SetTitle(const AValue: String); |
| 250 |
begin |
| 251 |
FTitle:=AValue; |
| 252 |
end; |
| 253 |
|
| 254 |
function TCustomApplication.GetConsoleApplication: boolean; |
| 255 |
begin |
| 256 |
Result:=IsConsole; |
| 257 |
end; |
| 258 |
|
| 259 |
procedure TCustomApplication.DoRun; |
| 260 |
begin |
| 261 |
if Assigned(FSingleInstance) then |
| 262 |
if FSingleInstance.IsServer then |
| 263 |
FSingleInstance.ServerCheckMessages; |
| 264 |
|
| 265 |
// Override in descendent classes. |
| 266 |
end; |
| 267 |
|
| 268 |
procedure TCustomApplication.DoLog(EventType: TEventType; const Msg: String); |
| 269 |
|
| 270 |
begin |
| 271 |
// Do nothing, override in descendants |
| 272 |
end; |
| 273 |
|
| 274 |
procedure TCustomApplication.Log(EventType: TEventType; const Msg: String); |
| 275 |
|
| 276 |
begin |
| 277 |
If (FEventLogFilter=[]) or (EventType in FEventLogFilter) then |
| 278 |
DoLog(EventType,Msg); |
| 279 |
end; |
| 280 |
|
| 281 |
procedure TCustomApplication.Log(EventType: TEventType; const Fmt: String; |
| 282 |
const Args: array of const); |
| 283 |
begin |
| 284 |
try |
| 285 |
Log(EventType, Format(Fmt, Args)); |
| 286 |
except |
| 287 |
On E : Exception do |
| 288 |
Log(etError,Format('Error formatting message "%s" with %d arguments: %s',[Fmt,Length(Args),E.Message])); |
| 289 |
end |
| 290 |
end; |
| 291 |
|
| 292 |
constructor TCustomApplication.Create(AOwner: TComponent); |
| 293 |
begin |
| 294 |
inherited Create(AOwner); |
| 295 |
FOptionChar:='-'; |
| 296 |
FCaseSensitiveOptions:=True; |
| 297 |
FStopOnException:=False; |
| 298 |
FSingleInstanceClass := DefaultSingleInstanceClass; |
| 299 |
end; |
| 300 |
|
| 301 |
destructor TCustomApplication.Destroy; |
| 302 |
begin |
| 303 |
inherited Destroy; |
| 304 |
end; |
| 305 |
|
| 306 |
procedure TCustomApplication.HandleException(Sender: TObject); |
| 307 |
begin |
| 308 |
If Not (ExceptObject is Exception) then |
| 309 |
SysUtils.showexception(ExceptObject,ExceptAddr) |
| 310 |
else |
| 311 |
begin |
| 312 |
If Not Assigned(FOnexception) then |
| 313 |
ShowException(Exception(ExceptObject)) |
| 314 |
else |
| 315 |
FOnException(Sender,Exception(ExceptObject)); |
| 316 |
end; |
| 317 |
If FStopOnException then |
| 318 |
Terminate(ExceptionExitCode); |
| 319 |
end; |
| 320 |
|
| 321 |
|
| 322 |
procedure TCustomApplication.Initialize; |
| 323 |
begin |
| 324 |
FTerminated:=False; |
| 325 |
if FSingleInstanceEnabled then |
| 326 |
begin |
| 327 |
case SingleInstance.Start of |
| 328 |
siClient: |
| 329 |
begin |
| 330 |
SingleInstance.ClientPostParams; |
| 331 |
FTerminated:=True; |
| 332 |
end; |
| 333 |
siNotResponding: |
| 334 |
FTerminated:=True; |
| 335 |
end; |
| 336 |
end; |
| 337 |
end; |
| 338 |
|
| 339 |
procedure TCustomApplication.Run; |
| 340 |
|
| 341 |
begin |
| 342 |
Repeat |
| 343 |
Try |
| 344 |
DoRun; |
| 345 |
except |
| 346 |
HandleException(Self); |
| 347 |
end; |
| 348 |
Until FTerminated; |
| 349 |
end; |
| 350 |
|
| 351 |
procedure TCustomApplication.SetSingleInstanceClass( |
| 352 |
const ASingleInstanceClass: TBaseSingleInstanceClass); |
| 353 |
begin |
| 354 |
Assert((FSingleInstance = nil) and (ASingleInstanceClass <> nil)); |
| 355 |
FSingleInstanceClass := ASingleInstanceClass; |
| 356 |
end; |
| 357 |
|
| 358 |
procedure TCustomApplication.ShowException(E: Exception); |
| 359 |
|
| 360 |
begin |
| 361 |
Sysutils.ShowException(E,ExceptAddr) |
| 362 |
end; |
| 363 |
|
| 364 |
procedure TCustomApplication.Terminate; |
| 365 |
begin |
| 366 |
Terminate(ExitCode); |
| 367 |
end; |
| 368 |
|
| 369 |
procedure TCustomApplication.Terminate(AExitCode : Integer) ; |
| 370 |
|
| 371 |
begin |
| 372 |
FTerminated:=True; |
| 373 |
ExitCode:=AExitCode; |
| 374 |
end; |
| 375 |
|
| 376 |
function TCustomApplication.GetOptionAtIndex(AIndex : Integer; IsLong: Boolean): String; |
| 377 |
|
| 378 |
Var |
| 379 |
P : Integer; |
| 380 |
O : String; |
| 381 |
|
| 382 |
begin |
| 383 |
Result:=''; |
| 384 |
If (AIndex=-1) then |
| 385 |
Exit; |
| 386 |
If IsLong then |
| 387 |
begin // Long options have form --option=value |
| 388 |
O:=Params[AIndex]; |
| 389 |
P:=Pos('=',O); |
| 390 |
If (P=0) then |
| 391 |
P:=Length(O); |
| 392 |
Delete(O,1,P); |
| 393 |
Result:=O; |
| 394 |
end |
| 395 |
else |
| 396 |
begin // short options have form '-o value' |
| 397 |
If (AIndex<ParamCount) then |
| 398 |
if (Copy(Params[AIndex+1],1,1)<>OptionChar) then |
| 399 |
Result:=Params[AIndex+1]; |
| 400 |
end; |
| 401 |
end; |
| 402 |
|
| 403 |
|
| 404 |
function TCustomApplication.GetOptionValue(const S: String): String; |
| 405 |
begin |
| 406 |
Result:=GetoptionValue(#255,S); |
| 407 |
end; |
| 408 |
|
| 409 |
function TCustomApplication.GetOptionValue(const C: Char; const S: String |
| 410 |
): String; |
| 411 |
|
| 412 |
Var |
| 413 |
B : Boolean; |
| 414 |
I : integer; |
| 415 |
|
| 416 |
begin |
| 417 |
Result:=''; |
| 418 |
I:=FindOptionIndex(C,B); |
| 419 |
If (I=-1) then |
| 420 |
I:=FindOptionIndex(S,B); |
| 421 |
If I<>-1 then |
| 422 |
Result:=GetOptionAtIndex(I,B); |
| 423 |
end; |
| 424 |
|
| 425 |
function TCustomApplication.GetOptionValues(const C: Char; const S: String): TStringArray; |
| 426 |
|
| 427 |
Var |
| 428 |
I,Cnt : Integer; |
| 429 |
B : Boolean; |
| 430 |
|
| 431 |
begin |
| 432 |
SetLength(Result,ParamCount); |
| 433 |
Cnt:=0; |
| 434 |
I:=-1; |
| 435 |
Repeat |
| 436 |
I:=FindOptionIndex(C,B,I); |
| 437 |
If I<>-1 then |
| 438 |
begin |
| 439 |
Inc(Cnt); |
| 440 |
Dec(I); |
| 441 |
end; |
| 442 |
Until I=-1; |
| 443 |
Repeat |
| 444 |
I:=FindOptionIndex(S,B,I); |
| 445 |
If I<>-1 then |
| 446 |
begin |
| 447 |
Inc(Cnt); |
| 448 |
Dec(I); |
| 449 |
end; |
| 450 |
Until I=-1; |
| 451 |
SetLength(Result,Cnt); |
| 452 |
Cnt:=0; |
| 453 |
I:=-1; |
| 454 |
Repeat |
| 455 |
I:=FindOptionIndex(C,B,I); |
| 456 |
If (I<>-1) then |
| 457 |
begin |
| 458 |
Result[Cnt]:=GetOptionAtIndex(I,False); |
| 459 |
Inc(Cnt); |
| 460 |
Dec(i); |
| 461 |
end; |
| 462 |
Until (I=-1); |
| 463 |
I:=-1; |
| 464 |
Repeat |
| 465 |
I:=FindOptionIndex(S,B,I); |
| 466 |
If I<>-1 then |
| 467 |
begin |
| 468 |
Result[Cnt]:=GetOptionAtIndex(I,True); |
| 469 |
Inc(Cnt); |
| 470 |
Dec(i); |
| 471 |
end; |
| 472 |
Until (I=-1); |
| 473 |
end; |
| 474 |
|
| 475 |
function TCustomApplication.HasOption(const S: String): Boolean; |
| 476 |
|
| 477 |
Var |
| 478 |
B : Boolean; |
| 479 |
|
| 480 |
begin |
| 481 |
Result:=FindOptionIndex(S,B)<>-1; |
| 482 |
end; |
| 483 |
|
| 484 |
function TCustomApplication.FindOptionIndex(const S: String; |
| 485 |
var Longopt: Boolean; StartAt : Integer = -1): Integer; |
| 486 |
|
| 487 |
Var |
| 488 |
SO,O : String; |
| 489 |
I,P : Integer; |
| 490 |
|
| 491 |
begin |
| 492 |
If Not CaseSensitiveOptions then |
| 493 |
SO:=UpperCase(S) |
| 494 |
else |
| 495 |
SO:=S; |
| 496 |
Result:=-1; |
| 497 |
I:=StartAt; |
| 498 |
if (I=-1) then |
| 499 |
I:=ParamCount; |
| 500 |
While (Result=-1) and (I>0) do |
| 501 |
begin |
| 502 |
O:=Params[i]; |
| 503 |
// - must be seen as an option value |
| 504 |
If (Length(O)>1) and (O[1]=FOptionChar) then |
| 505 |
begin |
| 506 |
Delete(O,1,1); |
| 507 |
LongOpt:=(Length(O)>0) and (O[1]=FOptionChar); |
| 508 |
If LongOpt then |
| 509 |
begin |
| 510 |
Delete(O,1,1); |
| 511 |
P:=Pos('=',O); |
| 512 |
If (P<>0) then |
| 513 |
O:=Copy(O,1,P-1); |
| 514 |
end; |
| 515 |
If Not CaseSensitiveOptions then |
| 516 |
O:=UpperCase(O); |
| 517 |
If (O=SO) then |
| 518 |
Result:=i; |
| 519 |
end; |
| 520 |
Dec(i); |
| 521 |
end; |
| 522 |
end; |
| 523 |
|
| 524 |
function TCustomApplication.HasOption(const C: Char; const S: String): Boolean; |
| 525 |
|
| 526 |
Var |
| 527 |
B : Boolean; |
| 528 |
|
| 529 |
begin |
| 530 |
Result:=(FindOptionIndex(C,B)<>-1) or (FindOptionIndex(S,B)<>-1); |
| 531 |
end; |
| 532 |
|
| 533 |
|
| 534 |
function TCustomApplication.CheckOptions(const ShortOptions: String; |
| 535 |
const Longopts: TStrings; AllErrors: Boolean): String; |
| 536 |
|
| 537 |
begin |
| 538 |
Result:=CheckOptions(ShortOptions,LongOpts,Nil,Nil,AllErrors); |
| 539 |
end; |
| 540 |
|
| 541 |
ResourceString |
| 542 |
SErrInvalidOption = 'Invalid option at position %d: "%s"'; |
| 543 |
SErrNoOptionAllowed = 'Option at position %d does not allow an argument: %s'; |
| 544 |
SErrOptionNeeded = 'Option at position %d needs an argument : %s'; |
| 545 |
|
| 546 |
function TCustomApplication.CheckOptions(const ShortOptions: String; |
| 547 |
const Longopts: TStrings; Opts, NonOpts: TStrings; AllErrors: Boolean |
| 548 |
): String; |
| 549 |
|
| 550 |
Var |
| 551 |
I,J,L,P : Integer; |
| 552 |
O,OV,SO : String; |
| 553 |
UsedArg,HaveArg : Boolean; |
| 554 |
|
| 555 |
Function FindLongOpt(S : String) : boolean; |
| 556 |
|
| 557 |
Var |
| 558 |
I : integer; |
| 559 |
|
| 560 |
begin |
| 561 |
Result:=Assigned(LongOpts); |
| 562 |
if Not Result then |
| 563 |
exit; |
| 564 |
If CaseSensitiveOptions then |
| 565 |
begin |
| 566 |
I:=LongOpts.Count-1; |
| 567 |
While (I>=0) and (LongOpts[i]<>S) do |
| 568 |
Dec(i); |
| 569 |
end |
| 570 |
else |
| 571 |
begin |
| 572 |
S:=UpperCase(S); |
| 573 |
I:=LongOpts.Count-1; |
| 574 |
While (I>=0) and (UpperCase(LongOpts[i])<>S) do |
| 575 |
Dec(i); |
| 576 |
end; |
| 577 |
Result:=(I<>-1); |
| 578 |
end; |
| 579 |
|
| 580 |
Procedure AddToResult(Const Msg : string); |
| 581 |
|
| 582 |
begin |
| 583 |
If (Result<>'') then |
| 584 |
Result:=Result+sLineBreak; |
| 585 |
Result:=Result+Msg; |
| 586 |
end; |
| 587 |
|
| 588 |
begin |
| 589 |
If CaseSensitiveOptions then |
| 590 |
SO:=Shortoptions |
| 591 |
else |
| 592 |
SO:=LowerCase(Shortoptions); |
| 593 |
Result:=''; |
| 594 |
I:=1; |
| 595 |
While (I<=ParamCount) and ((Result='') or AllErrors) do |
| 596 |
begin |
| 597 |
O:=Paramstr(I); |
| 598 |
If (Length(O)=0) or (O[1]<>FOptionChar) then |
| 599 |
begin |
| 600 |
If Assigned(NonOpts) then |
| 601 |
NonOpts.Add(O); |
| 602 |
end |
| 603 |
else |
| 604 |
begin |
| 605 |
If (Length(O)<2) then |
| 606 |
AddToResult(Format(SErrInvalidOption,[i,O])) |
| 607 |
else |
| 608 |
begin |
| 609 |
HaveArg:=False; |
| 610 |
OV:=''; |
| 611 |
// Long option ? |
| 612 |
If (O[2]=FOptionChar) then |
| 613 |
begin |
| 614 |
Delete(O,1,2); |
| 615 |
J:=Pos('=',O); |
| 616 |
If J<>0 then |
| 617 |
begin |
| 618 |
HaveArg:=true; |
| 619 |
OV:=O; |
| 620 |
Delete(OV,1,J); |
| 621 |
O:=Copy(O,1,J-1); |
| 622 |
end; |
| 623 |
// Switch Option |
| 624 |
If FindLongopt(O) then |
| 625 |
begin |
| 626 |
If HaveArg then |
| 627 |
AddToResult(Format(SErrNoOptionAllowed,[I,O])); |
| 628 |
end |
| 629 |
else |
| 630 |
begin // Required argument |
| 631 |
If FindLongOpt(O+':') then |
| 632 |
begin |
| 633 |
If Not HaveArg then |
| 634 |
AddToResult(Format(SErrOptionNeeded,[I,O])); |
| 635 |
end |
| 636 |
else |
| 637 |
begin // Optional Argument. |
| 638 |
If not FindLongOpt(O+'::') then |
| 639 |
AddToResult(Format(SErrInvalidOption,[I,O])); |
| 640 |
end; |
| 641 |
end; |
| 642 |
end |
| 643 |
else // Short Option. |
| 644 |
begin |
| 645 |
HaveArg:=(I<ParamCount) and (Length(ParamStr(I+1))>0) and (ParamStr(I+1)[1]<>FOptionChar); |
| 646 |
UsedArg:=False; |
| 647 |
If Not CaseSensitiveOptions then |
| 648 |
O:=LowerCase(O); |
| 649 |
L:=Length(O); |
| 650 |
J:=2; |
| 651 |
While ((Result='') or AllErrors) and (J<=L) do |
| 652 |
begin |
| 653 |
P:=Pos(O[J],SO); |
| 654 |
If (P=0) or (O[j]=':') then |
| 655 |
AddToResult(Format(SErrInvalidOption,[I,O[J]])) |
| 656 |
else |
| 657 |
begin |
| 658 |
If (P<Length(SO)) and (SO[P+1]=':') then |
| 659 |
begin |
| 660 |
// Required argument |
| 661 |
If ((P+1)=Length(SO)) or (SO[P+2]<>':') Then |
| 662 |
If (J<L) or not haveArg then // Must be last in multi-opt !! |
| 663 |
AddToResult(Format(SErrOptionNeeded,[I,O[J]])); |
| 664 |
O:=O[j]; // O is added to arguments. |
| 665 |
UsedArg:=True; |
| 666 |
end; |
| 667 |
end; |
| 668 |
Inc(J); |
| 669 |
end; |
| 670 |
HaveArg:=HaveArg and UsedArg; |
| 671 |
If HaveArg then |
| 672 |
begin |
| 673 |
Inc(I); // Skip argument. |
| 674 |
OV:=Paramstr(I); |
| 675 |
end; |
| 676 |
end; |
| 677 |
If HaveArg and ((Result='') or AllErrors) then |
| 678 |
If Assigned(Opts) then |
| 679 |
Opts.Add(O+'='+OV); |
| 680 |
end; |
| 681 |
end; |
| 682 |
Inc(I); |
| 683 |
end; |
| 684 |
end; |
| 685 |
|
| 686 |
function TCustomApplication.CheckOptions(const ShortOptions: String; |
| 687 |
const Longopts: array of string; Opts, NonOpts: TStrings; AllErrors: Boolean |
| 688 |
): String; |
| 689 |
Var |
| 690 |
L : TStringList; |
| 691 |
I : Integer; |
| 692 |
|
| 693 |
begin |
| 694 |
L:=TStringList.Create; |
| 695 |
Try |
| 696 |
For I:=0 to High(LongOpts) do |
| 697 |
L.Add(LongOpts[i]); |
| 698 |
Result:=CheckOptions(ShortOptions,L,Opts,NonOpts,AllErrors); |
| 699 |
Finally |
| 700 |
L.Free; |
| 701 |
end; |
| 702 |
end; |
| 703 |
|
| 704 |
function TCustomApplication.CheckOptions(const ShortOptions: String; |
| 705 |
const LongOpts: array of string; AllErrors: Boolean): String; |
| 706 |
|
| 707 |
Var |
| 708 |
L : TStringList; |
| 709 |
I : Integer; |
| 710 |
|
| 711 |
begin |
| 712 |
L:=TStringList.Create; |
| 713 |
Try |
| 714 |
For I:=0 to High(LongOpts) do |
| 715 |
L.Add(LongOpts[i]); |
| 716 |
Result:=CheckOptions(ShortOptions,L,AllErrors); |
| 717 |
Finally |
| 718 |
L.Free; |
| 719 |
end; |
| 720 |
end; |
| 721 |
|
| 722 |
function TCustomApplication.CheckOptions(const ShortOptions: String; |
| 723 |
const LongOpts: String; AllErrors: Boolean): String; |
| 724 |
|
| 725 |
Const |
| 726 |
SepChars = ' '#10#13#9; |
| 727 |
|
| 728 |
Var |
| 729 |
L : TStringList; |
| 730 |
Len,I,J : Integer; |
| 731 |
|
| 732 |
begin |
| 733 |
L:=TStringList.Create; |
| 734 |
Try |
| 735 |
I:=1; |
| 736 |
Len:=Length(LongOpts); |
| 737 |
While I<=Len do |
| 738 |
begin |
| 739 |
While Isdelimiter(SepChars,LongOpts,I) do |
| 740 |
Inc(I); |
| 741 |
J:=I; |
| 742 |
While (J<=Len) and Not IsDelimiter(SepChars,LongOpts,J) do |
| 743 |
Inc(J); |
| 744 |
If (I<=J) then |
| 745 |
L.Add(Copy(LongOpts,I,(J-I))); |
| 746 |
I:=J+1; |
| 747 |
end; |
| 748 |
Result:=CheckOptions(Shortoptions,L,AllErrors); |
| 749 |
Finally |
| 750 |
L.Free; |
| 751 |
end; |
| 752 |
end; |
| 753 |
|
| 754 |
function TCustomApplication.GetNonOptions(const ShortOptions: String; |
| 755 |
const Longopts: array of string): TStringArray; |
| 756 |
|
| 757 |
Var |
| 758 |
NO : TStrings; |
| 759 |
I : Integer; |
| 760 |
|
| 761 |
begin |
| 762 |
No:=TStringList.Create; |
| 763 |
try |
| 764 |
GetNonOptions(ShortOptions,LongOpts,No); |
| 765 |
SetLength(Result,NO.Count); |
| 766 |
For I:=0 to NO.Count-1 do |
| 767 |
Result[I]:=NO[i]; |
| 768 |
finally |
| 769 |
NO.Free; |
| 770 |
end; |
| 771 |
end; |
| 772 |
|
| 773 |
procedure TCustomApplication.GetNonOptions(const ShortOptions: String; |
| 774 |
const Longopts: array of string; NonOptions: TStrings); |
| 775 |
|
| 776 |
Var |
| 777 |
S : String; |
| 778 |
|
| 779 |
begin |
| 780 |
S:=CheckOptions(ShortOptions,LongOpts,Nil,NonOptions,true); |
| 781 |
if (S<>'') then |
| 782 |
Raise EListError.Create(S); |
| 783 |
end; |
| 784 |
|
| 785 |
end. |