[comp.lang.pascal] Getting the command line args

defaria@hpclapd.HP.COM (Andy DeFaria) (11/05/90)

This is probably obvious to you all but I scanned through the documentation
(pretty quickly) and I didn't come up with anything so I asking:

How does a running program access the command line arguments?  I would like
to do this with Turbo Pascal.

mcastle@mcs213f.cs.umr.edu (Mike Castle) (11/06/90)

In article <950041@hpclapd.HP.COM> defaria@hpclapd.HP.COM (Andy DeFaria) writes:
>How does a running program access the command line arguments?  I would like
>to do this with Turbo Pascal.
Well, looking in the manual (which you should have, cause you DID by the 
program :-), the function 'ParamCount' returns how many parameters were 
passed to the program, and the function 'ParamStr[I]' return's the I'th 
parameter.  ParamStr[0] will return the name of the program running (i.e., 
it's own name, just in case you want it to erase its self after the first
use :-)

If you haven't created an .exe file yet, you can set the passed parms in the
options menu ( alt-o,p ).		

Btw, this is on page 345-346 in the 'Reference Guide' manual.  :->
-- 
     Mike Castle       | Life is like a clock:  You can work constantly and be 
        Nexus          | right all the time, or not work at all and be right   
S087891@UMRVMA.UMR.EDU | at least twice a day. 
  mcastle@cs.umr.edu   |                           Love does not exist anymore.  

tswingle@oucsace.cs.OHIOU.EDU (Tom Swingle) (11/06/90)

In article <1611@umriscc.isc.umr.edu> mcastle@mcs213f.cs.umr.edu (Mike Castle) writes:
>Well, looking in the manual (which you should have, cause you DID by the 
>program :-), the function 'ParamCount' returns how many parameters were 
>passed to the program, and the function 'ParamStr[I]' return's the I'th 
>parameter.  ParamStr[0] will return the name of the program running (i.e., 
>it's own name, just in case you want it to erase its self after the first
>use :-)

Just a small syntactical point, ParamStr is a function rather than an array, so
the call should be 'ParamStr(I)' rather than 'ParamStr[I]'.  Probably most
everybody out there caught this also, but I thought I would post this in case
someone was having trouble.  Of course, like he said, it is in the manual,
which you all DO have a copy of don't you?  :-)
-- 
#include <signature.h>

kamal@wpi.WPI.EDU (Kamal Z Zamli) (11/07/90)

In article <950041@hpclapd.HP.COM> defaria@hpclapd.HP.COM (Andy DeFaria) writes:
>This is probably obvious to you all but I scanned through the documentation
>(pretty quickly) and I didn't come up with anything so I asking:
>
>How does a running program access the command line arguments?  I would like
>to do this with Turbo Pascal.

Try ParamStr and ParamCount functions:

Example:

___________________________________
program test;

begin
 if paramcount= 0 then 
  writeln (' No parameter is given to the program ')
 else
  writeln (' Parameter given to the program is = ',ParamStr(1));
end.

_________________________________

I hope that's help....

rio@image.me.utoronto.ca (Oscar del Rio) (11/07/90)

In article <2416@oucsace.cs.OHIOU.EDU> tswingle@oucsace.cs.OHIOU.EDU (Tom Swingle) writes:
>In article <1611@umriscc.isc.umr.edu> mcastle@mcs213f.cs.umr.edu (Mike Castle) writes:
>>Well, looking in the manual (which you should have, cause you DID by the 
>>program :-), the function 'ParamCount' returns how many parameters were 
>>passed to the program, and the function 'ParamStr[I]' return's the I'th 
>>parameter.  ParamStr[0] will return the name of the program running (i.e., 
>>it's own name, just in case you want it to erase its self after the first
>>use :-)
>
>Just a small syntactical point, ParamStr is a function rather than an array, so
>the call should be 'ParamStr(I)' rather than 'ParamStr[I]'.  Probably most
>everybody out there caught this also, but I thought I would post this in case
>someone was having trouble.  Of course, like he said, it is in the manual,
>which you all DO have a copy of don't you?  :-)
>-- 

Besides, I think that ParamStr(0) does not return the name of the program
if you are using DOS 2.x. (Yes, some people are still using DOS 2.11 :-).
Am I wrong? I have never used TURBO with DOS 2.x but I read it somewhere
(maybe in the manual :-)

Bye. 

docbrain@netmbx.UUCP (Frank Seidinger) (11/08/90)

