Automate pascal program command line input

I am looking to automate a pascal program to accept the readlnI() input automatically either via a parameter file or as parameters when the program is executed on the command line.

I am not really familiar with pascal and I am wondering how best to modify this source code such that the program will accept it's 'input' and 'output' filenames as parameters or if no parameters are supplied, prompt for the user to input them as the program is executing.
Program SPMake;
var
  f : text;
  w : text;
  In_file : String[12];
  In_pref : String[8];
  out_file : String[12];
  lines   : longint;
  suffix  : String[3];
  Alldone : Boolean;
  file_no : integer;
  c : char;
  asc : longint;
 
begin
  Write('Name of SPOOL files -> ');
  Readln(In_file);
  if (length(In_file)=0) or (pos('.',In_file)>0) then
  begin
    writeln('Invalid SPOOL file name.');
    halt;
  end;
 
  Write('File to be created -> ');
  Readln(Out_file);
 
  if (length(Out_file)=0) or (pos('.',Out_file)>9)
  or ((pos('.',In_file)=0) and (length(In_file)>8)) 
  or (length(Out_file)>12)   then
  begin
    writeln('Invalid output file name.');
    halt;
  end;
  In_Pref:=In_file;
 
  Assign(w, out_file);
  Rewrite(w);
    
 
 
  Alldone := False;
 
  file_no:=0;
  while not alldone do 
  Begin
    file_no:=file_no+1;
    str(file_no,suffix);
    while length(suffix)<3 do suffix:='0'+suffix;
 
    In_file:=In_pref+'.'+suffix;  
    Assign(f, In_file);
    {$I-}
    Reset(f);
    {$I+}
    If IOResult<>0 then
    begin
      Alldone:=True;
      if suffix='001' then 
      begin
        write('File ',in_file,' not found.');
        halt;
      end;
    end else
    begin  
      lines:=0;
      writeln('Reading ', In_file, ' into ',out_file);
      while not eof(f)  do
      begin
        read(f,c);
        asc:=ord(c);
        if (asc>9) and (asc<>29) then write(w,c);
 
        {if (asc>= 13) and (asc <= 126) then write(w,c);}
      end;
      close(f);
      erase(f);
    end;
  end;    
  Close(w);
  Writeln('Files combined.');
end.

Open in new window

foxbase-screenshot.JPG
james_j_hedgesAsked:
Who is Participating?
 
dbruntonCommented:
This is the type of code you are wanting.

GetBothFileNames is a procedure that does what you are doing now.  Your present file name getting code goes into this procedure.

GetOutFileName only gets the file name for the out file.  It is a subset of your present code.

These two lines assign the command line parameters to the in and out files.

InFile := ParamStr(1);
OutFile := ParamStr(2);


if ParamCount = 0 then
	GetBothFileNames
else  if ParamCount = 1 then
begin
	InFile := ParamStr(1);
	GetOutFileName
end
else
begin
	InFile := ParamStr(1);
	OutFile := ParamStr(2);
end;

Open in new window

0
 
Bill BachPresidentCommented:
In the C language, the main() function accepts two arguments, known commonly as argc and argv, which contains the count and the values or each command-line argument.  I would expect that your PASCAL compiler will have a similar feature.  A quick search seems to point to using an array called ParamStr[n] when n=0 is the program name, n=1 is the first parameter, and so on.  However, checking your specific compiler manual is the best way to figure this out.  Here's some links to get you started, though:
    http://www.hkbu.edu.hk/~bba_ism/ISM2110/pas074.htm
    http://www.mirrorservice.org/sites/www.gnu-pascal.de/gpc/Accessing-Command-Line-Arguments.html
0
 
dbruntonCommented:
This looks like Turbo Pascal.

In which case the code you want is the ParamCount and ParamStr functions.

if Paramcount < 1 then
writeln('No parameters on command line')
else
writeln(Paramcount, ' paramete(s)');


for i := 1 to Paramcount do
writeln(ParamStr(i));

Paramstr(index) is an array of the command line parameters.  Blanks and tabs separate the parameters on the command line.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
james_j_hedgesAuthor Commented:
Thanks for the advice. How did you determine whether the code is 'Pascal' or 'Turbo Pascal'? I'm assuming that I will need to use a different compiler depending on the language?

Thanks
0
 
dbruntonCommented:
Actually it could be standard Pascal.

I assumed the IOResult code was a Turbo Pascal function.  It's not.  It's in a couple of other Pascals as well.

This compiler directive

{$I+}

is Turbo Pascal plus it's used in other Pascal compilers such as Free Pascal and GNU Pascal.

The code for your file names looks like it's designed for DOS from the way the filenames are being created and checked.  The most common DOS Pascal compiler was the Turbo Pascal range.

But which compiler are you using for your code?  Several of the Turbo Pascal compilers are free and readily available.
0
 
james_j_hedgesAuthor Commented:
I'm not really sure which compiler is being used. I would imagine maybe Turbo Pascal. Do have any suggestions as to which specific compiler to use?

Thanks
0
 
dbruntonCommented:
Turbo Pascal 5.5

http://edn.embarcadero.com/article/20803

You'll need to create a free account to get to the download site.  There's a few tricks involved to get it running on Windows 2000 and XP but all of those are fixable/solvable.

Under Windows 9x or DOS there are no problems.  You may strike problems with fast CPUs but those are fixable as well.


Free Pascal would probably do the job as well.  http://www.freepascal.org/

Shouldn't be any problems getting it running under Win 2000 and XP.
0
 
james_j_hedgesAuthor Commented:
I've managed to update the pascal program to successfully command line parameters.

Thanks for your help!
Program SPMake (Output);
var
	arguments : integer;
  f : text;
  w : text;
  In_file : String[12];
  In_pref : String[8];
  Out_file : String[12];
  lines   : longint;
  suffix  : String[3];
  Alldone : Boolean;
  file_no : integer;
  c : char;
  asc : longint;
 
begin
 
	if ParamCount = 0 then
		begin
		Writeln('Please enter Input SPOOL files and Output SPOOL file names as parameters');
		end
	else if ParamCount = 1 then
					begin
			 		Writeln('Please enter both Input SPOOL files and Output SPOOL file names as parameters');
			 		end
			 else 
			 begin
  				In_file := ParamStr(1);
  				Out_file := ParamStr(2);
	end;
	
  if (length(In_file)=0) or (pos('.',In_file)>0) then
  begin
    writeln('Invalid SPOOL file name.');
    halt;
  end;

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.