[起動時の引数を取得する(オプション指定あり/なし)]
起動時の引数(オプション付き)を表示します。
オプション指定文字( / )よって、引数を指定することができます。
DelphiにはVBScriptと異なり便利な関数がないようなので、Delphiでそれっぽい関数を作成してみました。
【ソースコード】 [tips0003.pas]
program tips0003;
{$APPTYPE CONSOLE}
type
TMyArray = array[0..255] of String;
function NamedExists(key:string):Boolean;
var
ix: integer;
begin
NamedExists := false;
for ix := 1 to ParamCount do
begin
if (copy(ParamStr(ix),1,length(key)+1) = '/' + key) and (length(ParamStr(ix))=length(key)+1) then
NamedExists := true;
if (copy(ParamStr(ix),1,length(key)+2) = '/' + key + ':') and (length(ParamStr(ix)) >= length(key)+2) then
NamedExists := true;
end;
end;
function Named(key:string):string;
var
ix: integer;
begin
Named:='';
for ix := 1 to ParamCount do
begin
if copy(ParamStr(ix),1,length(key)+2) = '/' + key + ':' then
Named := copy(ParamStr(ix),length(key)+3,length(ParamStr(ix))-length(key)+2);
end;
end;
function UnnamedCount:Integer;
var
ix: integer;
cnt: integer;
begin
cnt := 0;
for ix := 1 to ParamCount do
begin
if copy(ParamStr(ix),1,1) <> '/' then
cnt := cnt + 1;
end;
UnnamedCount:=cnt;
end;
function Unnamed:TMyArray;
var
uAry : TMyArray;
ix, iy: integer;
begin
iy := 0;
for ix := 1 to ParamCount do
begin
if copy(ParamStr(ix),1,1) <> '/' then
begin
uAry[iy] := ParamStr(ix);
iy:=iy+1;
end;
end;
Unnamed:=uAry;
end;
begin
if NamedExists('A') then
begin
writeLn('/A パラメータの値は',Named('A'),'です。')
end;
if UnnamedCount > 0 Then
begin
writeLn('オプション指定なし: ',Unnamed[0]);
end;
end.
【コンパイル&実行】 [tips0003.bat]
dcc32 tips0003.pas
tips0003.exe arg1 /A:arg2 > tips0003.txt
pause
【実行結果】 [tips0003.txt]
/A パラメータの値はarg2です。
オプション指定なし: arg1
Copyright c 2009, ohishi,All rights reserved.