kamal@wpi.WPI.EDU (Kamal Z Zamli) writes:

>program test;

>begin
> if paramcount= 0 then 
>  writeln (' No parameter is given to the program ')
> else
>  writeln (' Parameter given to the program is = ',ParamStr(1));
>end.

This works fine, if you try to scan 'true' parameters. That means, that each
of the parameters in the given command line is seperated by whitespace. But
what can I do if i have to catch an argument like:

    program 'This should be one long parameter'

I never solved this problem in Turbo Pascal.

Any comments?

----------------------------------------------------------------------
>>>>>>>>>>>>>>>>>> Wenn alles egal ist,  Budweiser ! <<<<<<<<<<<<<<<<<
----------------------------------------------------------------------
*   \/|\/     _          *                                           *
*  \/\|/\/   /  \        *  e-mail   docbrain@netmbx.in-berlin.de    *
*    \|/    | @  |       *           docbrain%netmbx@db0tui6.bitnet  *    
*     |     |   A|       *           docbrain%netmbx@tub.uucp        *
----------------------------------------------------------------------

mcastle@mcs213f.cs.umr.edu (Mike Castle) (11/08/90)

In article <90Nov6.193651est.4323@image.me.utoronto.ca> rio@image.me.utoronto.ca (Oscar del Rio) writes:
>Besides, I think that ParamStr(0) does not return the name of the program
>if you are using DOS 2.x. (Yes, some people are still using DOS 2.11 :-).
>Am I wrong? I have never used TURBO with DOS 2.x but I read it somewhere
>(maybe in the manual :-)
>
>Bye. 

Yup, just checked.  ParamStr(0) (notice the parens!! :-) return the name of the
program in Dos 3.0 or later.

BTW, a friend of mine found out that FORMAT for dos 2.0 (which she is using)
defaults to formatting 3.5HD disks to 360K.  Just thought it was interesting...
-- 
     Mike Castle       | Life is like a clock:  You can work constantly and be 
        Nexus          | right all the time, or not work at all and be right   
S087891@UMRVMA.UMR.EDU | at least twice a day. 
  mcastle@cs.umr.edu   |                           Love does not exist anymore.  

defaria@hpclapd.HP.COM (Andy DeFaria) (11/08/90)

I'm sorry.  Don't know what I was thinking and my  mind didn't click on the
fact the I should search for "PARM*"  (I think I was  thinking  of argc and
argv because I was busy installing Turbo C+).

Also my version of TP is 4.0 or thereabouts (checks in the mail for 5.5 :-)
and TP 4.0 doesn't have DOS.GetEnv (so I wrote one)  and I  guess I assumed
that TP 4.0 would have a give-me-the-command-line  type function either and
I'd have to wait for my 5.5 to arrive.  

RTFM!  I'm a software engineer!  I don't need no stinking manuals! :-)

Only kidding, only kidding.

Thanks to all who pointed me into the right direction.  Got it working now!

rxcob@minyos.xx.rmit.oz.au (Owen Baker) (11/08/90)

The following TP Pascal code segment returns the full command tail as a string.
I have found this usefull when you want to access all the parameters as they
were originally entered at the command line. As far as I know there is no
Turbo routine to do this directly.

<------- Cut here ------>
USES dos;

FUNCTION COMMAND_TAIL : string;
{Returns the command tail from the PSP (Program Segment Prefix). The command
 tail is the remainder of the command line that invoked the current program
 after the programs name}

var s : string;
begin
  move (mem [PrefixSeg : $0080], s, mem [PrefixSeg : $0080] + 1);
  command_tail := s;
end; {command_tail}

BEGIN {test}
  writeln (command_tail);
END.
<-------- Cut here -------->

+-------------------------------+-------------------------------------------+
|  Owen Baker                   |  Communication Services Unit              |
|  rxcob@minyos.xx.rmit.oz.au   |  RMIT - Victoria University of Technology |
|  (61) (3) 660-2038            |  Melbourne, Victoria, Australia           |
+-------------------------------+-------------------------------------------+

CDCKAB%EMUVM1.BITNET@cunyvm.cuny.edu ( Karl Brendel) (11/09/90)

In brief, hoping to put this question to rest :)  :

1) Declare a string and use Move() to copy the command line tail to
the string; or

2) Declare a pointer to a string and assign the address of the
command line tail to the pointer.

The command line tail is located at PrefexSeg:$80. It comprises the
entire DOS command line (or EXECed command, etc.), including all
white space, less the program name and any redirection/piping symbols
(> < |) and their arguments.



