Board index » delphi » paramstr() used during runtime?

paramstr() used during runtime?

Hi again... I am writing a shell-type program but I am having some
problems with the command line interpreter part. I would like the
program to read a command, followed by the parameters for it (to be
passed to a function) from the command line. I tried using something
like the following:

type

 commandstr  = string[4];
 param1, param2 = string[5];

{blah blah blah}

begin {main}

 readln(commandstr, param1, param2);

If up(commandstr) = 'HELP' then help(param1)
 else
 If up(commandstr) = 'SAVE' then save(param1);

{etc...}

Now, this sux because the command is limited to 4 characters, and if
it is less, then stuff gets truncated out of the param1 variable.
Something like paramstr() would be absolutely ideal because it ignores
spaces, and there is no limit (except 256byte) on it either. How does
this work?

I know I can't use paramstr() except on the command line when my
program initially runs. I could write my own procedure for it (which I
am capable of doing) however, how can I load the parameter into a
string without knowing how long the parameter is?

Pointers could be an option, but I need to make a 'type', and that
type could be too small/large right? (wrong?)

Well, any help would be appreicated, and thanks.

 

Re:paramstr() used during runtime?


Quote
> Hi again... I am writing a shell-type program but I am having some
> problems with the command line interpreter part. I would like the
> program to read a command, followed by the parameters for it (to be
> passed to a function) from the command line. I tried using something
> like the following:

> type

>  commandstr  = string[4];
>  param1, param2 = string[5];

> {blah blah blah}

> begin {main}

>  readln(commandstr, param1, param2);
>  If up(commandstr) = 'HELP' then help(param1)
>  else
>  If up(commandstr) = 'SAVE' then save(param1);

> {etc...}

> Now, this sux because the command is limited to 4 characters, and if
> it is less, then stuff gets truncated out of the param1 variable.
> Something like paramstr() would be absolutely ideal because it ignores
> spaces, and there is no limit (except 256byte) on it either. How does
> this work?

> I know I can't use paramstr() except on the command line when my
> program initially runs. I could write my own procedure for it (which I
> am capable of doing) however, how can I load the parameter into a
> string without knowing how long the parameter is?

> Pointers could be an option, but I need to make a 'type', and that
> type could be too small/large right? (wrong?)

> Well, any help would be appreicated, and thanks.

   I don't know why you're using specific-length string variables here,
since the user might type in _anything_, nor can I see the advantage to
use multiple parameters/strings in the command line read.  Myself, I'd
use a readln of a (full) string variable and parse the components from
it - like DOS does with a command string.  All you'd need to do is scan
for/parse on spaces, and place each token into a different variable which
suits your needs.
   Furthermore, why are you working with individual tokens, anyway?  
Aren't you asking the user for a full command to be executed and just
passing that to the Exec procedure?
   No matter how you do it, I suggest that asking the user to key in a
valid command/parameters is a lot - I would establish a fixed menu of
allowable command/processes and let the user select from it.  That would
be must more robust and error-free.

Re:paramstr() used during runtime?


Quote
In article <4utiip$...@aphex.direct.ca> Elliot wrote:
>Hi again... I am writing a shell-type program but I am having some
>problems with the command line interpreter part. I would like the
>program to read a command, followed by the parameters for it (to be
>passed to a function) from the command line. I tried using something
>like the following:

>type

> commandstr  = string[4];
> param1, param2 = string[5];

>{blah blah blah}

>begin {main}

> readln(commandstr, param1, param2);

>If up(commandstr) = 'HELP' then help(param1)
> else
> If up(commandstr) = 'SAVE' then save(param1);

>{etc...}

>Now, this sux because the command is limited to 4 characters, and if
>it is less, then stuff gets truncated out of the param1 variable.
>Something like paramstr() would be absolutely ideal because it ignores
>spaces, and there is no limit (except 256byte) on it either. How does
>this work?

>I know I can't use paramstr() except on the command line when my
>program initially runs. I could write my own procedure for it (which I
>am capable of doing) however, how can I load the parameter into a
>string without knowing how long the parameter is?

>Pointers could be an option, but I need to make a 'type', and that
>type could be too small/large right? (wrong?)

>Well, any help would be appreicated, and thanks.

Elliot, follow the links from my home page and download the UTIL
Unit.  The archive contains source and necessary OBJs so you can
compile the unit for your particular version of TP.

   http:/members.gnn.com/rdonais/

or ftp://members.gnn.com/rdonais/tpascal/util.zip

I think you'll find that FTrim and the Field or Fields functions
will do what you want.

   Readln(s);
   s := UpperCase(Ftrim(s));
   Cmd := Field(s, ' ', 1);
   Op1 := Field(s, ' ', 2);
   Op2 := Field(s, ' ', 3);

           {.123456789.123456789.}
   Case Pos('HELP SAVE LOAD etc...') of
       1: Help(Op1);
       6: Save(Op1);
      11: Load(Op1);
     Else Writeln('Unrecognized command: "', Cmd, '"');
   End;

   ...red

Re:paramstr() used during runtime?


Quote
In article <4utiip$...@aphex.direct.ca>, Elliot <ewilt...@direct.ca> wrote:

>Now, this sux because the command is limited to 4 characters, and if
>it is less, then stuff gets truncated out of the param1 variable.
>Something like paramstr() would be absolutely ideal because it ignores
>spaces, and there is no limit (except 256byte) on it either. How does
>this work?

>I know I can't use paramstr() except on the command line when my
>program initially runs. I could write my own procedure for it (which I
>am capable of doing) however, how can I load the parameter into a
>string without knowing how long the parameter is?

Well you can use paramstr:

type string127=string[127];

var p:^string127;
    i:integer;

begin
  p:=ptr(prefixseg,$80);
  p^:='one two three';
  writeln(paramcount);
  for i:=1 to paramcount do writeln(paramstr(i));
  writeln;

  p^:='xxx yyy zzzz kkk';
  writeln(paramcount);
  for i:=1 to paramcount do writeln(paramstr(i));
  writeln;
End.

Of course there is the 127 byte limit. That works at least with tp7.0.

You might write following functions:

Function Pcount(const st:string127):integer;
var p:^string127;
    s:string127;
begin
  p:=ptr(prefixseg,$80);
  s:=p^;
  p^:=st;
  Pcount:=paramcount;
  p^:=s;
End;

(and Pstr in the same way)

So that the original command line is not messed up.

That is dirty, but it seems to work.

Osmo

Other Threads