home *** CD-ROM | disk | FTP | other *** search
/ ftp.update.uu.se / ftp.update.uu.se.2014.03.zip / ftp.update.uu.se / pub / rainbow / msdos / decus / RB120 / cla.pas < prev    next >
Pascal/Delphi Source File  |  1995-05-28  |  5KB  |  129 lines

  1. { This function provides sophisticated command line argument parsing.
  2.   One often wants a program to be able to take arguments from the command
  3.   line, yet at the same time provide prompts if they are not specified.
  4.   Arguments are assumed to be delimited by spaces, tabs, or slashes.  A slash
  5.   delimits a special argument, a switch.  Only non-positional switches are
  6.   implemented.  A positional switch is a switch that affects only part of the
  7.   command, i.e.
  8.     dir file1/date file2
  9.   meaning to add information about the date of file1, but not file2.
  10.   Nonpositional switches, as implemented here, always affect the entire
  11.   command.  So in this case, date information would be displayed for both
  12.   files.
  13.  
  14.   To request a non-switch parameter, a call to the function is made with the
  15.   boolean argument Switch set to false.  The function takes the first string
  16.   parameter off the command line.  If there are none, it looks at the value
  17.   of the Prompt argument.  If the prompt is specified, it prints it and reads
  18.   in a line.  This line may contain more than one argument: the user can
  19.   anticipate future prompts, or add switches.  The first argument on this line
  20.   is returned.  If there are no string arguments on the read line, the value
  21.   of the Default parameter is examined.  If it is empty, or contains a string,
  22.   that string is returned.  But if it contains the string '/', the prompt/read
  23.   is repeated.  This is for critical parameters.
  24.  
  25.   When a switch is requested, by setting the Switch parameter to true, the
  26.   first switch in the buffer is returned.  This is a string whose leading
  27.   character is a slash, i.e. '/date' in the earlier example.  If there are
  28.   none, an empty string is returned.  The Prompt and Default arguments have no
  29.   meaning when requesting a switch.
  30.  
  31.   The example program at the end of the file needs two file names: an input
  32.   name which must be specified, and an output name that defaults to the input
  33.   name with the extension '.OUT'.  It also checks for any switches specified.
  34.  
  35.   This system is modeled after the command line syntax of VAX/VMS, with the
  36.   omission of positional parameters, and some of the more esoteric things like
  37.   quoted arguments containing spaces.  Parsing of switches is left largely up
  38.   to the user program.
  39.  
  40.   Comments are welcomed (also in the sense that if you'd like to add comments
  41.   to my code, have fun)!
  42.  
  43.   -  Bela Lubkin
  44.  }
  45.  
  46. Type
  47.   BigString=String[127];
  48.  
  49. Function CommandLineArgument(Prompt, Default: BigString;
  50.                              Switch:Boolean): BigString;
  51.   Const
  52.     Buffered: Boolean=False;
  53.     CLBuffer: BigString='';
  54.     Delim: Set Of Char=[^I,' ','/'];
  55.  
  56.   Var
  57.     CommandLine: BigString Absolute CSeg:$0080;  { For MS-DOS  }
  58. (*  CommandLine: BigString Absolute DSeg:$0080;  { For CP/M-86 }  *)
  59. (*  CommandLine: BigString Absolute $0080;       { For CP/M-80 }  *)
  60.     CLA, CLBufferTemp: BigString;
  61.     Posn,PosnA: Integer;
  62.     Found: Boolean;
  63.  
  64.   Begin
  65.     If Not Buffered Then CLBuffer:=CommandLine;
  66.     Buffered:=True;
  67.     Posn:=1;
  68.     Found:=False;
  69.     While Not Found Do
  70.      Begin
  71.       CLA:='';
  72.       While (Posn<=Length(CLBuffer)) And (CLBuffer[Posn] In Delim) Do
  73.         Posn:=Posn+1;
  74.       PosnA:=Posn;
  75.       If (Posn<>1) And (Posn<=Length(CLBuffer)) Then
  76.         If CLBuffer[Posn-1]='/' Then
  77.          Begin
  78.           CLA:='/';
  79.           PosnA:=PosnA-1;
  80.          End;
  81.       While (Posn<=Length(CLBuffer)) And (Not (CLBuffer[Posn] In Delim)) Do
  82.        Begin
  83.         CLA:=CLA+CLBuffer[Posn];
  84.         Posn:=Posn+1;
  85.        End;
  86.       Found:=(Switch Xor (CLA[1]<>'/')) Or (CLA='');
  87.      End;
  88.     Delete(CLBuffer,PosnA,Posn-PosnA);
  89.     If (CLA='') And Not Switch Then
  90.      Begin
  91.       Found:=False;
  92.       While Not Found Do
  93.        Begin
  94.         If Prompt<>'' Then
  95.          Begin
  96.           Write(Prompt);
  97.           ReadLn(CLBufferTemp);
  98.           CLBuffer:=CLBufferTemp+CLBuffer;
  99.           CLA:=CommandLineArgument('','',False);
  100.          End;
  101.         If CLA='' Then CLA:=Default;
  102.         Found:=CLA<>'/';
  103.        End;
  104.      End;
  105.     CommandLineArgument:=CLA;
  106.   End;
  107.  
  108. { Example program.  To enable, delete the next line.  }
  109. (*
  110.  
  111. Var
  112.   InName,OutName,Temp: BigString;
  113.  
  114. Begin
  115.   InName:=CommandLineArgument('Input file name: ','/',False);
  116.   Temp:=InName;
  117.   If Pos('.',Temp)<>0 Then Delete(Temp,Pos('.',Temp),255);
  118.   OutName:=CommandLineArgument('Output file name: ',Temp+'.OUT',False);
  119.   WriteLn('Input file name = "',InName,'"');
  120.   WriteLn('Output file name = "',OutName,'"');
  121.   Temp:=' ';
  122.   While Temp<>'' Do
  123.    Begin
  124.     Temp:=CommandLineArgument('','',True);
  125.     WriteLn('Switch = "',Temp,'"');
  126.    End;
  127. End.
  128. (**)
  129.