Difference between revisions of "Command line parameters and environment variables"

From Lazarus wiki
Jump to navigationJump to search
(New page: When a program is started a user can give command line parameters and setup environment variables. For example the FreePascal compiler gets most of its parameters via command line options:...)
 
Line 1: Line 1:
 +
== Overview ==
 +
 
When a program is started a user can give command line parameters and setup environment variables. For example the FreePascal compiler gets most of its parameters via command line options:  
 
When a program is started a user can give command line parameters and setup environment variables. For example the FreePascal compiler gets most of its parameters via command line options:  
  
   fpc unit1.pas
+
   fpc -Fudirectory -gh unit1.pas
 +
 
 +
== Command line parameters ==
 +
 
 +
A pascal program can get access the parameters via ParamStr and ParamCount. ParamStr(0) is the program path itself. ParamStr(1) is the first parameter. ParamCount is the number of parameters.
 +
 
 +
<DELPHI>
 +
program Project1;
 +
 
 +
{$mode objfpc}{$H+}
 +
 
 +
var
 +
  i: Integer;
 +
begin
 +
  writeln('Program: ',ParamStr(0));
 +
  for i:=1 to ParamCount do
 +
    writeln('Param ',i,': ',ParamStr(i));
 +
end.
 +
</DELPHI>
 +
 
 +
For example:
 +
  $ /tmp/project1 -a
 +
  Program: /tmp/project1
 +
  Param 1: -a

Revision as of 16:49, 29 May 2008

Overview

When a program is started a user can give command line parameters and setup environment variables. For example the FreePascal compiler gets most of its parameters via command line options:

 fpc -Fudirectory -gh unit1.pas

Command line parameters

A pascal program can get access the parameters via ParamStr and ParamCount. ParamStr(0) is the program path itself. ParamStr(1) is the first parameter. ParamCount is the number of parameters.

<DELPHI> program Project1;

{$mode objfpc}{$H+}

var

 i: Integer;

begin

 writeln('Program: ',ParamStr(0));
 for i:=1 to ParamCount do
   writeln('Param ',i,': ',ParamStr(i));

end. </DELPHI>

For example:

 $ /tmp/project1 -a
 Program: /tmp/project1
 Param 1: -a