Ich habe die unten stehende Unit geschrieben, um Befehlszeilenargumente auf eine robustere Art und Weise zu parsen. Sie können sie gerne verwenden. Ich habe ein Anwendungsbeispiel nach der Unit eingefügt (scrollen Sie nach unten).
unit CLArgParser;
//this class makes it easier to parse command line arguments
interface
uses
Classes;
type
strarr = array of string;
type
TCLArgParser = class
private
FPermitTags : array of string;
FTrimAll: boolean;
public
function IsArg(argtag : string) : boolean;
function GetArg(argtag : string) : string;
function GetDelimtedArg(argtag, delimiter : string) : TStringList;
constructor Create(ArgTags : array of string); overload;
constructor Create; overload;
property TrimAll: boolean read FTrimAll write FTrimAll;
end;
implementation
uses
SysUtils;
const
cDefaultTags : array[0..1] of string = ('-','/');
constructor TCLArgParser.Create(ArgTags : array of string);
var i : integer;
begin
try
SetLength(FPermitTags,High(ArgTags)+1);
for i := 0 to High(ArgTags) do begin
FPermitTags[i] := ArgTags[i];
end; //for i
except on e : exception do
raise;
end; //try-except
end;
constructor TCLArgParser.Create;
begin
FTrimAll := False; //default value
inherited Create;
Create(cDefaultTags);
end;
function TCLArgParser.GetArg(argtag: string): string;
var i,j,n : integer;
begin
try
Result := '';
n := High(FPermitTags);
for i := 1 to ParamCount do
for j := 0 to n do
if Uppercase(ParamStr(i)) = (FPermitTags[j] + Uppercase(argtag)) then
Result := ParamStr(i+1);
if FTrimAll then begin
Result := Trim(Result);
end;
except on e : exception do
raise;
end; //try-except
end;
function TCLArgParser.GetDelimtedArg(argtag, delimiter: string): TStringList;
var i : integer;
argval, tmp : string;
begin
try
Result := TStringList.Create;
argval := GetArg(argtag);
for i := 1 to Length(argval) do begin
if ((i = Length(argval)) or ((argval[i] = delimiter) and (tmp <> '')))
then begin
if i = Length(argval) then begin
tmp := tmp + argval[i];
if FTrimAll then begin
tmp := Trim(tmp);
end;
end;
Result.Add(tmp);
tmp := '';
end //if we found a delimted value
else begin
tmp := tmp + argval[i];
end; //else we just keep looking
end; //for ea. character
except on e : exception do
raise;
end; //try-except
end;
function TCLArgParser.IsArg(argtag: string): boolean;
var i,j,n : integer;
begin
try
Result := False;
n := High(FPermitTags);
for i := 1 to ParamCount do begin
for j := 0 to n do begin
if Uppercase(ParamStr(i)) = (FPermitTags[j] + Uppercase(argtag))
then begin
Result := True;
Exit;
end; //if we found it
end; //for j
end; //for i
except on e : exception do
raise;
end; //try-except
end;
end.
Beispiel für die Verwendung:
procedure DefineParameters;
var
clarg: TCLArgParser;
begin
//assign command line arguments to various global variables
clarg := TCLArgParser.Create;
try
wantshelp := clarg.IsArg('?') or clArg.IsArg('help');
dbuser := clarg.GetArg('u');
dbpwd := clarg.GetArg('p');
dbserver := clarg.GetArg('d');
localfilename := clarg.GetArg('localfile');
ftpuser := clarg.GetArg('ftu');
ftppwd := clarg.GetArg('ftp');
ftpipaddr := clarg.GetArg('fti');
emailfromacct := clarg.GetArg('efrom');
emailtoacct := clarg.GetArg('eto');
archivefolder := clarg.GetArg('archive');
if archivefolder <> '' then begin
if archivefolder[Length(archivefolder)] <> '\' then begin
archivefolder := archivefolder + '\';
end;
end;
//figure out the (optional) verbosity code.
//if they didn't specify, assume the default value
verbosity := c_VerbosityDefault;
if clArg.IsArg('v') then begin
if not(TryStrToInt(clarg.GetArg('v'),verbosity)) then begin
WriteLn('Invalid verbosity code- using default of ' +
IntToStr(c_VerbosityDefault) + '.');
end; //if their specified verbosity was invalid
end; //if they specified the verbosity
if not(TryStrToInt(clarg.GetArg('maxtime'),maxtime)) then begin
maxtime := 9999999;
end;
finally
FreeAndNil(clarg);
end; //try-finally
end;