Delphi에서 명령어 인자 처리하기
절름발이 프로그래머/Delphi / 2009. 11. 18. 13:00
ParamCount 와 ParamStr 을 이용하여, 명령어 인자를 처리할 수 있다.
Form이 있건, 없건 상관이 없다.
Console Program의 경우에 대해서, 다음과 같이 소스로 만들면 된다.
Form이 있는 프로그램인 경우에는, 아래 소스를 발췌하여, FormCreate() 함수에 추가하면 된다.
인자 사용법은 실행시에,
c:> Project1.exe -a:HERE_IS_A -b:THRER_IS_B
와 같이 씁니다.
아래소스에는 반드시 FastStrings 모듈을 추가해서 사용해야 합니다.
--------------------------------------------------------------------------------
program Project1;
{$APPTYPE CONSOLE}
uses
Sysutils,
Classes,
Dialogs,
FastStringFuncs in 'src\FastStringFuncs.pas',
FastStrings in 'src\FastStrings.pas';
var
param_idx:integer;
argumentstr, prefix, realarg : String;
arguments : TStrings;
begin
try
{ TODO -oUser -cConsole Main : Insert code here }
for param_idx := 1 to ParamCount do
begin
//ShowMessage(ParamStr(param_idx));
argumentstr := ParamStr(param_idx);
if (argumentstr[1] = '-') then
begin
Split(Copy(argumentstr, 2, Length(argumentstr) - 1), ':', arguments);
prefix := arguments[0];
realarg := arguments[1];
if ((arguments.Count = 2) and (Length(prefix) = 1)) then
begin
Case arguments[0][1] of
'a' : ShowMessage( 'option a : ' + arguments[1]);
'b' : ShowMessage( 'option b : ' + arguments[1]);
else ;
end;
end;
end;
end;
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
end.