+-------------------------------------------------------------------------+
| Karl Brendel                                Centers for Disease Control |
| Internet: CDCKAB@EMUVM1.BITNET              Epidemiology Program Office |
| Bitnet: CDCKAB@EMUVM1                       Atlanta, GA, USA            |
| ILink/RIME: KARL BRENDEL                    phone 404/639-2709          |
| CIS  : 73307,3101                           fts       236-2709          |
| GEnie: K.BRENDEL                            Home of Epi Info 5.0        |
+-------------------------------------------------------------------------+

kamal@wpi.WPI.EDU (Kamal Z Zamli) (11/09/90)

In article <1365@netmbx.UUCP> docbrain@netmbx.UUCP (Frank Seidinger) writes
>
>program test;
>
>>begin
>> if paramcount= 0 then 
>>  writeln (' No parameter is given to the program ')
>> else
>>  writeln (' Parameter given to the program is = ',ParamStr(1));
>>end.
>
>This works fine, if you try to scan 'true' parameters. That means, that each
>of the parameters in the given command line is seperated by whitespace. But
>what can I do if>
>    program 'This should be one long parameter'
>
>I never solved this problem in Turbo Pascal.
>
>Any comments?
>


I'm not sure what you mean... Anyway if you want to access more command line
arguments, what you have to do is just call the paramstr....
i.e:
   paramstr(2) for argument no 2, paramstr(3) for argument no 3 etc.

I hope this helps.

awol@infopls.UUCP (Al Oomens) (11/10/90)

defaria@hpclapd.HP.COM (Andy DeFaria) writes:

> This is probably obvious to you all but I scanned through the documentation
> (pretty quickly) and I didn't come up with anything so I asking:
> 
> How does a running program access the command line arguments?  I would like
> to do this with Turbo Pascal.

There are two TP commands to access the command line:
ParamCount, and ParamStr. the first returns the number of parameters on 
the command line, while the second retrieves the specified parameter. For 
example:
If ParamCount < 1 THEN
   WriteLn( 'No Parameters on the command line!' );

IF ParamStr( 1 ) = '/v' THEN
   WriteLn( 'Verify turned on.' );

Also note that ParamStr( 0 ) will return the name of the program executed 
from the command line.

ts@uwasa.fi (Timo Salmi) (11/10/90)

In article <1365@netmbx.UUCP> docbrain@netmbx.UUCP (Frank Seidinger) writes:
... part deleted ...
>This works fine, if you try to scan 'true' parameters. That means, that each
>of the parameters in the given command line is seperated by whitespace. But
>what can I do if i have to catch an argument like:
>
>    program 'This should be one long parameter'
>
>I never solved this problem in Turbo Pascal.

From my FAQ:

18. *****
 Q: How can I obtain the entire command line (spaces and all)?

 A: ParamCount and ParamStr are for parsed parts of the command line
and cannot be used to get the command line exactly as it was. For
obtaining the command line unaltered, use, for example
  type CommandLineType = string[127];
  var  CommandLinePtr  : ^CommandLineType;
  begin
    CommandLinePtr := Ptr(PrefixSeg, $80);
    writeln (CommandLinePtr^);

...................................................................
Prof. Timo Salmi        (Moderating at anon. ftp site 128.214.12.3)
School of Business Studies, University of Vaasa, SF-65101, Finland
Internet: ts@chyde.uwasa.fi Funet: gado::salmi Bitnet: salmi@finfun

awol@infopls.UUCP (Al Oomens) (11/11/90)

> what can I do if i have to catch an argument like:
> 
>     program 'This should be one long parameter'
> 
> I never solved this problem in Turbo Pascal.
> 
> Any comments?
> 
Use a loop and concatenate each parameter to a string variable?

defaria@hpclapd.HP.COM (Andy DeFaria) (11/13/90)

>/ hpclapd:comp.lang.pascal / awol@infopls.UUCP (Al Oomens) /  1:37 pm  Nov 10, 1990 /
>> what can I do if i have to catch an argument like:
>> 
>>     program 'This should be one long parameter'
>> 
>> I never solved this problem in Turbo Pascal.
>> 
>> Any comments?
>> 
>Use a loop and concatenate each parameter to a string variable?

I'm not sure but I think that DOS would get rid of any special characters:

program  'This string has some special DOS characters !@#$%^&*()_+'

Does anybody know for sure?