where : ibrtses delphi
Command line
disclaimer
the source code of this page may not appear correctly in certain browsers
due to special characters. Have a look at the source of this HTML page
with notepad instead
The commandline tools provided by the system are somewhat insufficient.
They are
Tearing the string apart is left to the user.
A typical Commandline looks like
executable -c4 -n2345 -ffilename -...
executable /c4 /n2345 /ffilename /...
The following component provides exactly that, including that it
doesn't insist on the order of the parameters.
Upon :
h:=cmd.getparameter('c');
it returns '-c4', in the above example
limitations
There are several limitations.
- it searches for single characters, meaning it cannot
discriminate between -ni -nu
- it is case sensitive what this character concerns
- the parameters cannot contain spaces yet, this could be solved by
implementing additional " " around single parameters
commandline component
The following code is best copy/pasted from the HTML source.
{
It is assumed the commandline consists of a series of parameters
separated by blanks and prefixed with '-' or '/', Eg :
executable -c4 -n2345 -ffilename -...
usage:
procedure TForm1.FormCreate(Sender: TObject);
var cmd:TCommandLine;
h:string;
begin
cmd:=TCommandLine.create(CmdLine); // the parameter is provided by the system
h:=cmd.getparameter('c');
if (h < > '') then ComNr:=ord(h[3])-$30; // h= '-c4'
..
cmd.destroy;
end;
}
unit Commandline;
interface
uses
SysUtils, Classes;
type
TCommandline = class(TObject)
private
{ Private declarations }
fs:string;
protected
{ Protected declarations }
public
{ Public declarations }
constructor create(var Commandline:PChar); // copies the commandline
destructor destroy;
function getparameter(N:char):string;
published
{ Published declarations }
end;
procedure Register;
implementation
constructor TCommandline.create(var Commandline:PChar);
var i:integer;
begin
inherited create;
fs:=Commandline; // get the PCHAR into the Delphi string
end; //create
destructor TCommandline.destroy;
begin
setlength(fs,0); // get rid of the commandline string
inherited destroy;
end; // destroy
function TCommandline.getparameter(N:char):string;
var i,t,j:integer;
begin
t:=pos('-'+N,fs); // find the parameter
if (t=0) then result:='' // not found, return empty
else begin
i:=length(fs); j:=t;
while (j < i) and (fs[j] < > ' ') do inc(j); // find end
if (fs[j]=' ')or(j=i) then begin
if i=j then result:=copy(fs,t,j-t+1)
else result:=copy(fs,t,j-t);
end
else result:='';
end;
end; // func getparameter
procedure Register;
begin
// RegisterComponents('Samples', [TCommandline]);
end;
end. // unit Commandline
Feedback is welcome
sponsored links
Delphi index
home
last updated: 29.april.04, or perhaps later
Copyright (99,2004) Ing.Büro R.Tschaggelar