Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > Oracle Database, SQL, Application, Programming


How to look for and handle command line parameters


Reply
Views: 1489  
Thread Tools Rate Thread
  #1  
Old 08-20-2010, 10:51 AM
bholas bholas is offline
Award Winner
 
Join Date: Apr 2010
Posts: 4,977
Default How to look for and handle command line parameters

Most command line programs and some Windows programs has the ability to look for and handle parameters passed to it such as "/? /HELP /Q". If you want to add similar functionality to your Delphi programs, you can start with a function like this:
program myprog;

uses
SysUtils;

function CmdLineParamFound(
sParamName : String ) : Boolean;

const
{ assume that command line parameters
start with the "/" character }
c_token = '/';

var
i : integer;
sTemp : string;

begin
result := False;

for i := 1 to ParamCount do
begin
sTemp := ParamStr( i );
if( c_token = sTemp[ 1 ] )then
begin
if( ( c_token +
UpperCase( sParamName ) ) =
UpperCase( sTemp ) )then
begin
result := True;
exit;
end;
end;
end;
end;

begin

{
following "if" statement will be triggered
if called with the /HELP parameter:

myprog.exe /HELP
}

if( CmdLineParamFound( 'HELP' ) )then
begin

{
display help here...
}

end;

{
myprog.exe /FULLSCREEN
}
if( CmdLineParamFound( 'FULLSCREEN' ) )then
begin

{
run program in full screen mode
}

end;

end.

If you need more help, look up "ParamStr()" and "ParamCount" functions in your help files.

Reply With Quote
Reply

Tags
programming tips

New topics in Oracle Database, SQL, Application, Programming





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)