NASutils Knowledge BaseUnofficialPolski

Pascal · lesson 21 of 30 · Files and errorsRead the command line

ParamCount gives the number of arguments and ParamStr(1) the first one. Halt ends the program with a code.

Programming on the NAS › Pascal in 30 lessons

21-arguments.pas Download 21-arguments.pas

program Arguments;
{$mode objfpc}{$H+}
var
  I: Integer;
begin
  if ParamCount = 0 then
  begin
    WriteLn('usage: 21-arguments NAME...');
    Halt(2);
  end;
  for I := 1 to ParamCount do
    WriteLn(I, ': ', ParamStr(I));
end.

Terminal

$ ./21-arguments Ann "Jan Example"
1: Ann
2: Jan Example
$ ./21-arguments; echo "exit code $?"
usage: 21-arguments NAME...
exit code 2

Exercise

Add up all numbers given as arguments.